From 5c601dfd19a75bbb4fc2e54cbb6749922162c811 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Sun, 22 Jun 2008 15:12:42 +0200 Subject: [PATCH] Use extensible buffers in markup parsing. This should avoid quite a few malloc calls. Signed-off-by: Pierre Habouzit --- client.c | 3 +-- common/draw.c | 4 +--- common/markup.c | 38 ++++++++------------------------------ widgets/taglist.c | 3 +-- widgets/tasklist.c | 1 - 5 files changed, 11 insertions(+), 38 deletions(-) diff --git a/client.c b/client.c index c18fece8..00928b50 100644 --- a/client.c +++ b/client.c @@ -778,8 +778,7 @@ client_markup_parse(client_t *c, const char *str, ssize_t len) if(markup_parse(p, str, len)) { - ret = p->text; - p->text = NULL; + ret = buffer_detach(&p->text); } else ret = a_strdup(str); diff --git a/common/draw.c b/common/draw.c index 66f909a5..a4d92635 100644 --- a/common/draw.c +++ b/common/draw.c @@ -261,9 +261,7 @@ draw_text_markup_expand(draw_parser_data_t *data, data->margin.right = atoi(p->attribute_values[2][i]); /* stole text */ - data->text = p->text; - p->text = NULL; - + data->text = buffer_detach(&p->text); markup_parser_data_delete(&p); return true; diff --git a/common/markup.c b/common/markup.c index a7bc6c8c..6906d393 100644 --- a/common/markup.c +++ b/common/markup.c @@ -47,9 +47,7 @@ markup_parse_start_element(GMarkupParseContext *context __attribute__ ((unused)) GError **error __attribute__ ((unused))) { markup_parser_data_t *p = (markup_parser_data_t *) user_data; - char *newtext; int i, j; - ssize_t len; for(i = 0; p->elements[i]; i++) if(!a_strcmp(element_name, p->elements[i])) @@ -66,9 +64,7 @@ markup_parse_start_element(GMarkupParseContext *context __attribute__ ((unused)) if(p->elements_sub && p->elements_sub[i]) { - asprintf(&newtext, "%s%s", NONULL(p->text), p->elements_sub[i]); - p_delete(&p->text); - p->text = newtext; + buffer_adds(&p->text, p->elements_sub[i]); } return; @@ -76,22 +72,13 @@ markup_parse_start_element(GMarkupParseContext *context __attribute__ ((unused)) if(a_strcmp(element_name, "markup")) { - if(!(len = asprintf(&newtext, "%s<%s ", NONULL(p->text), element_name))) - return; - len++; /* add \0 that asprintf does not return in len */ + buffer_addf(&p->text, "<%s", element_name); for(i = 0; attribute_names[i]; i++) { - len += a_strlen(attribute_names[i]) + a_strlen(attribute_values[i]) + 5; - p_realloc(&newtext, len); - a_strcat(newtext, len, attribute_names[i]); - a_strcat(newtext, len, "=\""); - a_strcat(newtext, len, attribute_values[i]); - a_strcat(newtext, len, "\" "); + buffer_addf(&p->text, " %s=\"%s\"", attribute_names[i], + attribute_values[i]); } - p_realloc(&newtext, ++len); - a_strcat(newtext, len, ">"); - p_delete(&p->text); - p->text = newtext; + buffer_addc(&p->text, '>'); } } @@ -110,7 +97,6 @@ markup_parse_end_element(GMarkupParseContext *context __attribute__ ((unused)), GError **error __attribute__ ((unused))) { markup_parser_data_t *p = (markup_parser_data_t *) user_data; - char *newtext; int i; for(i = 0; p->elements[i]; i++) @@ -118,11 +104,7 @@ markup_parse_end_element(GMarkupParseContext *context __attribute__ ((unused)), return; if(a_strcmp(element_name, "markup")) - { - asprintf(&newtext, "%s", p->text, element_name); - p_delete(&p->text); - p->text = newtext; - } + buffer_addf(&p->text, "", element_name); } /** Callback to invoke when some text is seen (text is always inside an @@ -144,14 +126,10 @@ markup_parse_text(GMarkupParseContext *context __attribute__ ((unused)), GError **error __attribute__ ((unused))) { markup_parser_data_t *p = (markup_parser_data_t *) user_data; - ssize_t rlen; char *esc; esc = g_markup_escape_text(text, text_len); - text_len = a_strlen(esc); - rlen = a_strlen(p->text) + 1 + text_len; - p_realloc(&p->text, rlen); - a_strncat(p->text, rlen, esc, text_len); + buffer_adds(&p->text, esc); p_delete(&esc); } @@ -202,7 +180,7 @@ markup_parser_data_delete(markup_parser_data_t **p) p_delete(&(*p)->attribute_names); p_delete(&(*p)->attribute_values); - buffer_wipe(&p->text); + buffer_wipe(&(*p)->text); p_delete(p); } diff --git a/widgets/taglist.c b/widgets/taglist.c index 60988eaa..1a5d2fd3 100644 --- a/widgets/taglist.c +++ b/widgets/taglist.c @@ -67,8 +67,7 @@ tag_markup_parse(tag_t *t, const char *str, ssize_t len) if(markup_parse(p, str, len)) { - ret = p->text; - p->text = NULL; + ret = buffer_detach(&p->text); } else ret = a_strdup(str); diff --git a/widgets/tasklist.c b/widgets/tasklist.c index 2d774e4c..b5ee2b91 100644 --- a/widgets/tasklist.c +++ b/widgets/tasklist.c @@ -131,7 +131,6 @@ tasklist_draw(draw_context_t *ctx, int screen, draw_rectangle(ctx, area, 1.0, true, bg_color); break; } - p->text = NULL; markup_parser_data_delete(&p); if((image = draw_image_new(c->icon_path)))