feat(runtime): 添加字符串和内存操作工具函数
- 在 `core_mem.h` 中新增 `smcc_strhash32`、`smcc_strlen` 和 `smcc_strcmp` 函数, 提供 C 字符串的哈希、长度获取和比较功能 - 完善 `core_str.h` 中 `cstring_t` 结构体及相关操作函数的注释说明 - 更新 `core_str.h` 头文件保护宏命名,增强模块标识一致性 - 修改 `core_vec.h` 文件头部保护宏名称以匹配实际文件名 另外,在 lexer 测试运行代码中引入日志相关头文件并调整日志级别设置逻辑。
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include <lexer.h>
|
||||
#include <lexer_log.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -25,6 +26,8 @@ int main(int argc, char *argv[]) {
|
||||
if (argc == 3 && strcmp(argv[2], "--debug") == 0) {
|
||||
log_set_level(NULL, LOG_LEVEL_ALL);
|
||||
} else {
|
||||
// FIXME it is a hack lexer_logger
|
||||
log_set_level(&__smcc_lexer_log, LOG_LEVEL_NOTSET);
|
||||
log_set_level(NULL, LOG_LEVEL_INFO | LOG_LEVEL_WARN | LOG_LEVEL_ERROR);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,12 @@ void *smcc_memmove(void *dest, const void *src, usize n);
|
||||
void *smcc_memset(void *s, int c, usize n);
|
||||
int smcc_memcmp(const void *s1, const void *s2, usize n);
|
||||
|
||||
/**
|
||||
* @brief 使用FNV-1a进行 C 字符串哈希
|
||||
*
|
||||
* @param s
|
||||
* @return u32
|
||||
*/
|
||||
static inline u32 smcc_strhash32(const char *s) {
|
||||
u32 hash = 2166136261u; // FNV-1a偏移基础值
|
||||
while (*s) {
|
||||
@@ -17,6 +23,12 @@ static inline u32 smcc_strhash32(const char *s) {
|
||||
return hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 获取 C 字符串长度
|
||||
*
|
||||
* @param str
|
||||
* @return usize
|
||||
*/
|
||||
static inline usize smcc_strlen(const char *str) {
|
||||
usize len = 0;
|
||||
while (*str) {
|
||||
@@ -26,6 +38,13 @@ static inline usize smcc_strlen(const char *str) {
|
||||
return len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 比较两个 C 字符串
|
||||
*
|
||||
* @param s1
|
||||
* @param s2
|
||||
* @return int
|
||||
*/
|
||||
static inline int smcc_strcmp(const char *s1, const char *s2) {
|
||||
while (*s1 && *s2 && *s1 == *s2) {
|
||||
s1++;
|
||||
|
||||
@@ -1,25 +1,34 @@
|
||||
#ifndef __CORE_STR_H__
|
||||
#define __CORE_STR_H__
|
||||
#ifndef __SMCC_CORE_STR_H__
|
||||
#define __SMCC_CORE_STR_H__
|
||||
|
||||
#include "core_impl.h"
|
||||
#include "core_log.h"
|
||||
#include "core_type.h"
|
||||
|
||||
/**
|
||||
* @brief 动态字符串结构体
|
||||
* @attention 创建的字符串对象需要使用 cstring_free 释放
|
||||
*/
|
||||
typedef struct cstring {
|
||||
usize size;
|
||||
usize cap;
|
||||
char *data;
|
||||
usize size; /**< 字符串当前大小(包括结尾的'\0')*/
|
||||
usize cap; /**< 分配的容量 */
|
||||
char *data; /**< 实际存储数据的指针 */
|
||||
} cstring_t;
|
||||
|
||||
/**
|
||||
* 创建一个新的空字符串
|
||||
* @brief 创建一个新的空动态字符串对象
|
||||
*
|
||||
* @return cstring_t 初始化后的对象
|
||||
*/
|
||||
static inline cstring_t cstring_new(void) {
|
||||
return (cstring_t){.data = null, .size = 0, .cap = 0};
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 C 字符串创建 Rust 风格字符串
|
||||
* @brief 从 C 风格字符串创建一个动态字符串副本
|
||||
*
|
||||
* @param s 输入的 C 风格字符串
|
||||
* @return cstring_t 新建对象,包含输入字符串的副本
|
||||
*/
|
||||
static inline cstring_t cstring_from_cstr(const char *s) {
|
||||
if (s == null) {
|
||||
@@ -40,7 +49,9 @@ static inline cstring_t cstring_from_cstr(const char *s) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放字符串资源
|
||||
* @brief 释放动态字符串占用的内存资源
|
||||
*
|
||||
* @param str 要被释放的字符串指针
|
||||
*/
|
||||
static inline void cstring_free(cstring_t *str) {
|
||||
if (str && str->data && str->cap != 0) {
|
||||
@@ -52,7 +63,11 @@ static inline void cstring_free(cstring_t *str) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 向字符串追加内容
|
||||
* @brief 向动态字符串末尾追加一段 C 风格字符串
|
||||
*
|
||||
* @param str 目标动态字符串指针
|
||||
* @param data 要追加的 C 字符串指针
|
||||
* @param len 要追加的 C 字符串长度
|
||||
*/
|
||||
static inline void cstring_push_cstr(cstring_t *str, const char *data,
|
||||
usize len) {
|
||||
@@ -89,28 +104,39 @@ static inline void cstring_push_cstr(cstring_t *str, const char *data,
|
||||
}
|
||||
|
||||
/**
|
||||
* 向字符串追加单个字符
|
||||
* @brief 向动态字符串末尾追加一个字符
|
||||
*
|
||||
* @param str 目标动态字符串指针
|
||||
* @param ch 要追加的字符
|
||||
*/
|
||||
static inline void cstring_push(cstring_t *str, char ch) {
|
||||
cstring_push_cstr(str, &ch, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字符串长度
|
||||
* @brief 获取动态字符串的实际内容长度 不包括结尾的'\0'
|
||||
*
|
||||
* @param str 动态字符串指针
|
||||
* @return usize 字符串实际长度
|
||||
*/
|
||||
static inline usize cstring_len(const cstring_t *str) {
|
||||
return str ? str->size - 1 : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查字符串是否为空
|
||||
* @brief 判断动态字符串是否为空
|
||||
*
|
||||
* @param str 动态字符串指针
|
||||
* @return cbool
|
||||
*/
|
||||
static inline cbool cstring_is_empty(const cstring_t *str) {
|
||||
return str == null || str->size == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空字符串内容但保留分配的内存
|
||||
* @brief 清空动态字符串内容但保留已分配的内存空间
|
||||
*
|
||||
* @param str 动态字符串指针
|
||||
*/
|
||||
static inline void cstring_clear(cstring_t *str) {
|
||||
if (str) {
|
||||
@@ -122,7 +148,10 @@ static inline void cstring_clear(cstring_t *str) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 C 风格字符串
|
||||
* @brief 将动态字符串转换为 C 风格字符串
|
||||
*
|
||||
* @param str 动态字符串指针
|
||||
* @return char* 返回指向内部缓冲区的 C 风格字符串指针
|
||||
*/
|
||||
static inline char *cstring_as_cstr(const cstring_t *str) {
|
||||
if (str == null || str->data == null) {
|
||||
@@ -131,4 +160,4 @@ static inline char *cstring_as_cstr(const cstring_t *str) {
|
||||
return str->data;
|
||||
}
|
||||
|
||||
#endif /* __CORE_STR_H__ */
|
||||
#endif /* __SMCC_CORE_STR_H__ */
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
* 提供类型安全的动态数组容器实现,支持自动扩容和基本操作
|
||||
*/
|
||||
|
||||
#ifndef __SMCC_CORE_DARRAY_H__
|
||||
#define __SMCC_CORE_DARRAY_H__
|
||||
#ifndef __SMCC_CORE_VEC_H__
|
||||
#define __SMCC_CORE_VEC_H__
|
||||
|
||||
#include "core_impl.h"
|
||||
#include "core_type.h"
|
||||
@@ -114,4 +114,4 @@
|
||||
(vec).size = (vec).cap = 0; \
|
||||
} while (0)
|
||||
|
||||
#endif // __SMCC_CORE_DARRAY_H__
|
||||
#endif // __SMCC_CORE_VEC_H__
|
||||
|
||||
Reference in New Issue
Block a user