diff options
author | João Valverde <j@v6e.pt> | 2021-12-18 23:22:27 +0000 |
---|---|---|
committer | João Valverde <j@v6e.pt> | 2021-12-19 10:47:50 +0000 |
commit | f75b79a59d661c4eadcffa28d9101e45cc5e649e (patch) | |
tree | 22134b64ab7b772a0eaf0f1b304c553c6ca44675 /wsutil/wmem | |
parent | 3319d994b5e56ee03c6010ad2b5f8765ffb77aa0 (diff) |
Move wmem string utility functions to wsutil
Diffstat (limited to 'wsutil/wmem')
-rw-r--r-- | wsutil/wmem/wmem_strutl.c | 186 | ||||
-rw-r--r-- | wsutil/wmem/wmem_strutl.h | 52 | ||||
-rw-r--r-- | wsutil/wmem/wmem_test.c | 104 |
3 files changed, 0 insertions, 342 deletions
diff --git a/wsutil/wmem/wmem_strutl.c b/wsutil/wmem/wmem_strutl.c index dfc7271ef7..74d908cd06 100644 --- a/wsutil/wmem/wmem_strutl.c +++ b/wsutil/wmem/wmem_strutl.c @@ -109,192 +109,6 @@ wmem_strdup_vprintf(wmem_allocator_t *allocator, const char *fmt, va_list ap) return new_buf; } -gchar * -wmem_strconcat(wmem_allocator_t *allocator, const gchar *first, ...) -{ - gsize len; - va_list args; - gchar *s; - gchar *concat; - gchar *ptr; - - if (!first) - return NULL; - - len = 1 + strlen(first); - va_start(args, first); - while ((s = va_arg(args, gchar*))) { - len += strlen(s); - } - va_end(args); - - ptr = concat = (gchar *)wmem_alloc(allocator, len); - - ptr = g_stpcpy(ptr, first); - va_start(args, first); - while ((s = va_arg(args, gchar*))) { - ptr = g_stpcpy(ptr, s); - } - va_end(args); - - return concat; -} - -gchar * -wmem_strjoin(wmem_allocator_t *allocator, - const gchar *separator, const gchar *first, ...) -{ - gsize len; - va_list args; - gsize separator_len; - gchar *s; - gchar *concat; - gchar *ptr; - - if (!first) - return NULL; - - if (separator == NULL) { - separator = ""; - } - - separator_len = strlen (separator); - - len = 1 + strlen(first); /* + 1 for null byte */ - va_start(args, first); - while ((s = va_arg(args, gchar*))) { - len += (separator_len + strlen(s)); - } - va_end(args); - - ptr = concat = (gchar *)wmem_alloc(allocator, len); - ptr = g_stpcpy(ptr, first); - va_start(args, first); - while ((s = va_arg(args, gchar*))) { - ptr = g_stpcpy(ptr, separator); - ptr = g_stpcpy(ptr, s); - } - va_end(args); - - return concat; - -} - -gchar * -wmem_strjoinv(wmem_allocator_t *allocator, - const gchar *separator, gchar **str_array) -{ - gchar *string = NULL; - - if (!str_array) - return NULL; - - if (separator == NULL) { - separator = ""; - } - - if (str_array[0]) { - gint i; - gchar *ptr; - gsize len, separator_len; - - separator_len = strlen(separator); - - /* Get first part of length. Plus one for null byte. */ - len = 1 + strlen(str_array[0]); - /* Get the full length, including the separators. */ - for (i = 1; str_array[i] != NULL; i++) { - len += separator_len; - len += strlen(str_array[i]); - } - - /* Allocate and build the string. */ - string = (gchar *)wmem_alloc(allocator, len); - ptr = g_stpcpy(string, str_array[0]); - for (i = 1; str_array[i] != NULL; i++) { - ptr = g_stpcpy(ptr, separator); - ptr = g_stpcpy(ptr, str_array[i]); - } - } - - return string; - -} - -gchar ** -wmem_strsplit(wmem_allocator_t *allocator, const gchar *src, - const gchar *delimiter, int max_tokens) -{ - gchar *splitted; - gchar *s; - guint tokens; - guint sep_len; - guint i; - gchar **vec; - - if (!src || !delimiter || !delimiter[0]) - return NULL; - - /* An empty string results in an empty vector. */ - if (!src[0]) { - vec = wmem_new0(allocator, gchar *); - return vec; - } - - splitted = wmem_strdup(allocator, src); - sep_len = (guint)strlen(delimiter); - - if (max_tokens < 1) - max_tokens = INT_MAX; - - /* Calculate the number of fields. */ - s = splitted; - tokens = 1; - while (tokens < (guint)max_tokens && (s = strstr(s, delimiter))) { - s += sep_len; - tokens++; - } - - vec = wmem_alloc_array(allocator, gchar *, tokens + 1); - - /* Populate the array of string tokens. */ - s = splitted; - vec[0] = s; - tokens = 1; - while (tokens < (guint)max_tokens && (s = strstr(s, delimiter))) { - for (i = 0; i < sep_len; i++) - s[i] = '\0'; - s += sep_len; - vec[tokens] = s; - tokens++; - - } - - vec[tokens] = NULL; - - return vec; -} - -/* - * wmem_ascii_strdown: - * based on g_ascii_strdown. - */ -gchar* -wmem_ascii_strdown(wmem_allocator_t *allocator, const gchar *str, gssize len) -{ - gchar *result, *s; - - g_return_val_if_fail (str != NULL, NULL); - - if (len < 0) - len = strlen (str); - - result = wmem_strndup(allocator, str, len); - for (s = result; *s; s++) - *s = g_ascii_tolower (*s); - - return result; -} /* * Editor modelines - https://www.wireshark.org/tools/modelines.html * diff --git a/wsutil/wmem/wmem_strutl.h b/wsutil/wmem/wmem_strutl.h index dceeb32bdc..9e476eb901 100644 --- a/wsutil/wmem/wmem_strutl.h +++ b/wsutil/wmem/wmem_strutl.h @@ -50,58 +50,6 @@ gchar * wmem_strdup_vprintf(wmem_allocator_t *allocator, const gchar *fmt, va_list ap) G_GNUC_MALLOC; -WS_DLL_PUBLIC -gchar * -wmem_strconcat(wmem_allocator_t *allocator, const gchar *first, ...) -G_GNUC_MALLOC G_GNUC_NULL_TERMINATED; - -WS_DLL_PUBLIC -gchar * -wmem_strjoin(wmem_allocator_t *allocator, - const gchar *separator, const gchar *first, ...) -G_GNUC_MALLOC G_GNUC_NULL_TERMINATED; - -WS_DLL_PUBLIC -gchar * -wmem_strjoinv(wmem_allocator_t *allocator, - const gchar *separator, gchar **str_array) -G_GNUC_MALLOC; - -/** - * Splits a string into a maximum of max_tokens pieces, using the given - * delimiter. If max_tokens is reached, the remainder of string is appended - * to the last token. Successive tokens are not folded and will instead result - * in an empty string as element. - * - * If src or delimiter are NULL, or if delimiter is empty, this will return - * NULL. - * - * Do not use with a NULL allocator, use g_strsplit instead. - */ -WS_DLL_PUBLIC -gchar ** -wmem_strsplit(wmem_allocator_t *allocator, const gchar *src, - const gchar *delimiter, int max_tokens); - - -/** - * wmem_ascii_strdown: - * Based on g_ascii_strdown - * @param allocator An enumeration of the different types of available allocators. - * @param str a string. - * @param len length of str in bytes, or -1 if str is nul-terminated. - * - * Converts all upper case ASCII letters to lower case ASCII letters. - * - * Return value: a newly-allocated string, with all the upper case - * characters in str converted to lower case, with - * semantics that exactly match g_ascii_tolower(). (Note - * that this is unlike the old g_strdown(), which modified - * the string in place.) - **/ -WS_DLL_PUBLIC -gchar* -wmem_ascii_strdown(wmem_allocator_t *allocator, const gchar *str, gssize len); /** @} * @} */ diff --git a/wsutil/wmem/wmem_test.c b/wsutil/wmem/wmem_test.c index 0bdb2be571..9c5605760e 100644 --- a/wsutil/wmem/wmem_test.c +++ b/wsutil/wmem/wmem_test.c @@ -404,7 +404,6 @@ wmem_test_strutls(void) wmem_allocator_t *allocator; const char *orig_str; char *new_str; - char **split_str; allocator = wmem_allocator_new(WMEM_ALLOCATOR_STRICT); @@ -429,49 +428,6 @@ wmem_test_strutls(void) g_assert_cmpstr(new_str, ==, STRING_80); wmem_strict_check_canaries(allocator); - new_str = wmem_strconcat(allocator, "ABC", NULL); - g_assert_cmpstr(new_str, ==, "ABC"); - new_str = wmem_strconcat(allocator, "ABC", "DEF", NULL); - g_assert_cmpstr(new_str, ==, "ABCDEF"); - wmem_strict_check_canaries(allocator); - new_str = wmem_strconcat(allocator, "", "", "ABCDEF", "", "GH", NULL); - g_assert_cmpstr(new_str, ==, "ABCDEFGH"); - wmem_strict_check_canaries(allocator); - - split_str = wmem_strsplit(allocator, "A-C", "-", 2); - g_assert_cmpstr(split_str[0], ==, "A"); - g_assert_cmpstr(split_str[1], ==, "C"); - g_assert_true(split_str[2] == NULL); - split_str = wmem_strsplit(allocator, "A-C", "-", 0); - g_assert_cmpstr(split_str[0], ==, "A"); - g_assert_cmpstr(split_str[1], ==, "C"); - g_assert_true(split_str[2] == NULL); - split_str = wmem_strsplit(allocator, "--aslkf-asio--asfj-as--", "-", 10); - g_assert_cmpstr(split_str[0], ==, ""); - g_assert_cmpstr(split_str[1], ==, ""); - g_assert_cmpstr(split_str[2], ==, "aslkf"); - g_assert_cmpstr(split_str[3], ==, "asio"); - g_assert_cmpstr(split_str[4], ==, ""); - g_assert_cmpstr(split_str[5], ==, "asfj"); - g_assert_cmpstr(split_str[6], ==, "as"); - g_assert_cmpstr(split_str[7], ==, ""); - g_assert_cmpstr(split_str[8], ==, ""); - g_assert_true(split_str[9] == NULL); - split_str = wmem_strsplit(allocator, "--aslkf-asio--asfj-as--", "-", 5); - g_assert_cmpstr(split_str[0], ==, ""); - g_assert_cmpstr(split_str[1], ==, ""); - g_assert_cmpstr(split_str[2], ==, "aslkf"); - g_assert_cmpstr(split_str[3], ==, "asio"); - g_assert_cmpstr(split_str[4], ==, "-asfj-as--"); - g_assert_true(split_str[5] == NULL); - split_str = wmem_strsplit(allocator, "", "-", -1); - g_assert_true(split_str[0] == NULL); - wmem_strict_check_canaries(allocator); - - orig_str = "TeStAsCiIsTrDoWn"; - new_str = wmem_ascii_strdown(allocator, orig_str, -1); - g_assert_cmpstr(new_str, ==, "testasciistrdown"); - orig_str = "Short String"; new_str = wmem_strdup_printf(allocator, "TEST %s", orig_str); g_assert_cmpstr(new_str, ==, "TEST Short String"); @@ -617,17 +573,6 @@ wmem_test_stringperf(void) RESOURCE_USAGE_START; for (i = 0; i < LOOP_COUNT; i++) { - str_ptr[i] = g_strconcat(s_val, s_val, NULL); - } - RESOURCE_USAGE_END; - g_test_minimized_result(utime_ms + stime_ms, - "g_strconcat 2 strings: u %.3f ms s %.3f ms", utime_ms, stime_ms); - for (i = 0; i < LOOP_COUNT; i++) { - g_free(str_ptr[i]); - } - - RESOURCE_USAGE_START; - for (i = 0; i < LOOP_COUNT; i++) { str_ptr[i] = g_strdup_printf("%s%s%s%s%s", s_val, s_val, s_val, s_val, s_val); } RESOURCE_USAGE_END; @@ -637,17 +582,6 @@ wmem_test_stringperf(void) g_free(str_ptr[i]); } - RESOURCE_USAGE_START; - for (i = 0; i < LOOP_COUNT; i++) { - str_ptr[i] = g_strconcat(s_val, s_val, s_val, s_val, s_val, NULL); - } - RESOURCE_USAGE_END; - g_test_minimized_result(utime_ms + stime_ms, - "g_strconcat 5 strings: u %.3f ms s %.3f ms", utime_ms, stime_ms); - for (i = 0; i < LOOP_COUNT; i++) { - g_free(str_ptr[i]); - } - /* wmem strdup null allocator */ RESOURCE_USAGE_START; @@ -663,17 +597,6 @@ wmem_test_stringperf(void) RESOURCE_USAGE_START; for (i = 0; i < LOOP_COUNT; i++) { - str_ptr[i] = wmem_strconcat(NULL, s_val, s_val, NULL); - } - RESOURCE_USAGE_END; - g_test_minimized_result(utime_ms + stime_ms, - "wmem_strconcat(NULL) 2 strings: u %.3f ms s %.3f ms", utime_ms, stime_ms); - for (i = 0; i < LOOP_COUNT; i++) { - g_free(str_ptr[i]); - } - - RESOURCE_USAGE_START; - for (i = 0; i < LOOP_COUNT; i++) { str_ptr[i] = wmem_strdup_printf(NULL, "%s%s%s%s%s", s_val, s_val, s_val, s_val, s_val); } RESOURCE_USAGE_END; @@ -683,17 +606,6 @@ wmem_test_stringperf(void) g_free(str_ptr[i]); } - RESOURCE_USAGE_START; - for (i = 0; i < LOOP_COUNT; i++) { - str_ptr[i] = wmem_strconcat(NULL, s_val, s_val, s_val, s_val, s_val, NULL); - } - RESOURCE_USAGE_END; - g_test_minimized_result(utime_ms + stime_ms, - "wmem_strconcat(NULL) 5 strings: u %.3f ms s %.3f ms", utime_ms, stime_ms); - for (i = 0; i < LOOP_COUNT; i++) { - g_free(str_ptr[i]); - } - /* wmem strdup strict allocator */ RESOURCE_USAGE_START; @@ -706,28 +618,12 @@ wmem_test_stringperf(void) RESOURCE_USAGE_START; for (i = 0; i < LOOP_COUNT; i++) { - wmem_strconcat(allocator, s_val, s_val, NULL); - } - RESOURCE_USAGE_END; - g_test_minimized_result(utime_ms + stime_ms, - "wmem_strconcat(allocator) 2 strings: u %.3f ms s %.3f ms", utime_ms, stime_ms); - - RESOURCE_USAGE_START; - for (i = 0; i < LOOP_COUNT; i++) { wmem_strdup_printf(allocator, "%s%s%s%s%s", s_val, s_val, s_val, s_val, s_val); } RESOURCE_USAGE_END; g_test_minimized_result(utime_ms + stime_ms, "wmem_strdup_printf(allocator) 5 strings: u %.3f ms s %.3f ms", utime_ms, stime_ms); - RESOURCE_USAGE_START; - for (i = 0; i < LOOP_COUNT; i++) { - wmem_strconcat(allocator, s_val, s_val, s_val, s_val, s_val, NULL); - } - RESOURCE_USAGE_END; - g_test_minimized_result(utime_ms + stime_ms, - "wmem_strconcat(allocator) 5 strings: u %.3f ms s %.3f ms", utime_ms, stime_ms); - wmem_destroy_allocator(allocator); g_free(str_ptr); } |