diff options
Diffstat (limited to 'string')
-rw-r--r-- | string/bits/string2.h | 192 | ||||
-rw-r--r-- | string/tester.c | 117 |
2 files changed, 252 insertions, 57 deletions
diff --git a/string/bits/string2.h b/string/bits/string2.h index 403bcca77c..45c6112b55 100644 --- a/string/bits/string2.h +++ b/string/bits/string2.h @@ -1,5 +1,5 @@ /* Machine-independant string function optimizations. - Copyright (C) 1997 Free Software Foundation, Inc. + Copyright (C) 1997, 1998 Free Software Foundation, Inc. This file is part of the GNU C Library. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997. @@ -526,7 +526,15 @@ __STRING2_COPY_TYPE (8); ? strlen (s) \ : (((__const unsigned char *) (reject))[1] == '\0' \ ? __strcspn_c1 (s, ((__const char *) (reject))[0]) \ - : strcspn (s, reject))) \ + : (((__const unsigned char *) (reject))[2] == '\0' \ + ? __strcspn_c2 (s, ((__const char *) (reject))[0], \ + ((__const char *) (reject))[1]) \ + : (((__const unsigned char *) (reject))[3] == '\0' \ + ? __strcspn_c3 (s, \ + ((__const char *) (reject))[0], \ + ((__const char *) (reject))[1], \ + ((__const char *) (reject))[2]) \ + : strcspn (s, reject))))) \ : strcspn (s, reject))) __STRING_INLINE size_t __strcspn_c1 (__const char *__s, char __reject); @@ -538,6 +546,31 @@ __strcspn_c1 (__const char *__s, char __reject) ++__result; return __result; } + +__STRING_INLINE size_t __strcspn_c2 (__const char *__s, char __reject1, + char __reject2); +__STRING_INLINE size_t +__strcspn_c2 (__const char *__s, char __reject1, char __reject2) +{ + register size_t __result = 0; + while (__s[__result] != '\0' && __s[__result] != __reject1 + && __s[__result] != __reject2) + ++__result; + return __result; +} + +__STRING_INLINE size_t __strcspn_c3 (__const char *__s, char __reject1, + char __reject2, char __reject3); +__STRING_INLINE size_t +__strcspn_c3 (__const char *__s, char __reject1, char __reject2, + char __reject3) +{ + register size_t __result = 0; + while (__s[__result] != '\0' && __s[__result] != __reject1 + && __s[__result] != __reject2 && __s[__result] != __reject3) + ++__result; + return __result; +} #endif @@ -550,7 +583,15 @@ __strcspn_c1 (__const char *__s, char __reject) ? 0 \ : (((__const unsigned char *) (accept))[1] == '\0' \ ? __strspn_c1 (s, ((__const char *) (accept))[0]) \ - : strspn (s, accept))) \ + : (((__const unsigned char *) (accept))[2] == '\0' \ + ? __strspn_c2 (s, ((__const char *) (accept))[0], \ + ((__const char *) (accept))[1]) \ + : (((__const unsigned char *) (accept))[3] == '\0' \ + ? __strspn_c3 (s, \ + ((__const char *) (accept))[0], \ + ((__const char *) (accept))[1], \ + ((__const char *) (accept))[2]) \ + : strspn (s, accept))))) \ : strspn (s, accept))) __STRING_INLINE size_t __strspn_c1 (__const char *__s, char __accept); @@ -563,6 +604,31 @@ __strspn_c1 (__const char *__s, char __accept) ++__result; return __result; } + +__STRING_INLINE size_t __strspn_c2 (__const char *__s, char __accept1, + char __accept2); +__STRING_INLINE size_t +__strspn_c2 (__const char *__s, char __accept1, char __accept2) +{ + register size_t __result = 0; + /* Please note that __accept1 and __accept2 never can be '\0'. */ + while (__s[__result] == __accept1 || __s[__result] == __accept2) + ++__result; + return __result; +} + +__STRING_INLINE size_t __strspn_c3 (__const char *__s, char __accept1, + char __accept2, char __accept3); +__STRING_INLINE size_t +__strspn_c3 (__const char *__s, char __accept1, char __accept2, char __accept3) +{ + register size_t __result = 0; + /* Please note that __accept1 to __accept3 never can be '\0'. */ + while (__s[__result] == __accept1 || __s[__result] == __accept2 + || __s[__result] == __accept3) + ++__result; + return __result; +} #endif @@ -574,8 +640,40 @@ __strspn_c1 (__const char *__s, char __accept) ? NULL \ : (((__const unsigned char *) (accept))[1] == '\0' \ ? strchr (s, ((__const unsigned char *) (accept))[0]) \ - : strpbrk (s, accept))) \ + : (((__const unsigned char *) (accept))[2] == '\0' \ + ? __strpbrk_c2 (s, ((__const char *) (accept))[0], \ + ((__const char *) (accept))[1]) \ + : (((__const unsigned char *) (accept))[3] == '\0' \ + ? __strpbrk_c3 (s, \ + ((__const char *) (accept))[0], \ + ((__const char *) (accept))[1], \ + ((__const char *) (accept))[2]) \ + : strpbrk (s, accept))))) \ : strpbrk (s, accept))) + +__STRING_INLINE char *__strpbrk_c2 (__const char *__s, char __accept1, + char __accept2); +__STRING_INLINE char * +__strpbrk_c2 (__const char *__s, char __accept1, char __accept2) +{ + /* Please note that __accept1 and __accept2 never can be '\0'. */ + while (*__s != '\0' && *__s != __accept1 && *__s != __accept2) + ++__s; + return *__s == '\0' ? NULL : (char *) __s; +} + +__STRING_INLINE char *__strpbrk_c3 (__const char *__s, char __accept1, + char __accept2, char __accept3); +__STRING_INLINE char * +__strpbrk_c3 (__const char *__s, char __accept1, char __accept2, + char __accept3) +{ + /* Please note that __accept1 to __accept3 never can be '\0'. */ + while (*__s != '\0' && *__s != __accept1 && *__s != __accept2 + && *__s != __accept3) + ++__s; + return *__s == '\0' ? NULL : (char *) __s; +} #endif @@ -612,8 +710,7 @@ strnlen (__const char *__string, size_t __maxlen) (__extension__ (__builtin_constant_p (sep) && __string2_1bptr_p (sep) \ ? (((__const unsigned char *) (sep))[0] != '\0' \ && ((__const unsigned char *) (sep))[1] == '\0' \ - ? __strtok_r_1c (s, ((__const unsigned char *) (sep))[0],\ - nextp) \ + ? __strtok_r_1c (s, ((__const char *) (sep))[0], nextp) \ : strtok_r (s, sep, nextp)) \ : strtok_r (s, sep, nextp))) @@ -652,11 +749,18 @@ __strtok_r_1c (char *__s, char __sep, char **__nextp) # define strsep(s, reject) \ (__extension__ (__builtin_constant_p (reject) && __string2_1bptr_p (reject) \ - ? (((__const unsigned char *) (reject))[0] != '\0' \ - && ((__const unsigned char *) (reject))[1] == '\0' \ + && ((__const unsigned char *) (reject))[0] != '\0' \ + ? (((__const unsigned char *) (reject))[1] == '\0' \ ? __strsep_1c (s, \ - ((__const unsigned char *) (reject))[0]) \ - : __strsep_g (s, reject)) \ + ((__const char *) (reject))[0]) \ + : (((__const unsigned char *) (reject))[2] == '\0' \ + ? __strsep_2c (s, ((__const char *) (reject))[0], \ + ((__const char *) (reject))[1]) \ + : (((__const unsigned char *) (reject))[3] == '\0' \ + ? __strsep_3c (s, ((__const char *) (reject))[0], \ + ((__const char *) (reject))[1], \ + ((__const char *) (reject))[2]) \ + : __strsep_g (s, reject)))) \ : __strsep_g (s, reject))) __STRING_INLINE char *__strsep_1c (char **__s, char __reject); @@ -664,12 +768,68 @@ __STRING_INLINE char * __strsep_1c (char **__s, char __reject) { register char *__retval = *__s; - if (__retval == NULL || *__retval == '\0') - return NULL; - while (*__retval == __reject) - ++__retval; - if ((*__s = strchr (__retval, __reject)) != NULL) + if (__retval == NULL) + return *__s = NULL; + if (*__retval == __reject) *(*__s)++ = '\0'; + else + if ((*__s = strchr (__retval, __reject)) != NULL) + *(*__s)++ = '\0'; + else + *__s = NULL; + return __retval; +} + +__STRING_INLINE char *__strsep_2c (char **__s, char __reject1, char __reject2); +__STRING_INLINE char * +__strsep_2c (char **__s, char __reject1, char __reject2) +{ + register char *__retval = *__s; + if (__retval == NULL) + return *__s = NULL; + if (*__retval == __reject1 || *__retval == __reject2) + *(*__s)++ = '\0'; + else + { + register char *__cp = __retval; + while (*__cp != '\0' && *__cp != __reject1 && *__cp != __reject2) + ++__cp; + if (*__cp != '\0') + { + *__s = __cp; + *(*__s)++ = '\0'; + } + else + *__s = NULL; + } + return __retval; +} + +__STRING_INLINE char *__strsep_3c (char **__s, char __reject1, char __reject2, + char __reject3); +__STRING_INLINE char * +__strsep_3c (char **__s, char __reject1, char __reject2, char __reject3) +{ + register char *__retval = *__s; + if (__retval == NULL) + return *__s = NULL; + if (*__retval == __reject1 || *__retval == __reject2 + || *__retval == __reject3) + *(*__s)++ = '\0'; + else + { + register char *__cp = __retval; + while (*__cp != '\0' && *__cp != __reject1 && *__cp != __reject2 + && *__cp != __reject3) + ++__cp; + if (*__cp != '\0') + { + *__s = __cp; + *(*__s)++ = '\0'; + } + else + *__s = NULL; + } return __retval; } @@ -680,7 +840,7 @@ __strsep_g (char **__s, __const char *__reject) register char *__retval = *__s; if (__retval == NULL || *__retval == '\0') return NULL; - if ((*__s = strpbrk (__retval, __reject)) != NULL) + if ((*__s = strpbrk (__retval, __reject)) != '\0') *(*__s)++ = '\0'; return __retval; } diff --git a/string/tester.c b/string/tester.c index d74ab72852..9545c28080 100644 --- a/string/tester.c +++ b/string/tester.c @@ -1,5 +1,5 @@ /* Tester for string functions. - Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. + Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -479,7 +479,16 @@ test_strpbrk (void) check(strpbrk(one, "db") == one+1, 9); /* Another variant. */ (void) strcpy(one, ""); check(strpbrk(one, "bc") == NULL, 10); /* Empty string. */ - check(strpbrk(one, "") == NULL, 11); /* Both strings empty. */ + (void) strcpy(one, ""); + check(strpbrk(one, "bcd") == NULL, 11); /* Empty string. */ + (void) strcpy(one, ""); + check(strpbrk(one, "bcde") == NULL, 12); /* Empty string. */ + check(strpbrk(one, "") == NULL, 13); /* Both strings empty. */ + (void) strcpy(one, "abcabdea"); + check(strpbrk(one, "befg") == one+1, 14); /* Finding first. */ + check(strpbrk(one, "cbr") == one+1, 15); /* With multiple search. */ + check(strpbrk(one, "db") == one+1, 16); /* Another variant. */ + check(strpbrk(one, "efgh") == one+6, 17); /* And yet another. */ } void @@ -648,60 +657,86 @@ test_strsep (void) equal(strsep(&cp, ", "), "", 9); equal(strsep(&cp, ", "), "first", 10); /* Extra delims, 1 tok. */ equal(strsep(&cp, ", "), "", 11); - check(strsep(&cp, ", ") == NULL, 12); + equal(strsep(&cp, ", "), "", 12); + check(strsep(&cp, ", ") == NULL, 13); cp = strcpy(one, "1a, 1b; 2a, 2b"); - equal(strsep(&cp, ", "), "1a", 13); /* Changing delim lists. */ - equal(strsep(&cp, ", "), "", 14); - equal(strsep(&cp, "; "), "1b", 15); - equal(strsep(&cp, ", "), "", 16); - equal(strsep(&cp, ", "), "2a", 17); + equal(strsep(&cp, ", "), "1a", 14); /* Changing delim lists. */ + equal(strsep(&cp, ", "), "", 15); + equal(strsep(&cp, "; "), "1b", 16); + equal(strsep(&cp, ", "), "", 17); + equal(strsep(&cp, ", "), "2a", 18); cp = strcpy(two, "x-y"); - equal(strsep(&cp, "-"), "x", 18); /* New string before done. */ - equal(strsep(&cp, "-"), "y", 19); - check(strsep(&cp, "-") == NULL, 20); - cp = strcpy(one, "a,b, c,, ,d"); - equal(strsep(&cp, ", "), "a", 21); /* Different separators. */ - equal(strsep(&cp, ", "), "b", 22); - equal(strsep(&cp, " ,"), "", 23); - equal(strsep(&cp, " ,"), "c", 24); /* Permute list too. */ - equal(strsep(&cp, " ,"), "", 25); + equal(strsep(&cp, "-"), "x", 19); /* New string before done. */ + equal(strsep(&cp, "-"), "y", 20); + check(strsep(&cp, "-") == NULL, 21); + cp = strcpy(one, "a,b, c,, ,d "); + equal(strsep(&cp, ", "), "a", 22); /* Different separators. */ + equal(strsep(&cp, ", "), "b", 23); + equal(strsep(&cp, " ,"), "", 24); + equal(strsep(&cp, " ,"), "c", 25); /* Permute list too. */ equal(strsep(&cp, " ,"), "", 26); equal(strsep(&cp, " ,"), "", 27); - equal(strsep(&cp, " ,"), "d", 28); - check(strsep(&cp, ", ") == NULL, 29); - check(strsep(&cp, ", ") == NULL, 30); /* Persistence. */ + equal(strsep(&cp, " ,"), "", 28); + equal(strsep(&cp, " ,"), "d", 29); + equal(strsep(&cp, " ,"), "", 30); + check(strsep(&cp, ", ") == NULL, 31); + check(strsep(&cp, ", ") == NULL, 32); /* Persistence. */ cp = strcpy(one, ", "); - equal(strsep(&cp, ", "), "", 31); - equal(strsep(&cp, ", "), "", 32); - check(strsep(&cp, ", ") == NULL, 33); /* No tokens. */ + equal(strsep(&cp, ", "), "", 33); + equal(strsep(&cp, ", "), "", 34); + equal(strsep(&cp, ", "), "", 35); + check(strsep(&cp, ", ") == NULL, 36); /* No tokens. */ cp = strcpy(one, ""); - check(strsep(&cp, ", ") == NULL, 34); /* Empty string. */ + equal(strsep(&cp, ", "), "", 37); + check(strsep(&cp, ", ") == NULL, 38); /* Empty string. */ cp = strcpy(one, "abc"); - equal(strsep(&cp, ", "), "abc", 35); /* No delimiters. */ - check(strsep(&cp, ", ") == NULL, 36); + equal(strsep(&cp, ", "), "abc", 39); /* No delimiters. */ + check(strsep(&cp, ", ") == NULL, 40); cp = strcpy(one, "abc"); - equal(strsep(&cp, ""), "abc", 37); /* Empty delimiter list. */ - check(strsep(&cp, "") == NULL, 38); + equal(strsep(&cp, ""), "abc", 41); /* Empty delimiter list. */ + check(strsep(&cp, "") == NULL, 42); (void) strcpy(one, "abcdefgh"); cp = strcpy(one, "a,b,c"); - equal(strsep(&cp, ","), "a", 39); /* Basics again... */ - equal(strsep(&cp, ","), "b", 40); - equal(strsep(&cp, ","), "c", 41); - check(strsep(&cp, ",") == NULL, 42); - equal(one+6, "gh", 43); /* Stomped past end? */ - equal(one, "a", 44); /* Stomped old tokens? */ - equal(one+2, "b", 45); - equal(one+4, "c", 46); + equal(strsep(&cp, ","), "a", 43); /* Basics again... */ + equal(strsep(&cp, ","), "b", 44); + equal(strsep(&cp, ","), "c", 45); + check(strsep(&cp, ",") == NULL, 46); + equal(one+6, "gh", 47); /* Stomped past end? */ + equal(one, "a", 48); /* Stomped old tokens? */ + equal(one+2, "b", 49); + equal(one+4, "c", 50); { char text[] = "This,is,a,test"; char *list = strdupa (text); - equal (strsep (&list, ","), "This", 47); - equal (strsep (&list, ","), "is", 48); - equal (strsep (&list, ","), "a", 49); - equal (strsep (&list, ","), "test", 50); - check (strsep (&list, ",") == NULL, 51); + equal (strsep (&list, ","), "This", 51); + equal (strsep (&list, ","), "is", 52); + equal (strsep (&list, ","), "a", 53); + equal (strsep (&list, ","), "test", 54); + check (strsep (&list, ",") == NULL, 55); } + + cp = strcpy(one, "a,b, c,, ,d,"); + equal(strsep(&cp, ","), "a", 56); /* Different separators. */ + equal(strsep(&cp, ","), "b", 57); + equal(strsep(&cp, ","), " c", 58); /* Permute list too. */ + equal(strsep(&cp, ","), "", 59); + equal(strsep(&cp, ","), " ", 60); + equal(strsep(&cp, ","), "d", 61); + equal(strsep(&cp, ","), "", 62); + check(strsep(&cp, ",") == NULL, 63); + check(strsep(&cp, ",") == NULL, 64); /* Persistence. */ + + cp = strcpy(one, "a,b, c,, ,d,"); + equal(strsep(&cp, "xy,"), "a", 65); /* Different separators. */ + equal(strsep(&cp, "x,y"), "b", 66); + equal(strsep(&cp, ",xy"), " c", 67); /* Permute list too. */ + equal(strsep(&cp, "xy,"), "", 68); + equal(strsep(&cp, "x,y"), " ", 69); + equal(strsep(&cp, ",xy"), "d", 70); + equal(strsep(&cp, "xy,"), "", 71); + check(strsep(&cp, "x,y") == NULL, 72); + check(strsep(&cp, ",xy") == NULL, 73); /* Persistence. */ } void |