diff --git a/libs/lex_parser/src/lex_parser.c b/libs/lex_parser/src/lex_parser.c index fb55280..3557ad9 100644 --- a/libs/lex_parser/src/lex_parser.c +++ b/libs/lex_parser/src/lex_parser.c @@ -293,7 +293,7 @@ cbool lex_parse_string(core_probe_stream_t *input, core_pos_t *pos, if (val == -1) { LOG_ERROR("Invalid escape character it is \\%c [%d]", ch, ch); } else { - cstring_push(&str, val); + cstring_append_ch(&str, val); continue; } } 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_pos_next(pos); - cstring_push(&str, ch); + cstring_append_ch(&str, ch); } *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') || (ch >= 'A' && ch <= 'Z')) { while (1) { - cstring_push(output, ch); + cstring_append_ch(output, ch); core_probe_stream_consume(stream); core_pos_next(pos); ch = core_probe_stream_peek(stream); diff --git a/libs/lexer/tests/test_run.c b/libs/lexer/tests/test_run.c index a2c38d8..abcb15e 100644 --- a/libs/lexer/tests/test_run.c +++ b/libs/lexer/tests/test_run.c @@ -68,7 +68,7 @@ int main(int argc, char *argv[]) { core_mem_probe_stream_init(&mem_stream, buffer, fsize, false); Assert(stream != null); 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_tok_t tok; diff --git a/runtime/libcore/include/core_str.h b/runtime/libcore/include/core_str.h index 29f8eb3..e34045f 100644 --- a/runtime/libcore/include/core_str.h +++ b/runtime/libcore/include/core_str.h @@ -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); } /** diff --git a/runtime/libcore/include/core_stream.h b/runtime/libcore/include/core_stream.h index be74ad6..373620d 100644 --- a/runtime/libcore/include/core_stream.h +++ b/runtime/libcore/include/core_stream.h @@ -46,7 +46,7 @@ struct core_probe_stream { cbool (*is_at_end)(core_probe_stream_t *stream); /// @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) { @@ -86,8 +86,8 @@ static inline cbool core_probe_stream_has_more(core_probe_stream_t *self) { return !self->is_at_end(self); } -static inline void core_probe_stream_destroy(core_probe_stream_t *self) { - self->destroy(self); +static inline void core_probe_stream_drop(core_probe_stream_t *self) { + self->drop(self); } #ifndef __SMCC_CORE_NO_MEM_PROBE_STREAM__ diff --git a/runtime/libcore/src/stream.c b/runtime/libcore/src/stream.c index 70c9a66..dcdf312 100644 --- a/runtime/libcore/src/stream.c +++ b/runtime/libcore/src/stream.c @@ -68,8 +68,8 @@ static cbool mem_probe_stream_back(core_probe_stream_t *_stream) { return true; } -static usize mem_probe_stream_read_buf(core_probe_stream_t *_stream, char *buffer, - usize count) { +static usize mem_probe_stream_read_buf(core_probe_stream_t *_stream, + char *buffer, usize count) { Assert(_stream != null); 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.reset = mem_probe_stream_reset; 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; }