commit 753ceac3272a8c4f150ace8b4695991a85345c3c
parent aebe8604605b41803c43c1def87549569b821b27
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date: Fri, 22 Sep 2023 15:28:36 +0000
Revamp case functions, add strcasestr() fallback
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat:
12 files changed, 52 insertions(+), 47 deletions(-)
diff --git a/NEWS b/NEWS
@@ -10,6 +10,8 @@ In 2.14.0.0
- New strerr macros to warn with a "fatal" message.
- New cdb functions: cdb_hashv(), cdbmake_addv().
- child_spawn() revamp. Prototype change (last arg is a size_t).
+ - case_lowerb() and friends now use ctype.h functions
+ - case_str() removed, strcasestr() fallback implementation added.
In 2.13.1.1
diff --git a/doc/upgrade.html b/doc/upgrade.html
@@ -26,6 +26,9 @@
<li> New cdb functions: <tt>cdb_hashv()</tt>, <tt>cdbmake_addv()</tt> </li>
<li> New <tt>cspawn</tt> function, for posix_spawn/fork unification </li>
<li> <tt>child_spawn</tt> functions are now declared in <tt>cspawn.h</tt> </li>
+ <li> <tt>case_lowerb() and friends now use ctype.h functions, supporting locale
+if it is declared in the application. </li>
+ <li> <tt>case_str()</tt> removed, strcasestr() fallback implementation added </li>
</ul>
<h2> in 2.13.1.1 </h2>
diff --git a/package/deps.mak b/package/deps.mak
@@ -166,6 +166,7 @@ src/libposixplz/mkptemp.o src/libposixplz/mkptemp.lo: src/libposixplz/mkptemp.c
src/libposixplz/mkptemp2.o src/libposixplz/mkptemp2.lo: src/libposixplz/mkptemp2.c src/include/skalibs/djbunix.h src/include/skalibs/posixplz.h
src/libposixplz/munmap_void.o src/libposixplz/munmap_void.lo: src/libposixplz/munmap_void.c src/include/skalibs/posixplz.h
src/libposixplz/setgroups.o src/libposixplz/setgroups.lo: src/libposixplz/setgroups.c src/include/skalibs/nonposix.h src/include/skalibs/posixishard.h src/include/skalibs/setgroups.h src/include/skalibs/sysdeps.h
+src/libposixplz/strcasestr.o src/libposixplz/strcasestr.lo: src/libposixplz/strcasestr.c src/include/skalibs/bytestr.h src/include/skalibs/sysdeps.h
src/libposixplz/strnlen.o src/libposixplz/strnlen.lo: src/libposixplz/strnlen.c src/include/skalibs/bytestr.h src/include/skalibs/posixishard.h src/include/skalibs/sysdeps.h
src/libposixplz/touch.o src/libposixplz/touch.lo: src/libposixplz/touch.c src/include/skalibs/djbunix.h src/include/skalibs/nonposix.h src/include/skalibs/posixplz.h src/include/skalibs/sysdeps.h
src/libposixplz/unlink_void.o src/libposixplz/unlink_void.lo: src/libposixplz/unlink_void.c src/include/skalibs/posixplz.h
@@ -270,7 +271,6 @@ src/libstddjb/byte_zzero.o src/libstddjb/byte_zzero.lo: src/libstddjb/byte_zzero
src/libstddjb/case_lowerb.o src/libstddjb/case_lowerb.lo: src/libstddjb/case_lowerb.c src/include/skalibs/bytestr.h
src/libstddjb/case_lowers.o src/libstddjb/case_lowers.lo: src/libstddjb/case_lowers.c src/include/skalibs/bytestr.h
src/libstddjb/case_startb.o src/libstddjb/case_startb.lo: src/libstddjb/case_startb.c src/include/skalibs/bytestr.h
-src/libstddjb/case_str.o src/libstddjb/case_str.lo: src/libstddjb/case_str.c src/include/skalibs/bytestr.h src/include/skalibs/nonposix.h src/include/skalibs/sysdeps.h
src/libstddjb/case_upperb.o src/libstddjb/case_upperb.lo: src/libstddjb/case_upperb.c src/include/skalibs/bytestr.h
src/libstddjb/case_uppers.o src/libstddjb/case_uppers.lo: src/libstddjb/case_uppers.c src/include/skalibs/bytestr.h
src/libstddjb/cbuffer_get.o src/libstddjb/cbuffer_get.lo: src/libstddjb/cbuffer_get.c src/include/skalibs/cbuffer.h src/include/skalibs/siovec.h
diff --git a/src/include/skalibs/bytestr.h b/src/include/skalibs/bytestr.h
@@ -49,6 +49,5 @@ extern void case_upperb (char *, size_t) ;
#define case_equalb(a, n, b) (!strncasecmp(a, (b), n))
#define case_starts(s, t) case_startb(s, strlen(s), t)
extern int case_startb (char const *, size_t, char const *) gccattr_pure ;
-extern size_t case_str (char const *, char const *) gccattr_pure ;
#endif
diff --git a/src/include/skalibs/posixplz.h b/src/include/skalibs/posixplz.h
@@ -24,6 +24,7 @@ extern char **environ ;
*/
extern void *memmem (void const *, size_t, void const *, size_t) gccattr_pure ;
+extern char *strcasestr (char const *, char const *) gccattr_pure ;
extern int getpeereid (int, uid_t *, gid_t *) ;
diff --git a/src/libposixplz/strcasestr.c b/src/libposixplz/strcasestr.c
@@ -0,0 +1,28 @@
+/* ISC license. */
+
+#include <skalibs/sysdeps.h>
+
+#ifndef SKALIBS_HASSTRCASESTR
+
+#include <string.h>
+
+#include <skalibs/bytestr.h>
+
+ /* XXX: copies strings on the stack, careful with long strings */
+
+char *strcasestr (char const *haystack, char const *needle)
+{
+ size_t nlen = strlen(needle) ;
+ size_t hlen = strlen(haystack) ;
+ char *p ;
+ char lneedle[nlen + 1] ;
+ char lhaystack[hlen + 1] ;
+ memcpy(lneedle, needle, nlen + 1) ;
+ memcpy(lhaystack, haystack, hlen + 1) ;
+ case_lowerb(lneedle, nlen) ;
+ case_lowerb(lhaystack, hlen) ;
+ p = strstr(lhaystack, lneedle) ;
+ return p ? haystack + (p - lhaystack) : 0 ;
+}
+
+#endif
diff --git a/src/libstddjb/case_lowerb.c b/src/libstddjb/case_lowerb.c
@@ -1,13 +1,14 @@
/* ISC license. */
+#include <ctype.h>
+
#include <skalibs/bytestr.h>
void case_lowerb (char *s, size_t len)
{
- unsigned char const d = 'a' - 'A' ;
while (len--)
{
- if (('A' <= *s) && (*s <= 'Z')) *s += d ;
- ++s ;
+ int c = tolower(*s) ;
+ *s++ = (unsigned char)c ;
}
}
diff --git a/src/libstddjb/case_lowers.c b/src/libstddjb/case_lowers.c
@@ -1,13 +1,14 @@
/* ISC license. */
+#include <ctype.h>
+
#include <skalibs/bytestr.h>
void case_lowers (char *s)
{
- unsigned char const d = 'a' - 'A' ;
while (*s)
{
- if (('A' <= *s) && (*s <= 'Z')) *s += d ;
- ++s ;
+ int c = tolower(*s) ;
+ *s++ = (unsigned char)c ;
}
}
diff --git a/src/libstddjb/case_startb.c b/src/libstddjb/case_startb.c
@@ -2,6 +2,7 @@
#include <string.h>
#include <strings.h>
+
#include <skalibs/bytestr.h>
int case_startb (char const *s, size_t slen, char const *t)
diff --git a/src/libstddjb/case_str.c b/src/libstddjb/case_str.c
@@ -1,33 +0,0 @@
-/* ISC license. */
-
-#include <skalibs/sysdeps.h>
-
-#ifdef SKALIBS_HASSTRCASESTR
-
-#include <skalibs/nonposix.h>
-#include <string.h>
-#include <skalibs/bytestr.h>
-
-size_t case_str (char const *haystack, char const *needle)
-{
- char *p = strcasestr(haystack, needle) ;
- return p ? p - haystack : strlen(haystack) ;
-}
-
-#else
-
-#include <string.h>
-#include <strings.h>
-#include <skalibs/bytestr.h>
-
-size_t case_str (char const *haystack, char const *needle)
-{
- size_t nlen = strlen(needle) ;
- char const *p = haystack ;
- if (!nlen) return 0 ;
- for (; *p ; p++)
- if (!strncasecmp(p, needle, nlen)) return p - haystack ;
- return strlen(haystack) ;
-}
-
-#endif
diff --git a/src/libstddjb/case_upperb.c b/src/libstddjb/case_upperb.c
@@ -1,13 +1,14 @@
/* ISC license. */
+#include <ctype.h>
+
#include <skalibs/bytestr.h>
void case_upperb (char *s, size_t len)
{
- unsigned char const d = 'a' - 'A' ;
while (len--)
{
- if (('a' <= *s) && (*s <= 'z')) *s -= d ;
- ++s ;
+ int c = toupper(*s) ;
+ *s++ = (unsigned char)c ;
}
}
diff --git a/src/libstddjb/case_uppers.c b/src/libstddjb/case_uppers.c
@@ -1,13 +1,14 @@
/* ISC license. */
+#include <ctype.h>
+
#include <skalibs/bytestr.h>
void case_uppers (char *s)
{
- unsigned char const d = 'a' - 'A' ;
while (*s)
{
- if (('a' <= *s) && (*s <= 'z')) *s -= d ;
- ++s ;
+ int c = toupper(*s) ;
+ *s++ = (unsigned char)c ;
}
}