Commit 39f0d052 authored by Pekka Pessi's avatar 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()

darcs-hash:20090108180421-db55f-09f628afe3f5a7af303476d39894efc05aad9689.gz
parent 1986eb30
......@@ -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
......
......@@ -22,52 +22,65 @@
*
*/
/**@internal @file strcasestr.c
* @brief Backup implementation of strcasestr()
#ifndef SOFIA_SIP_SU_STRING_H
/** Defined when <sofia-sip/su_string.h> is included. */
#define SOFIA_SIP_SU_STRING_H
/**@file sofia-sip/su_string.h
*
* @brief String functions for Sofia-SIP.
*
* Various string comparison functions also accepting NULL pointer as empty
* string:
* - su_strcmp(),
* - su_strncmp(),
* - su_strcasecmp() (comparison with US-ASCII case folding to lower case),
* - su_strncasecmp() (comparison with US-ASCII case folding to lower case)
* - su_casematch() (match token with US-ASCII case folding to lower case)
* - su_casenmatch() (match token with US-ASCII case folding to lower case)
*
* @author Pekka Pessi <Pekka.Pessi@nokia.com>
* Also includes span functions testing at most @a n bytes:
* - su_strncspn()
* - su_strnspn().
*/
#include "config.h"
#ifndef SU_CONFIG_H
#include <sofia-sip/su_config.h>
#endif
#include <ctype.h>
#include <stddef.h>
#include <string.h>
SOFIA_BEGIN_DECLS
su_inline int su_strcmp(char const *a, char const *b)
{
return strcmp(a ? a : "", b ? b : "");
}
/* Naive implementation of strcasestr() */
char *strcasestr(const char *haystack,
const char *needle)
su_inline int su_strncmp(char const *a, char const *b, size_t n)
{
unsigned char lcn, ucn;
unsigned i;
return strncmp(a ? a : "", b ? b : "", n);
}
if (haystack == NULL || needle == NULL)
return NULL;
SOFIAPUBFUN char *su_strcasestr(const char *haystack, const char *needle);
lcn = ucn = needle[0];
if (isupper(lcn))
lcn = tolower(lcn);
else if (islower(ucn))
ucn = toupper(ucn);
SOFIAPUBFUN int su_strcasecmp(char const *s1, char const *s2);
SOFIAPUBFUN int su_strncasecmp(char const *s1, char const *s2, size_t n);
if (lcn == 0)
return (char *)haystack;
SOFIAPUBFUN int su_strmatch(char const *str, char const *with);
SOFIAPUBFUN int su_strnmatch(char const *str, char const *with, size_t n);
while (haystack[0] != 0) {
if (lcn == haystack[0] || ucn == haystack[0]) {
for (i = 1; ; i++) {
char n = needle[i], h = haystack[i];
if (n == 0)
return (char *)haystack;
if (h == 0)
return NULL;
if (isupper(n)) n = tolower(n);
if (isupper(h)) h = tolower(h);
if (n != h)
break;
}
}
haystack++;
}
SOFIAPUBFUN int su_casematch(char const *s1, char const *with);
SOFIAPUBFUN int su_casenmatch(char const *s1, char const *with, size_t n);
return NULL; /* Not found */
}
SOFIAPUBFUN size_t su_strnspn(char const *s, size_t size, char const *term);
SOFIAPUBFUN size_t su_strncspn(char const *s, size_t ssize, char const *reject);
SOFIAPUBFUN size_t su_memspn(const void *mem, size_t memlen,
const void *accept, size_t acceptlen);
SOFIAPUBFUN size_t su_memcspn(const void *mem, size_t memlen,
const void *reject, size_t rejectlen);
SOFIA_END_DECLS
#endif /* !SOFIA_SIP_SU_STRING_H */
This diff is collapsed.
......@@ -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 <Pekka.Pessi@nokia.com>
*
......@@ -35,6 +35,8 @@
#include "config.h"
#include <sofia-sip/su_string.h>
#include <stddef.h>
#include <stdlib.h>
#include <limits.h>
......@@ -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 <string.h>
......@@ -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;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment