diff options
author | etxrab <etxrab@f5534014-38df-0310-8fa8-9805f1628bb7> | 2007-10-26 05:42:12 +0000 |
---|---|---|
committer | etxrab <etxrab@f5534014-38df-0310-8fa8-9805f1628bb7> | 2007-10-26 05:42:12 +0000 |
commit | 9a758f1d5e73eac05ee23a1b02ba88cb805570f5 (patch) | |
tree | 4d67f48f3730a2b0a859650113866f141a786480 /epan/strutil.c | |
parent | f49fb83f338d42d04e8a6ca8527b207f89e3c130 (diff) |
Apply yet another set of the optimization patches:
Replace strncpy with g_strlcpy.
Add g_strlcat for GTK1 and don't use g_snprintf in GTK1 g_strlcpy
printf family is very slow.
git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@23273 f5534014-38df-0310-8fa8-9805f1628bb7
Diffstat (limited to 'epan/strutil.c')
-rw-r--r-- | epan/strutil.c | 70 |
1 files changed, 60 insertions, 10 deletions
diff --git a/epan/strutil.c b/epan/strutil.c index 3f1b04db86..05ab72bf94 100644 --- a/epan/strutil.c +++ b/epan/strutil.c @@ -952,19 +952,69 @@ convert_string_case(const char *string, gboolean case_insensitive) return out_string; } -/* g_strlcat() does not exist in GLib 1.2[.x] */ +/* g_strlcat(), g_strlcpy don't exist in GLib 1.2[.x] */ #if GLIB_MAJOR_VERSION < 2 gsize -g_strlcat(gchar *dst, gchar *src, gsize size) +g_strlcat(gchar *dest, gchar *src, gsize dest_size) { - gsize strl; - int strs; - strl=strlen(dst); - strs=strlen(src); - if(strl<size) - g_snprintf(dst+strl, size-strl, "%s", src); - dst[size-1]=0; - return strl+strs; + gchar *d = dest; + const gchar *s = src; + gsize bytes_left = dest_size; + gsize dlength; /* Logically, MIN (strlen (d), dest_size) */ + + /* Find the end of dst and adjust bytes left but don't go past end */ + while (*d != 0 && bytes_left-- != 0) + d++; + dlength = d - dest; + bytes_left = dest_size - dlength; + + if (bytes_left == 0) + return dlength + strlen (s); + + while (*s != 0) + { + if (bytes_left != 1) + { + *d++ = *s; + bytes_left--; + } + s++; + } + *d = 0; + + return dlength + (s - src); /* count does not include NUL */ +} + +/* --------------------------------- */ +gsize +g_strlcpy(gchar *dest, const gchar *src, gsize dest_size) +{ + gchar *d = dest; + const gchar *s = src; + gsize n = dest_size; + + /* Copy as many bytes as will fit */ + if (n != 0 && --n != 0) + do + { + gchar c = *s++; + + *d++ = c; + if (c == 0) + break; + } + while (--n != 0); + + /* If not enough room in dest, add NUL and traverse rest of src */ + if (n == 0) + { + if (dest_size != 0) + *d = 0; + while (*s++) + ; + } + + return s - src - 1; /* count does not include NUL */ } #endif |