Move wmem string utility functions to wsutil

This commit is contained in:
João Valverde 2021-12-18 23:22:27 +00:00
parent 3319d994b5
commit f75b79a59d
26 changed files with 352 additions and 355 deletions

View File

@ -14,6 +14,7 @@
#include <epan/packet.h>
#include <epan/oids.h>
#include <epan/asn1.h>
#include <epan/strutil.h>
#include "packet-ber.h"
#include "packet-x509af.h"

View File

@ -14,6 +14,7 @@
#include <epan/packet.h>
#include <epan/oids.h>
#include <epan/asn1.h>
#include <epan/strutil.h>
#include "packet-ber.h"
#include "packet-p1.h"

View File

@ -17,6 +17,7 @@
#include <epan/packet.h>
#include <epan/to_str.h>
#include <epan/strutil.h>
#include <epan/reassemble.h>
#include <epan/expert.h>
#include <epan/proto_data.h>

View File

@ -20,6 +20,7 @@
#include <epan/conversation.h>
#include <epan/prefs.h>
#include <epan/expert.h>
#include <epan/strutil.h>
#include "proto_data.h"
#ifdef HAVE_ZLIB

View File

@ -24,6 +24,7 @@
#include <epan/proto_data.h>
#include <epan/unit_strings.h>
#include <epan/reassemble.h>
#include <epan/strutil.h>
#include "packet-bluetooth.h"
#include "packet-btatt.h"

View File

@ -49,6 +49,7 @@
#include <epan/sctpppids.h>
#include <epan/show_exception.h>
#include <epan/to_str.h>
#include <epan/strutil.h>
#include <epan/afn.h>
#include <wsutil/filesystem.h>
#include <wsutil/report_message.h>

View File

@ -58,6 +58,7 @@
#include <epan/packet.h>
#include <epan/expert.h>
#include <epan/prefs.h>
#include <epan/strutil.h>
#include <epan/proto_data.h>
#include <epan/dissectors/packet-http2.h>

View File

@ -13,6 +13,7 @@
#include <epan/packet.h>
#include <epan/expert.h>
#include <epan/strutil.h>
void proto_reg_handoff_hicp(void);
void proto_register_hicp(void);

View File

@ -14,6 +14,7 @@
#include <epan/packet.h>
#include <epan/expert.h>
#include <epan/strutil.h>
#include <wsutil/pint.h>
#include <wsutil/strtoi.h>

View File

@ -30,6 +30,7 @@
#include <epan/prefs.h>
#include <epan/conversation.h>
#include <epan/tap.h>
#include <epan/strutil.h>
#include <epan/rtd_table.h>
#include <epan/expert.h>
#include "packet-mgcp.h"

View File

@ -13,6 +13,7 @@
#include <epan/aftypes.h>
#include <epan/etypes.h>
#include <epan/ipproto.h>
#include <epan/strutil.h>
#include "packet-netlink.h"
void proto_register_netlink_sock_diag(void);

View File

@ -12,6 +12,7 @@
#include <epan/packet.h>
#include <epan/conversation.h>
#include <epan/strutil.h>
#define PMPROXY_PORT 44322
/* length of "pmproxy-server 1\n" and also "pmproxy-client 1\n" */

View File

@ -24,6 +24,7 @@
#include <epan/expert.h>
#include <epan/rtp_pt.h>
#include <epan/addr_resolv.h>
#include <epan/strutil.h>
/* For setting up RTP/RTCP dissectors based on the RTPproxy's answers */
#include "packet-rtp.h"

View File

@ -50,6 +50,7 @@
#include <epan/addr_resolv.h>
#include <epan/proto_data.h>
#include <epan/reassemble.h>
#include <epan/strutil.h>
#include "zlib.h"
void proto_register_rtps(void);

View File

@ -54,6 +54,7 @@
#include "packet-tcp.h"
#include <epan/prefs.h>
#include <epan/to_str.h>
#include <epan/strutil.h>
#include <epan/reassemble.h>
#include <epan/expert.h>
#include <epan/crc16-tvb.h>

View File

@ -143,6 +143,7 @@
#include <epan/prefs.h>
#include <epan/expert.h>
#include <epan/proto_data.h>
#include <epan/strutil.h>
#include <wsutil/epochs.h>

View File

@ -13,6 +13,7 @@
#include <epan/packet.h>
#include <epan/expert.h>
#include <epan/strutil.h>
#include <wsutil/strtoi.h>
#define TSDNS_PORT 41144 /* Not IANA registered */

View File

