From d37b8d74ac85ecf1cf9e7446153293e05a836133 Mon Sep 17 00:00:00 2001 From: hui1601 Date: Fri, 13 Oct 2023 09:24:44 +0900 Subject: [PATCH] clean code(append to buffer) --- library/src/parser.c | 57 ++++++++++++++++---------------------------- 1 file changed, 20 insertions(+), 37 deletions(-) diff --git a/library/src/parser.c b/library/src/parser.c index e84b6d6..3d43b1c 100644 --- a/library/src/parser.c +++ b/library/src/parser.c @@ -58,8 +58,6 @@ struct syntax syntax_defines[] = { {",,", ",,", AST_NODE_TYPE_LOWER_TEXT}, }; -struct syntax NULL_SYNTAX = {"", "", INT_MAX, 0}; - const size_t named_colors_size = 147; // Warning: MUST Check String Length(MAX: 29) before adding new color const char named_colors[][30] = {"aliceblue", "antiquewhite", "aqua", "aquamarine", "azure", "beige", "bisque", "black", @@ -95,6 +93,8 @@ const char named_colors[][30] = {"aliceblue", "antiquewhite", "aqua", "aquamarin "tomato", "turquoise", "violet", "wheat", "white", "whitesmoke", "yellow", "yellowgreen"}; +struct syntax NULL_SYNTAX = {"", "", INT_MAX, 0}; + // get syntax by type struct syntax *get_syntax_by_type(int type) { for (size_t i = 0; i < sizeof(syntax_defines) / sizeof(struct syntax); i++) { @@ -244,8 +244,7 @@ bool starts_with_color(const char *text, const size_t text_size) { break; } // check is alphabet, or number or '#' - if (!(text[i] >= 'a' && text[i] <= 'z') && !(text[i] >= 'A' && text[i] <= 'Z') && - !(text[i] >= '0' && text[i] <= '9') && text[i] != '#') { + if (!is_hex(text[i]) && text[i] != '#') { break; } } @@ -264,6 +263,18 @@ bool starts_with_color(const char *text, const size_t text_size) { return false; } +char* append_buffer(char* buf, size_t *buf_size, char c) { + char* tmp = realloc(buf, sizeof(char) * (*buf_size + 2)); + if (tmp == NULL) { + abort(); + } + buf = tmp; + buf[*buf_size] = c; + buf[*buf_size + 1] = '\0'; + *buf_size += 1; + return buf; +} + // Just perform the parsing process // parser ast_node *parse(const char *text, const size_t text_size) { @@ -411,14 +422,7 @@ ast_node *parse(const char *text, const size_t text_size) { // combine current_node = current_node->children[current_node->children_size - 1]; // add \n to str_buf - str_buf[str_buf_size] = '\n'; - char *tmp = realloc(str_buf, sizeof(char *) * (str_buf_size + 2)); - if (tmp == NULL) { - abort(); - } - str_buf = tmp; - str_buf_size++; - str_buf[str_buf_size] = '\0'; + str_buf = append_buffer(str_buf, &str_buf_size, '\n'); // skip syntax i += strlen(current_syntax.start) - 1; is_break = true; @@ -520,17 +524,9 @@ ast_node *parse(const char *text, const size_t text_size) { char *link_buf = calloc(sizeof(char), 1); // get link. we get all things needed to create link node at once for (size_t k = i + strlen(current_syntax.start); k < text_size - 1; k++) { - link_size++; if (text[k] == '\\') { + link_buf = append_buffer(link_buf, &link_size, text[k + 1]); k++; - // realloc link_buf - char *tmp = realloc(link_buf, sizeof(char *) * (link_size + 2)); - if (tmp == NULL) { - abort(); - } - link_buf = tmp; - link_buf[link_size - 1] = text[k + 1]; - link_buf[link_size] = '\0'; continue; } if (text[k] == '\n') { @@ -544,15 +540,9 @@ ast_node *parse(const char *text, const size_t text_size) { is_link_syntax = true; break; } - // realloc link_buf - char *tmp = realloc(link_buf, sizeof(char *) * (link_size + 2)); - if (tmp == NULL) { - abort(); - } - link_buf = tmp; - link_buf[link_size - 1] = text[k]; - link_buf[link_size] = '\0'; + link_buf = append_buffer(link_buf, &link_size, text[k]); } + link_size++; if (!is_link_syntax) { free(link_buf); continue; @@ -613,14 +603,7 @@ ast_node *parse(const char *text, const size_t text_size) { } // append text[i] to str_buf // realloc str_buf - str_buf[str_buf_size] = text[i]; - char *tmp = realloc(str_buf, sizeof(char *) * (str_buf_size + 2)); - if (tmp == NULL) { - abort(); - } - str_buf = tmp; - str_buf_size++; - str_buf[str_buf_size] = '\0'; + str_buf = append_buffer(str_buf, &str_buf_size, text[i]); // if current node is LINE syntax, and text[i] is '\n', this is not correct syntax if (current_node_syntax->flags & SYNTAX_FLAG_LINE && text[i] == '\n') {