[refactor] Organize STRING

This commit is contained in:
2026-06-05 18:38:45 +08:00
Unverified
parent da03b301fb
commit 9ee88ef7b9
19 changed files with 124 additions and 51 deletions
+2 -7
View File
@@ -1,10 +1,5 @@
#pragma once
#include <types.h>
#define ASM asm volatile
static SUINT32 strlen(const char* arr) { // 获取string长度
SSINT32 i = 0;
while (arr[i++] != '\0');
return i - 1;
}
#include <string_utils.h>
#define ASM asm volatile
+2 -1
View File
@@ -1,7 +1,8 @@
#pragma once
#include <graphics/context.h>
#include <string_utils.h>
void pf_print_char(char c, SUINT32 basex, SUINT32 basey,
EFI_GRAPHICS_OUTPUT_BLT_PIXEL color = {255, 255, 255, 255}); // Pixel Font 打印字符
void pf_print(const char* text, SUINT32 basex, SUINT32 basey, EFI_GRAPHICS_OUTPUT_BLT_PIXEL color = {255, 255, 255, 255}); // Pixel Font 打印string
void pf_print(String text, SUINT32 basex, SUINT32 basey, EFI_GRAPHICS_OUTPUT_BLT_PIXEL color = {255, 255, 255, 255}); // Pixel Font 打印string
+2 -1
View File
@@ -1,6 +1,7 @@
#pragma once
#include <efi.h>
#include <string_utils.h>
EFI_STATUS fs_init();
void fs_list();
EFI_STATUS fs_read(const CHAR16 *Path, void **Buffer, UINTN *Size);
EFI_STATUS fs_read(WString Path, void **Buffer, UINTN *Size);
+2 -1
View File
@@ -2,6 +2,7 @@
#include <efi.h>
#include <common.h>
#include <string_utils.h>
#define LAYER_MAX 32
#define LAYER_NAME_LEN 32
@@ -29,7 +30,7 @@ typedef struct layer {
void layer_init(void);
// Layer management
layer_t* layer_create(const char* name, layer_type_t type, UINT32 w, UINT32 h);
layer_t* layer_create(String name, layer_type_t type, UINT32 w, UINT32 h);
void layer_destroy(layer_t* layer);
void layer_set_z(layer_t* layer, SSINT32 z);
void layer_set_pos(layer_t* layer, SSINT32 x, SSINT32 y);
+2 -1
View File
@@ -3,6 +3,7 @@
#include <efi.h>
#include <memory/pmm.h>
#include <common.h>
#include <string_utils.h>
#define TASK_STACK_SIZE (PAGE_SIZE * 4) // 16 KB kernel stack per task
#define TASK_MAX 32
@@ -26,7 +27,7 @@ typedef struct task {
} task_t;
// Create a new task. Returns task pointer or NULL on failure.
task_t* task_create(const char* name, void (*entry)(void));
task_t* task_create(String name, void (*entry)(void));
// Yield CPU to next ready task (cooperative)
void yield(void);
+2 -1
View File
@@ -2,6 +2,7 @@
#include <efi.h>
#include <efiser.h>
#include <string_utils.h>
struct serial_context { // 串行内容结构体
EFI_SERIAL_IO_PROTOCOL *SerialIo;
@@ -10,7 +11,7 @@ struct serial_context { // 串行内容结构体
extern serial_context g_serial;
void serial_init(EFI_SERIAL_IO_PROTOCOL *SerialIo); // 初始化串行驱动
void serial_write(const char *str); // 往串行写string
void serial_write(String str); // 往串行写string
void serial_write_char(char c); // 往串行写char(不推荐使用)
void serial_write_hex(UINTN val); // 往串行写十六进制数字
char serial_read_char(); // 读串行
+91
View File
@@ -0,0 +1,91 @@
#pragma once
#include <types.h>
// === Unified string types ===
// Use these throughout the kernel instead of raw char*/const char*.
typedef const char* String; // 8-bit narrow string
typedef const uint16_t* WString; // 16-bit wide string (CHAR16)
// === Narrow string utilities (String) ===
static inline SUINT32 str_len(String s) {
SUINT32 i = 0;
while (s[i]) i++;
return i;
}
static inline SSINT32 str_cmp(String a, String b) {
while (*a && *a == *b) { a++; b++; }
return (SUINT8)*a - (SUINT8)*b;
}
static inline bool str_eq(String a, String b) {
return str_cmp(a, b) == 0;
}
// Copy up to max-1 chars, always null-terminates.
static inline void str_copy(char* dst, String src, SUINT32 max) {
if (max == 0) return;
SUINT32 i = 0;
while (src[i] && i < max - 1) {
dst[i] = src[i];
i++;
}
dst[i] = '\0';
}
// === Wide string utilities (WString) ===
static inline SUINT32 wstr_len(WString s) {
SUINT32 i = 0;
while (s[i]) i++;
return i;
}
static inline SSINT32 wstr_cmp(WString a, WString b) {
while (*a && *a == *b) { a++; b++; }
return (SUINT16)*a - (SUINT16)*b;
}
static inline bool wstr_eq(WString a, WString b) {
return wstr_cmp(a, b) == 0;
}
static inline void wstr_copy(uint16_t* dst, WString src, SUINT32 max) {
if (max == 0) return;
SUINT32 i = 0;
while (src[i] && i < max - 1) {
dst[i] = src[i];
i++;
}
dst[i] = 0;
}
// Convert wide string to ASCII (non-printable -> '?')
static inline void wstr_to_ascii(char* dst, WString src, SUINT32 max) {
if (max == 0) return;
SUINT32 i = 0;
while (src[i] && i < max - 1) {
if (src[i] >= 0x20 && src[i] <= 0x7E)
dst[i] = (char)src[i];
else
dst[i] = '?';
i++;
}
dst[i] = '\0';
}
// === Memory utilities ===
static inline void mem_set(void* dst, SUINT8 val, SUINT32 size) {
SUINT8* d = (SUINT8*)dst;
for (SUINT32 i = 0; i < size; i++) d[i] = val;
}
static inline void mem_copy(void* dst, const void* src, SUINT32 size) {
SUINT8* d = (SUINT8*)dst;
const SUINT8* s = (const SUINT8*)src;
for (SUINT32 i = 0; i < size; i++) d[i] = s[i];
}