feat(core): rename string and stream functions for clarity

- Rename `cstring_push` to `cstring_append_ch` and `cstring_push_cstr` to `cstring_append_cstr` for consistent naming with new `cstring_append` function
- Update all callers in lexer and tests to use new function names
- Rename stream `destroy` method to `drop` for consistency with resource management conventions
- Fix potential overflow in string capacity calculation by adjusting growth logic
This commit is contained in:
zzy
2025-12-09 18:04:53 +08:00
parent 36bff64a91
commit 186602a301
5 changed files with 28 additions and 16 deletions

View File

@@ -72,13 +72,14 @@ static inline void cstring_free(cstring_t *str) {
* @param data 要追加的 C 字符串指针
* @param len 要追加的 C 字符串长度
*/
static inline void cstring_push_cstr(cstring_t *str, const char *data,
usize len) {
static inline void cstring_append_cstr(cstring_t *str, const char *data,
usize len) {
if (str == null || data == null || len == 0) {
return;
}
if (str->cap == 0) {
// add '\0' to str
str->size = 1;
}
@@ -86,12 +87,13 @@ static inline void cstring_push_cstr(cstring_t *str, const char *data,
if (str->size + len > str->cap) {
usize new_cap = str->cap;
while (new_cap < str->size + len) {
new_cap *= 2;
// FIXME write by AI 处理溢出情况
if (new_cap == 0) {
new_cap = str->size + len;
break;
} else {
new_cap *= 2;
}
// FIXME 处理溢出情况
}
char *new_data = (char *)smcc_realloc(str->data, new_cap);
@@ -106,14 +108,24 @@ static inline void cstring_push_cstr(cstring_t *str, const char *data,
str->data[str->size - 1] = '\0'; // 保证 C 字符串兼容性
}
/**
* @brief 向动态字符串末尾追加另一个动态字符串
*
* @param str 目标动态字符串指针
* @param other 要追加的动态字符串指针
*/
static inline void cstring_append(cstring_t *str, const cstring_t *other) {
cstring_append_cstr(str, other->data, other->size - 1);
}
/**
* @brief 向动态字符串末尾追加一个字符
*
* @param str 目标动态字符串指针
* @param ch 要追加的字符
*/
static inline void cstring_push(cstring_t *str, char ch) {
cstring_push_cstr(str, &ch, 1);
static inline void cstring_append_ch(cstring_t *str, char ch) {
cstring_append_cstr(str, &ch, 1);
}
/**