From 7e750dcf97fd5e49774ede3ac7018d26b579f9fe Mon Sep 17 00:00:00 2001 From: Michael Jerris Date: Wed, 11 Feb 2009 16:16:52 +0000 Subject: [PATCH] Thu Jan 8 12:04:21 CST 2009 Pekka Pessi * sofia-sip/su_string.h: added Adding locale-independent, NULL-safe string comparison, matching and searching functions: - su_strcmp() - su_strncmp() - su_strcasecmp() - su_strncasecmp() - su_strmatch() - su_strnmatch() - su_casematch() - su_casenmatch() - su_strcasestr() - su_strnspn() - su_strncspn() - su_memspn() - su_memcspn() git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@11777 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- libs/sofia-sip/.update | 2 +- libs/sofia-sip/libsofia-sip-ua/su/Makefile.am | 7 +- .../libsofia-sip-ua/su/test_memmem.c | 146 +++++++++++++++--- 3 files changed, 128 insertions(+), 27 deletions(-) diff --git a/libs/sofia-sip/.update b/libs/sofia-sip/.update index 46695c387c..f3382220ab 100644 --- a/libs/sofia-sip/.update +++ b/libs/sofia-sip/.update @@ -1 +1 @@ -Wed Feb 11 10:15:25 CST 2009 +Wed Feb 11 10:16:40 CST 2009 diff --git a/libs/sofia-sip/libsofia-sip-ua/su/Makefile.am b/libs/sofia-sip/libsofia-sip-ua/su/Makefile.am index 2eb90fd79f..db3a331aea 100644 --- a/libs/sofia-sip/libsofia-sip-ua/su/Makefile.am +++ b/libs/sofia-sip/libsofia-sip-ua/su/Makefile.am @@ -54,7 +54,7 @@ nobase_include_sofia_HEADERS = \ sofia-sip/su_config.h sofia-sip/su_md5.h \ sofia-sip/su_uniqueid.h sofia-sip/su_bm.h \ sofia-sip/tstdef.h sofia-sip/su_os_nw.h \ - sofia-sip/string0.h \ + sofia-sip/su_string.h sofia-sip/string0.h \ $(OSXHEADERS) nobase_nodist_include_sofia_HEADERS = sofia-sip/su_configure.h @@ -73,11 +73,12 @@ libsu_la_SOURCES = \ su_os_nw.c \ su_taglist.c su_tag.c su_tag_io.c \ su_log.c su_global_log.c su_default_log.c su_module_debug.h \ - su_md5.c su_uniqueid.c su_bm.c smoothsort.c string0.c $(OSXSOURCES) + su_md5.c su_uniqueid.c su_bm.c smoothsort.c su_string.c string0.c \ + $(OSXSOURCES) EXTRA_libsu_la_SOURCES = \ - memmem.c strtoull.c strcasestr.c \ + memmem.c strtoull.c \ memspn.c memcspn.c memccpy.c \ inet_ntop.c inet_pton.c poll.c getopt.c \ su_tag_ref.c diff --git a/libs/sofia-sip/libsofia-sip-ua/su/test_memmem.c b/libs/sofia-sip/libsofia-sip-ua/su/test_memmem.c index ca30b6cac8..9af01ecabc 100644 --- a/libs/sofia-sip/libsofia-sip-ua/su/test_memmem.c +++ b/libs/sofia-sip/libsofia-sip-ua/su/test_memmem.c @@ -22,11 +22,11 @@ * */ -/**@ingroup test_memmem +/**@ingroup test_su_string * * @CFILE test_memmem.c * - * Torture tests for memmem() and strcasestr(). + * Torture tests for various string utility functions. * * @author Pekka Pessi * @@ -35,6 +35,8 @@ #include "config.h" +#include + #include #include #include @@ -45,9 +47,6 @@ void *memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen); #endif -#if !HAVE_STRCASESTR -char *strcasestr(const char *haystack, const char *needle); -#endif #include @@ -116,38 +115,38 @@ int test_strcasestr(void) "A case-folding string searching test consisting of a Long String"; char const *s; - s = strcasestr(hs, "sting"); + s = su_strcasestr(hs, "sting"); TEST_S(s, hs + 42); - s = strcasestr(hs, "String"); + s = su_strcasestr(hs, "String"); TEST_S(s, hs + 15); - s = strcasestr(hs, "S"); + s = su_strcasestr(hs, "S"); TEST_S(s, hs + 4); - s = strcasestr(hs, "L"); + s = su_strcasestr(hs, "L"); TEST_S(s, hs + 9); - s = strcasestr(hs, "trings"); + s = su_strcasestr(hs, "trings"); TEST_1(s == NULL); - s = strcasestr(hs, "String"); + s = su_strcasestr(hs, "String"); TEST_S(s, hs + 15); - s = strcasestr(hs, "StRiNg"); + s = su_strcasestr(hs, "StRiNg"); TEST_S(s, hs + 15); - s = strcasestr(hs, "OnG"); + s = su_strcasestr(hs, "OnG"); TEST_S(s, hs + 54); /* Special cases */ - TEST_1(strcasestr(hs, "") == hs); - TEST_1(strcasestr("", "ong") == NULL); - TEST_1(strcasestr("", "OnG") == NULL); - TEST_1(strcasestr("ong", hs) == NULL); - TEST_1(strcasestr("OnG", hs) == NULL); - TEST_1(strcasestr(hs, "Z") == NULL); - TEST_1(strcasestr(hs, "z") == NULL); + TEST_1(su_strcasestr(hs, "") == hs); + TEST_1(su_strcasestr("", "ong") == NULL); + TEST_1(su_strcasestr("", "OnG") == NULL); + TEST_1(su_strcasestr("ong", hs) == NULL); + TEST_1(su_strcasestr("OnG", hs) == NULL); + TEST_1(su_strcasestr(hs, "Z") == NULL); + TEST_1(su_strcasestr(hs, "z") == NULL); } { @@ -219,16 +218,114 @@ char const Needle[] = char const *s; - s = strcasestr(hs, needle); + s = su_strcasestr(hs, needle); TEST_S(s, hs + 1920); - s = strcasestr(hs, Needle); + s = su_strcasestr(hs, Needle); TEST_S(s, hs + 1920); } END(); } +static int test_casematch(void) +{ + BEGIN(); + + TEST_1(!su_casematch(NULL, "")); + TEST_1(su_casematch(NULL, NULL)); + TEST_1(!su_casematch("", NULL)); + + TEST_1(!su_casenmatch(NULL, "", 1)); + TEST_1(su_casenmatch(NULL, NULL, 1)); + TEST_1(!su_casenmatch("", NULL, 1)); + + TEST_1(su_casenmatch(NULL, "", 0)); + TEST_1(su_casenmatch(NULL, NULL, 0)); + TEST_1(su_casenmatch("", NULL, 0)); + + TEST_1(su_casenmatch("foo", "foo", 3)); + TEST_1(su_casenmatch("FOO", "foo", 3)); + TEST_1(su_casenmatch("foo", "FOO", 3)); + + TEST_1(su_casenmatch("foo", "foo", 4)); + TEST_1(su_casenmatch("FOO", "foo", 4)); + TEST_1(su_casenmatch("foo", "FOO", 4)); + + TEST_1(su_casematch("foo", "foo")); + TEST_1(su_casematch("FOO", "foo")); + TEST_1(su_casematch("foo", "FOO")); + + TEST_1(!su_casematch("foo_", "foo")); + TEST_1(!su_casematch("FOO_", "foo")); + TEST_1(!su_casematch("foo_", "FOO")); + + TEST_1(su_casenmatch("foo\0X", "foo\0z", 5)); + TEST_1(su_casenmatch("FOO\0X", "foo\0z", 5)); + TEST_1(su_casenmatch("foo\0X", "FOO\0z", 5)); + + END(); +} + +static int test_strmatch(void) +{ + BEGIN(); + + TEST_1(!su_strmatch(NULL, "")); + TEST_1(su_strmatch(NULL, NULL)); + TEST_1(!su_strmatch("", NULL)); + + TEST_1(!su_strnmatch(NULL, "", 1)); + TEST_1(su_strnmatch(NULL, NULL, 1)); + TEST_1(!su_strnmatch("", NULL, 1)); + + TEST_1(su_strnmatch(NULL, "", 0)); + TEST_1(su_strnmatch(NULL, NULL, 0)); + TEST_1(su_strnmatch("", NULL, 0)); + + TEST_1(su_strnmatch("foo", "foo", 3)); + TEST_1(!su_strnmatch("FOO", "foo", 3)); + TEST_1(!su_strnmatch("foo", "FOO", 3)); + + TEST_1(su_strnmatch("foo", "foo", 4)); + TEST_1(!su_strnmatch("FOO", "foo", 4)); + TEST_1(!su_strnmatch("foo", "FOO", 4)); + + TEST_1(su_strmatch("foo", "foo")); + TEST_1(!su_strmatch("FOO", "foo")); + TEST_1(!su_strmatch("foo", "FOO")); + + TEST_1(!su_strmatch("foo_", "foo")); + + TEST_1(su_strnmatch("foo\0X", "foo\0z", 5)); + TEST_1(!su_strnmatch("FOO\0X", "foo\0z", 5)); + TEST_1(!su_strnmatch("foo\0X", "FOO\0z", 5)); + + END(); +} + +static int test_strnspn(void) +{ + BEGIN(); + + TEST(su_strnspn("foobar", 3, ""), 0); + TEST(su_strnspn("foobar", 5, "fo"), 3); + TEST(su_strnspn("foobar", 5, "o"), 0); + TEST(su_strnspn("foobar", 5, "f"), 1); + TEST(su_strnspn("", 5, "fo"), 0); + TEST(su_strnspn("foobarf", 5, "fob"), 4); + + TEST(su_strncspn("foobar", 3, ""), 3); + TEST(su_strncspn("foobar", 5, "bach"), 3); + TEST(su_strncspn("foobar", 5, "ri"), 5); + TEST(su_strncspn("foobar", 5, "b"), 3); + TEST(su_strncspn("", 5, "fo"), 0); + TEST(su_strncspn(NULL, 0, "fo"), 0); + TEST(su_strncspn("foobarf", 5, "a"), 4); + + END(); +} + int main(int argc, char *argv[]) { int retval = 0; @@ -247,7 +344,10 @@ int main(int argc, char *argv[]) retval |= test_pattern(); fflush(stdout); retval |= test_strcasestr(); fflush(stdout); + retval |= test_casematch(); fflush(stdout); + retval |= test_strmatch(); fflush(stdout); + + retval |= test_strnspn(); fflush(stdout); return retval; } -