diff options
author | Jakub Zawadzki <darkjames-ws@darkjames.pl> | 2013-07-16 22:09:29 +0000 |
---|---|---|
committer | Jakub Zawadzki <darkjames-ws@darkjames.pl> | 2013-07-16 22:09:29 +0000 |
commit | 13e07be25f820c0ef4225c445550db378b831d64 (patch) | |
tree | 56b2dac8ce4eeaffe725979586fce837ab0aa168 | |
parent | cc82d38c8b7c03d729f177274dbe33d67c867e0b (diff) |
optimize: remove g_snprintf()'s from label_fill[_descr]
svn path=/trunk/; revision=50681
-rw-r--r-- | epan/proto.c | 69 |
1 files changed, 55 insertions, 14 deletions
diff --git a/epan/proto.c b/epan/proto.c index b77d3a3e33..f10c016a25 100644 --- a/epan/proto.c +++ b/epan/proto.c @@ -5087,32 +5087,73 @@ proto_register_subtree_array(gint *const *indices, const int num_indices) } } -static int -label_fill_descr(char *label_str, const header_field_info *hfinfo, const char *text, const char *descr) +static inline gsize +label_concat(char *label_str, gsize pos, const char *str) +{ + if (pos < ITEM_LABEL_LENGTH) + pos += g_strlcpy(label_str + pos, str, ITEM_LABEL_LENGTH - pos); + + return pos; +} + +static void +label_mark_truncated(char *label_str, gsize name_pos) +{ + static const char trunc_str[] = " [truncated]"; + const size_t trunc_len = sizeof(trunc_str)-1; + + /* ..... field_name: dataaaaaaaaaaaaa + * | + * ^^^^^ name_pos + * + * ..... field_name [truncated]: dataaaaaaaaaaaaa */ + + if (name_pos < ITEM_LABEL_LENGTH - trunc_len) { + memmove(label_str + name_pos + trunc_len, label_str + name_pos, ITEM_LABEL_LENGTH - name_pos - trunc_len); + memcpy(label_str + name_pos, trunc_str, trunc_len); + label_str[ITEM_LABEL_LENGTH-1] = '\0'; + + } else if (name_pos < ITEM_LABEL_LENGTH) + g_strlcpy(label_str + name_pos, trunc_str, ITEM_LABEL_LENGTH - name_pos); +} + +static gsize +label_fill(char *label_str, const header_field_info *hfinfo, const char *text) { - gint ret; + gsize name_pos, pos = 0; - ret = g_snprintf(label_str, ITEM_LABEL_LENGTH, "%s: %s (%s)", hfinfo->name, text, descr); - if (ret >= ITEM_LABEL_LENGTH) { + /* "%s: %s", hfinfo->name, text */ + name_pos = pos = label_concat(label_str, pos, hfinfo->name); + pos = label_concat(label_str, pos, ": "); + pos = label_concat(label_str, pos, text ? text : "(null)"); + + if (pos >= ITEM_LABEL_LENGTH) { /* Uh oh, we don't have enough room. Tell the user that the field is truncated. */ - ret = g_snprintf(label_str, ITEM_LABEL_LENGTH, "%s [truncated]: %s (%s)", hfinfo->name, text, descr); + label_mark_truncated(label_str, name_pos); } - return ret; + return pos; } -static int -label_fill(char *label_str, const header_field_info *hfinfo, const char *text) +static gsize +label_fill_descr(char *label_str, const header_field_info *hfinfo, const char *text, const char *descr) { - gint ret; + gsize name_pos, pos = 0; + + /* "%s: %s (%s)", hfinfo->name, text, descr */ + name_pos = pos = label_concat(label_str, pos, hfinfo->name); + pos = label_concat(label_str, pos, ": "); + pos = label_concat(label_str, pos, text ? text : "(null)"); + pos = label_concat(label_str, pos, " ("); + pos = label_concat(label_str, pos, descr ? descr : "(null)"); + pos = label_concat(label_str, pos, ")"); - ret = g_snprintf(label_str, ITEM_LABEL_LENGTH, "%s: %s", hfinfo->name, text); - if (ret >= ITEM_LABEL_LENGTH) { + if (pos >= ITEM_LABEL_LENGTH) { /* Uh oh, we don't have enough room. Tell the user that the field is truncated. */ - ret = g_snprintf(label_str, ITEM_LABEL_LENGTH, "%s [truncated]: %s", hfinfo->name, text); + label_mark_truncated(label_str, name_pos); } - return ret; + return pos; } void |