optimize: remove g_snprintf()'s from label_fill[_descr]

svn path=/trunk/; revision=50681
This commit is contained in:
Jakub Zawadzki 2013-07-16 22:09:29 +00:00
parent cc82d38c8b
commit 13e07be25f
1 changed files with 57 additions and 16 deletions

View File

@ -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)
{
gint ret;
if (pos < ITEM_LABEL_LENGTH)
pos += g_strlcpy(label_str + pos, str, ITEM_LABEL_LENGTH - pos);
ret = g_snprintf(label_str, ITEM_LABEL_LENGTH, "%s: %s (%s)", hfinfo->name, text, descr);
if (ret >= 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);
}
return ret;
return pos;
}
static int
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", hfinfo->name, text);
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", hfinfo->name, text);
label_mark_truncated(label_str, name_pos);
}
return ret;
return pos;
}
static gsize
label_fill_descr(char *label_str, const header_field_info *hfinfo, const char *text, const char *descr)
{
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, ")");
if (pos >= ITEM_LABEL_LENGTH) {
/* Uh oh, we don't have enough room. Tell the user that the field is truncated. */
label_mark_truncated(label_str, name_pos);
}
return pos;
}
void