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

@@ -293,7 +293,7 @@ cbool lex_parse_string(core_probe_stream_t *input, core_pos_t *pos,
if (val == -1) { if (val == -1) {
LOG_ERROR("Invalid escape character it is \\%c [%d]", ch, ch); LOG_ERROR("Invalid escape character it is \\%c [%d]", ch, ch);
} else { } else {
cstring_push(&str, val); cstring_append_ch(&str, val);
continue; continue;
} }
} else if (ch == '"') { } else if (ch == '"') {
@@ -304,7 +304,7 @@ cbool lex_parse_string(core_probe_stream_t *input, core_pos_t *pos,
core_probe_stream_consume(stream); core_probe_stream_consume(stream);
core_pos_next(pos); core_pos_next(pos);
cstring_push(&str, ch); cstring_append_ch(&str, ch);
} }
*output = str; *output = str;
@@ -419,7 +419,7 @@ cbool lex_parse_identifier(core_probe_stream_t *input, core_pos_t *pos,
} else if (ch == '_' || (ch >= 'a' && ch <= 'z') || } else if (ch == '_' || (ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z')) { (ch >= 'A' && ch <= 'Z')) {
while (1) { while (1) {
cstring_push(output, ch); cstring_append_ch(output, ch);
core_probe_stream_consume(stream); core_probe_stream_consume(stream);
core_pos_next(pos); core_pos_next(pos);
ch = core_probe_stream_peek(stream); ch = core_probe_stream_peek(stream);

View File

@@ -68,7 +68,7 @@ int main(int argc, char *argv[]) {
core_mem_probe_stream_init(&mem_stream, buffer, fsize, false); core_mem_probe_stream_init(&mem_stream, buffer, fsize, false);
Assert(stream != null); Assert(stream != null);
cstring_clear(&stream->name); cstring_clear(&stream->name);
cstring_push_cstr(&stream->name, file_name, strlen(file_name)); cstring_append_cstr(&stream->name, file_name, strlen(file_name));
lexer_init(&lexer, stream); lexer_init(&lexer, stream);
lexer_tok_t tok; lexer_tok_t tok;

View File

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

View File

@@ -46,7 +46,7 @@ struct core_probe_stream {
cbool (*is_at_end)(core_probe_stream_t *stream); cbool (*is_at_end)(core_probe_stream_t *stream);
/// @brief 销毁流并释放资源 /// @brief 销毁流并释放资源
void (*destroy)(core_probe_stream_t *stream); void (*drop)(core_probe_stream_t *stream);
}; };
static inline int core_probe_stream_consume(core_probe_stream_t *self) { static inline int core_probe_stream_consume(core_probe_stream_t *self) {
@@ -86,8 +86,8 @@ static inline cbool core_probe_stream_has_more(core_probe_stream_t *self) {
return !self->is_at_end(self); return !self->is_at_end(self);
} }
static inline void core_probe_stream_destroy(core_probe_stream_t *self) { static inline void core_probe_stream_drop(core_probe_stream_t *self) {
self->destroy(self); self->drop(self);
} }
#ifndef __SMCC_CORE_NO_MEM_PROBE_STREAM__ #ifndef __SMCC_CORE_NO_MEM_PROBE_STREAM__

View File

@@ -68,8 +68,8 @@ static cbool mem_probe_stream_back(core_probe_stream_t *_stream) {
return true; return true;
} }
static usize mem_probe_stream_read_buf(core_probe_stream_t *_stream, char *buffer, static usize mem_probe_stream_read_buf(core_probe_stream_t *_stream,
usize count) { char *buffer, usize count) {
Assert(_stream != null); Assert(_stream != null);
core_mem_probe_stream_t *stream = (core_mem_probe_stream_t *)_stream; core_mem_probe_stream_t *stream = (core_mem_probe_stream_t *)_stream;
@@ -164,7 +164,7 @@ core_probe_stream_t *core_mem_probe_stream_init(core_mem_probe_stream_t *stream,
stream->stream.read_buf = mem_probe_stream_read_buf; stream->stream.read_buf = mem_probe_stream_read_buf;
stream->stream.reset = mem_probe_stream_reset; stream->stream.reset = mem_probe_stream_reset;
stream->stream.is_at_end = mem_probe_stream_is_at_end; stream->stream.is_at_end = mem_probe_stream_is_at_end;
stream->stream.destroy = mem_probe_stream_destroy; stream->stream.drop = mem_probe_stream_destroy;
return (core_probe_stream_t *)stream; return (core_probe_stream_t *)stream;
} }