@ -22,6 +22,7 @@
#include <epan/packet.h>
#include <epan/oids.h>
#include <epan/asn1.h>
#include <epan/strutil.h>
#include "packet-ber.h"
#include "packet-x509af.h"
@ -125,7 +126,7 @@ static int hf_x509af_q = -1; /* INTEGER */
static int hf_x509af_g = -1; /* INTEGER */
/*--- End of included file: packet-x509af-hf.c ---*/
#line 41 "./asn1/x509af/packet-x509af-template.c"
#line 42 "./asn1/x509af/packet-x509af-template.c"
/* Initialize the subtree pointers */
static gint ett_pkix_crl = -1;
@ -166,7 +167,7 @@ static gint ett_x509af_SET_OF_AttributeType = -1;
static gint ett_x509af_DSS_Params = -1;
/*--- End of included file: packet-x509af-ett.c ---*/
#line 45 "./asn1/x509af/packet-x509af-template.c"
#line 46 "./asn1/x509af/packet-x509af-template.c"
static const char *algorithm_id = NULL;
static void
x509af_export_publickey(tvbuff_t *tvb, asn1_ctx_t *actx, int offset, int len);
@ -957,7 +958,7 @@ static int dissect_Userid_PDU(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_t
/*--- End of included file: packet-x509af-fn.c ---*/
#line 49 "./asn1/x509af/packet-x509af-template.c"
#line 50 "./asn1/x509af/packet-x509af-template.c"
/* Exports the SubjectPublicKeyInfo structure as gnutls_datum_t.
* actx->private_data is assumed to be a gnutls_datum_t pointer which will be
@ -1321,7 +1322,7 @@ void proto_register_x509af(void) {
"INTEGER", HFILL }},
/*--- End of included file: packet-x509af-hfarr.c ---*/
#line 108 "./asn1/x509af/packet-x509af-template.c"
#line 109 "./asn1/x509af/packet-x509af-template.c"
};
/* List of subtrees */
@ -1364,7 +1365,7 @@ void proto_register_x509af(void) {
&ett_x509af_DSS_Params,
/*--- End of included file: packet-x509af-ettarr.c ---*/
#line 114 "./asn1/x509af/packet-x509af-template.c"
#line 115 "./asn1/x509af/packet-x509af-template.c"
};
/* Register protocol */
@ -1409,7 +1410,7 @@ void proto_reg_handoff_x509af(void) {
/*--- End of included file: packet-x509af-dis-tab.c ---*/
#line 143 "./asn1/x509af/packet-x509af-template.c"
#line 144 "./asn1/x509af/packet-x509af-template.c"
/*XXX these should really go to a better place but since
I have not that ITU standard, I'll put it here for the time

View File

@ -22,6 +22,7 @@
#include <epan/packet.h>
#include <epan/oids.h>
#include <epan/asn1.h>
#include <epan/strutil.h>
#include "packet-ber.h"
#include "packet-p1.h"
@ -207,7 +208,7 @@ static int hf_x509sat_T_bitNamedDays_friday = -1;
static int hf_x509sat_T_bitNamedDays_saturday = -1;
/*--- End of included file: packet-x509sat-hf.c ---*/
#line 33 "./asn1/x509sat/packet-x509sat-template.c"
#line 34 "./asn1/x509sat/packet-x509sat-template.c"
/* Initialize the subtree pointers */
@ -262,7 +263,7 @@ static gint ett_x509sat_T_between = -1;
static gint ett_x509sat_LocaleContextSyntax = -1;
/*--- End of included file: packet-x509sat-ett.c ---*/
#line 36 "./asn1/x509sat/packet-x509sat-template.c"
#line 37 "./asn1/x509sat/packet-x509sat-template.c"
/*--- Included file: packet-x509sat-fn.c ---*/
@ -1906,7 +1907,7 @@ static int dissect_GUID_PDU(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tre
/*--- End of included file: packet-x509sat-fn.c ---*/
#line 38 "./asn1/x509sat/packet-x509sat-template.c"
#line 39 "./asn1/x509sat/packet-x509sat-template.c"
/*--- proto_register_x509sat ----------------------------------------------*/
@ -2575,7 +2576,7 @@ void proto_register_x509sat(void) {
NULL, HFILL }},
/*--- End of included file: packet-x509sat-hfarr.c ---*/
#line 46 "./asn1/x509sat/packet-x509sat-template.c"
#line 47 "./asn1/x509sat/packet-x509sat-template.c"
};
/* List of subtrees */
@ -2632,7 +2633,7 @@ void proto_register_x509sat(void) {
&ett_x509sat_LocaleContextSyntax,
/*--- End of included file: packet-x509sat-ettarr.c ---*/
#line 51 "./asn1/x509sat/packet-x509sat-template.c"
#line 52 "./asn1/x509sat/packet-x509sat-template.c"
};
/* Register protocol */
@ -2685,7 +2686,7 @@ void proto_register_x509sat(void) {
register_ber_syntax_dissector("X121Address", proto_x509sat, dissect_X121Address_PDU);
/*--- End of included file: packet-x509sat-syn-reg.c ---*/
#line 61 "./asn1/x509sat/packet-x509sat-template.c"
#line 62 "./asn1/x509sat/packet-x509sat-template.c"
}
@ -2878,7 +2879,7 @@ void proto_reg_handoff_x509sat(void) {
/*--- End of included file: packet-x509sat-dis-tab.c ---*/
#line 68 "./asn1/x509sat/packet-x509sat-template.c"
#line 69 "./asn1/x509sat/packet-x509sat-template.c"
/* OBJECT CLASSES */

View File

@ -18,6 +18,7 @@
#include <wsutil/report_message.h>
#include <epan/strutil.h>
#include <epan/wmem_scopes.h>
#include "uat.h"
#include "prefs.h"

View File

@ -16,6 +16,193 @@
#include <wsutil/to_str.h>
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;
}
int
ws_xton(char ch)
{

View File

@ -18,6 +18,58 @@
extern "C" {
#endif /* __cplusplus */
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);
/** Convert all upper-case ASCII letters to their ASCII lower-case
* equivalents, in place, with a simple non-locale-dependent
* ASCII mapping (A-Z -> a-z).

View File

@ -45,6 +45,83 @@ static void test_escape_string(void)
wmem_free(NULL, buf);
}
static void test_strconcat(void)
{
wmem_allocator_t *allocator;
char *new_str;
allocator = wmem_allocator_new(WMEM_ALLOCATOR_BLOCK);
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");
new_str = wmem_strconcat(allocator, "", "", "ABCDEF", "", "GH", NULL);
g_assert_cmpstr(new_str, ==, "ABCDEFGH");
wmem_destroy_allocator(allocator);
}
static void test_strsplit(void)
{
wmem_allocator_t *allocator;
char **split_str;
allocator = wmem_allocator_new(WMEM_ALLOCATOR_BLOCK);
split_str = wmem_strsplit(allocator, "A-C", "-", 2);
g_assert_cmpstr(split_str[0], ==, "A");
g_assert_cmpstr(split_str[1], ==, "C");
g_assert_null(split_str[2]);
split_str = wmem_strsplit(allocator, "A-C", "-", 0);
g_assert_cmpstr(split_str[0], ==, "A");
g_assert_cmpstr(split_str[1], ==, "C");
g_assert_null(split_str[2]);
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_null(split_str[9]);
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_null(split_str[5]);
split_str = wmem_strsplit(allocator, "", "-", -1);
g_assert_null(split_str[0]);
wmem_destroy_allocator(allocator);
}
static void test_str_ascii(void)
{
wmem_allocator_t *allocator;
const char *orig_str;
char *new_str;
allocator = wmem_allocator_new(WMEM_ALLOCATOR_BLOCK);
orig_str = "TeStAsCiIsTrDoWn";
new_str = wmem_ascii_strdown(allocator, orig_str, -1);
g_assert_cmpstr(new_str, ==, "testasciistrdown");
wmem_destroy_allocator(allocator);
}
#include "to_str.h"
static void test_word_to_hex(void)
@ -542,6 +619,9 @@ int main(int argc, char **argv)
g_test_add_func("/str_util/format_size", test_format_size);
g_test_add_func("/str_util/escape_string", test_escape_string);
g_test_add_func("/str_util/strconcat", test_strconcat);
g_test_add_func("/str_util/strsplit", test_strsplit);
g_test_add_func("/str_util/str_ascii", test_str_ascii);
g_test_add_func("/to_str/word_to_hex", test_word_to_hex);
g_test_add_func("/to_str/bytes_to_str", test_bytes_to_str);

View File

@ -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
*

View File

@ -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);
/** @}
* @} */

View File

@ -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");
@ -615,17 +571,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, 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);
@ -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;
@ -661,17 +595,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, 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);
@ -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;
@ -704,14 +616,6 @@ wmem_test_stringperf(void)
g_test_minimized_result(utime_ms + stime_ms,
"wmem_strdup_printf(allocator) 2 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, 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);
@ -720,14 +624,6 @@ wmem_test_stringperf(void)
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);
}