From: Paul Eggert Date: Tue, 26 Sep 2023 22:49:02 +0000 (-0700) Subject: Remaining support for GNULIB_MCEL_PREFER X-Git-Tag: v1.0~767 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=5e4405c7fafad960dd7bbb2311bf10df1655ba91;p=gnulib.git Remaining support for GNULIB_MCEL_PREFER Support mcel API in remaining modules where this might matter, for apps that prefer it. * lib/mbmemcasecmp.c, lib/mbscspn.c, lib/mbsncasecmp.c, lib/mbsnlen.c: * lib/mbspbrk.c, lib/mbspcasecmp.c, lib/mbssep.c, lib/mbsspn.c: * lib/regex-quote.c: Include mcel.h instead of mbiterf.h or mbuiterf.h, if GNULIB_MCEL_PREFER. * lib/mbmemcasecmp.c (mbmemcasecmp), lib/mbscspn.c (mbscspn): * lib/mbsncasecmp.c (mbsncasecmp), lib/mbsnlen.c (mbsnlen): * lib/mbspbrk.c (mbspbrk), lib/mbspcasecmp.c (mbspcasecmp): * lib/mbssep.c (mbssep), lib/mbsspn.c (mbsspn): * lib/regex-quote.c (regex_quote_length, regex_quote_copy): Use mcel API, if GNULIB_MCEL_PREFER. * lib/mbscspn.c, lib/mbspbrk.c, lib/mbspcasecmp.c, lib/mbsspn.c: Include stdlib.h, for MB_CUR_MAX. * modules/mbmemcasecmp, modules/mbsncasecmp, modules/mbspcasecmp: Depend on c32tolower. * modules/regex-quote: Depend on mempcpy. --- diff --git a/ChangeLog b/ChangeLog index 7b7246f2a5..d405872dff 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,25 @@ +2023-09-26 Paul Eggert + + Remaining support for GNULIB_MCEL_PREFER + Support mcel API in remaining modules where this might matter, + for apps that prefer it. + * lib/mbmemcasecmp.c, lib/mbscspn.c, lib/mbsncasecmp.c, lib/mbsnlen.c: + * lib/mbspbrk.c, lib/mbspcasecmp.c, lib/mbssep.c, lib/mbsspn.c: + * lib/regex-quote.c: + Include mcel.h instead of mbiterf.h or mbuiterf.h, + if GNULIB_MCEL_PREFER. + * lib/mbmemcasecmp.c (mbmemcasecmp), lib/mbscspn.c (mbscspn): + * lib/mbsncasecmp.c (mbsncasecmp), lib/mbsnlen.c (mbsnlen): + * lib/mbspbrk.c (mbspbrk), lib/mbspcasecmp.c (mbspcasecmp): + * lib/mbssep.c (mbssep), lib/mbsspn.c (mbsspn): + * lib/regex-quote.c (regex_quote_length, regex_quote_copy): + Use mcel API, if GNULIB_MCEL_PREFER. + * lib/mbscspn.c, lib/mbspbrk.c, lib/mbspcasecmp.c, lib/mbsspn.c: + Include stdlib.h, for MB_CUR_MAX. + * modules/mbmemcasecmp, modules/mbsncasecmp, modules/mbspcasecmp: + Depend on c32tolower. + * modules/regex-quote: Depend on mempcpy. + 2023-09-25 Paul Eggert mbscasestr: support GNULIB_MCEL_PREFER diff --git a/lib/mbmemcasecmp.c b/lib/mbmemcasecmp.c index 2bf4effa47..0ecc8f34d2 100644 --- a/lib/mbmemcasecmp.c +++ b/lib/mbmemcasecmp.c @@ -25,7 +25,11 @@ #include #include -#include "mbiterf.h" +#if GNULIB_MCEL_PREFER +# include "mcel.h" +#else +# include "mbiterf.h" +#endif int mbmemcasecmp (const char *s1, size_t n1, const char *s2, size_t n2) @@ -35,14 +39,25 @@ mbmemcasecmp (const char *s1, size_t n1, const char *s2, size_t n2) const char *iter1 = s1; const char *iter2 = s2; + const char *s1_end = s1 + n1; + const char *s2_end = s2 + n2; if (MB_CUR_MAX > 1) { - const char *s1_end = s1 + n1; +#if GNULIB_MCEL_PREFER + while ((iter1 < s1_end) & (iter2 < s2_end)) + { + mcel_t g1 = mcel_scan (iter1, s1_end); iter1 += g1.len; + mcel_t g2 = mcel_scan (iter2, s2_end); iter2 += g2.len; + int cmp = mcel_tocmp (c32tolower, g1, g2); + if (cmp) + return cmp; + } + return (iter1 < s1_end) - (iter2 < s2_end); +#else mbif_state_t state1; mbif_init (state1); - const char *s2_end = s2 + n2; mbif_state_t state2; mbif_init (state2); @@ -66,12 +81,10 @@ mbmemcasecmp (const char *s1, size_t n1, const char *s2, size_t n2) /* s1 terminated before s2. */ return -1; return 0; +#endif } else { - const char *s1_end = s1 + n1; - const char *s2_end = s2 + n2; - while (iter1 < s1_end && iter2 < s2_end) { unsigned char c1 = *iter1++; diff --git a/lib/mbscspn.c b/lib/mbscspn.c index f67a557f4b..e5af4b4648 100644 --- a/lib/mbscspn.c +++ b/lib/mbscspn.c @@ -20,7 +20,13 @@ /* Specification. */ #include -#include "mbuiterf.h" +#include + +#if GNULIB_MCEL_PREFER +# include "mcel.h" +#else +# include "mbuiterf.h" +#endif /* Find the first occurrence in the character string STRING of any character in the character string ACCEPT. Return the number of bytes from the @@ -40,6 +46,27 @@ mbscspn (const char *string, const char *accept) /* General case. */ if (MB_CUR_MAX > 1) { +#if GNULIB_MCEL_PREFER + mcel_t a, g; + size_t i; + for (i = 0; string[i]; i += g.len) + { + g = mcel_scanz (string + i); + if (g.len == 1) + { + if (mbschr (accept, string[i])) + return i; + } + else + for (char const *aiter = accept; *aiter; aiter += a.len) + { + a = mcel_scanz (aiter); + if (mcel_cmp (g, a) == 0) + return i; + } + } + return i; +#else mbuif_state_t state; const char *iter; for (mbuif_init (state), iter = string; mbuif_avail (state, iter); ) @@ -67,6 +94,7 @@ mbscspn (const char *string, const char *accept) } found: return iter - string; +#endif } else return strcspn (string, accept); diff --git a/lib/mbsncasecmp.c b/lib/mbsncasecmp.c index 8ee1df7d4d..d58a18f23b 100644 --- a/lib/mbsncasecmp.c +++ b/lib/mbsncasecmp.c @@ -23,8 +23,13 @@ #include #include +#include -#include "mbuiterf.h" +#if GNULIB_MCEL_PREFER +# include "mcel.h" +#else +# include "mbuiterf.h" +#endif /* Compare the initial segment of the character string S1 consisting of at most N characters with the initial segment of the character string S2 consisting @@ -47,6 +52,17 @@ mbsncasecmp (const char *s1, const char *s2, size_t n) most often already in the very few first characters. */ if (MB_CUR_MAX > 1) { +#if GNULIB_MCEL_PREFER + while (true) + { + mcel_t g1 = mcel_scanz (iter1); iter1 += g1.len; + mcel_t g2 = mcel_scanz (iter2); iter2 += g2.len; + int cmp = mcel_tocmp (c32tolower, g1, g2); + n--; + if (cmp | !n | !g1.ch) + return cmp; + } +#else mbuif_state_t state1; mbuif_init (state1); @@ -75,6 +91,7 @@ mbsncasecmp (const char *s1, const char *s2, size_t n) /* s1 terminated before s2 and n. */ return -1; return 0; +#endif } else for (;;) diff --git a/lib/mbsnlen.c b/lib/mbsnlen.c index ec1dfcaec3..c15dd3032e 100644 --- a/lib/mbsnlen.c +++ b/lib/mbsnlen.c @@ -22,7 +22,11 @@ #include -#include "mbiterf.h" +#if GNULIB_MCEL_PREFER +# include "mcel.h" +#else +# include "mbiterf.h" +#endif /* Return the number of multibyte characters in the character string starting at STRING and ending at STRING + LEN. */ @@ -34,6 +38,11 @@ mbsnlen (const char *string, size_t len) size_t count = 0; const char *string_end = string + len; + +#if GNULIB_MCEL_PREFER + for (; *string; string += mcel_scan (string, string_end).len) + count++; +#else mbif_state_t state; const char *iter; for (mbif_init (state), iter = string; mbif_avail (state, iter, string_end); ) @@ -42,6 +51,7 @@ mbsnlen (const char *string, size_t len) count++; iter += mb_len (cur); } +#endif return count; } diff --git a/lib/mbspbrk.c b/lib/mbspbrk.c index 9482eb00cb..b1ee02942f 100644 --- a/lib/mbspbrk.c +++ b/lib/mbspbrk.c @@ -20,7 +20,13 @@ /* Specification. */ #include -#include "mbuiterf.h" +#include + +#if GNULIB_MCEL_PREFER +# include "mcel.h" +#else +# include "mbuiterf.h" +#endif /* Find the first occurrence in the character string STRING of any character in the character string ACCEPT. Return the pointer to it, or NULL if none @@ -36,9 +42,28 @@ mbspbrk (const char *string, const char *accept) /* General case. */ if (MB_CUR_MAX > 1) { + char const *iter = string; +#if GNULIB_MCEL_PREFER + mcel_t a, g; + for (char const *iter = string; *iter; iter += g.len) + { + g = mcel_scanz (iter); + if (g.len == 1) + { + if (mbschr (accept, *iter)) + return (char *) iter; + } + else + for (char const *aiter = accept; *aiter; aiter += a.len) + { + a = mcel_scanz (aiter); + if (mcel_cmp (a, g) == 0) + return (char *) iter; + } + } +#else mbuif_state_t state; - const char *iter; - for (mbuif_init (state), iter = string; mbuif_avail (state, iter); ) + for (mbuif_init (state); mbuif_avail (state, iter); ) { mbchar_t cur = mbuif_next (state, iter); if (mb_len (cur) == 1) @@ -61,6 +86,7 @@ mbspbrk (const char *string, const char *accept) } iter += mb_len (cur); } +#endif return NULL; } else diff --git a/lib/mbspcasecmp.c b/lib/mbspcasecmp.c index 090d12531b..9ff42977b0 100644 --- a/lib/mbspcasecmp.c +++ b/lib/mbspcasecmp.c @@ -21,8 +21,13 @@ #include #include +#include -#include "mbuiterf.h" +#if GNULIB_MCEL_PREFER +# include "mcel.h" +#else +# include "mbuiterf.h" +#endif /* Compare the initial segment of the character string STRING consisting of at most mbslen (PREFIX) characters with the character string PREFIX, @@ -47,6 +52,18 @@ mbspcasecmp (const char *string, const char *prefix) most often already in the very few first characters. */ if (MB_CUR_MAX > 1) { +#if GNULIB_MCEL_PREFER + while (*iter2) + { + if (!*iter1) + return NULL; + mcel_t g1 = mcel_scanz (iter1); iter1 += g1.len; + mcel_t g2 = mcel_scanz (iter2); iter2 += g2.len; + if (mcel_tocmp (c32tolower, g1, g2) != 0) + return NULL; + } + return (char *) iter1; +#else mbuif_state_t state1; mbuif_init (state1); @@ -71,6 +88,7 @@ mbspcasecmp (const char *string, const char *prefix) else /* STRING terminated before PREFIX. */ return NULL; +#endif } else for (;; iter1++, iter2++) diff --git a/lib/mbssep.c b/lib/mbssep.c index 31df5d1cef..d0734c384c 100644 --- a/lib/mbssep.c +++ b/lib/mbssep.c @@ -22,7 +22,11 @@ #include -#include "mbuiterf.h" +#if GNULIB_MCEL_PREFER +# include "mcel.h" +#else +# include "mbuiterf.h" +#endif char * mbssep (char **stringp, const char *delim) @@ -30,7 +34,6 @@ mbssep (char **stringp, const char *delim) if (MB_CUR_MAX > 1) { char *start = *stringp; - char *ptr; if (start == NULL) return NULL; @@ -38,24 +41,25 @@ mbssep (char **stringp, const char *delim) /* No need to optimize the cases of 0 or 1 delimiters specially, since mbspbrk already optimizes them. */ - ptr = mbspbrk (start, delim); + char *ptr = mbspbrk (start, delim); if (ptr == NULL) - { - *stringp = NULL; - return start; - } + *stringp = NULL; else { +#if GNULIB_MCEL_PREFER + *stringp = ptr + mcel_scanz (ptr).len; +#else mbuif_state_t state; mbuif_init (state); if (!mbuif_avail (state, ptr)) abort (); mbchar_t cur = mbuif_next (state, ptr); - *ptr = '\0'; *stringp = ptr + mb_len (cur); - return start; +#endif + *ptr = '\0'; } + return start; } else return strsep (stringp, delim); diff --git a/lib/mbsspn.c b/lib/mbsspn.c index 5e3832b72c..b74a5dd875 100644 --- a/lib/mbsspn.c +++ b/lib/mbsspn.c @@ -20,7 +20,13 @@ /* Specification. */ #include -#include "mbuiterf.h" +#include + +#if GNULIB_MCEL_PREFER +# include "mcel.h" +#else +# include "mbuiterf.h" +#endif /* Find the first occurrence in the character string STRING of any character not in the character string REJECT. Return the number of bytes from the @@ -35,33 +41,66 @@ mbsspn (const char *string, const char *reject) if (reject[1] == '\0') { unsigned char uc = (unsigned char) reject[0]; + const char *iter = string; if (MB_CUR_MAX > 1) { +#if GNULIB_MCEL_PREFER + for (mcel_t g; *iter; iter += g.len) + { + g = mcel_scanz (iter); + if (! (g.len == 1 && (unsigned char) *iter == uc)) + break; + } +#else mbuif_state_t state; - const char *iter; - for (mbuif_init (state), iter = string; mbuif_avail (state, iter); ) + for (mbuif_init (state); mbuif_avail (state, iter); ) { mbchar_t cur = mbuif_next (state, iter); if (!(mb_len (cur) == 1 && (unsigned char) *iter == uc)) break; iter += mb_len (cur); } - return iter - string; +#endif } else { - const char *ptr; - - for (ptr = string; *ptr != '\0'; ptr++) - if ((unsigned char) *ptr != uc) + for (; *iter != '\0'; iter++) + if ((unsigned char) *iter != uc) break; - return ptr - string; } + return iter - string; } /* General case. */ if (MB_CUR_MAX > 1) { +#if GNULIB_MCEL_PREFER + for (size_t i = 0; ; ) + { + char c = string[i]; + if (!c) + return i; + mcel_t g = mcel_scanz (string + i); + if (g.len == 1) + { + if (!mbschr (reject, c)) + return i; + } + else + { + for (char const *aiter = reject; ; ) + { + if (!*aiter) + return i; + mcel_t a = mcel_scanz (aiter); + if (mcel_cmp (a, g) == 0) + break; + aiter += a.len; + } + } + i += g.len; + } +#else mbuif_state_t state; const char *iter; for (mbuif_init (state), iter = string; mbuif_avail (state, iter); ) @@ -90,6 +129,7 @@ mbsspn (const char *string, const char *reject) } found: return iter - string; +#endif } else return strspn (string, reject); diff --git a/lib/regex-quote.c b/lib/regex-quote.c index cfc05ad139..45fac5abce 100644 --- a/lib/regex-quote.c +++ b/lib/regex-quote.c @@ -22,7 +22,12 @@ #include -#include "mbuiter.h" +#if GNULIB_MCEL_PREFER +# include "mcel.h" +#else +# include "mbuiter.h" +#endif + #include "xalloc.h" /* Characters that are special in a BRE. */ @@ -138,6 +143,16 @@ regex_quote_length (const char *string, const struct regex_quote_spec *spec) length += 2; /* for '^' at the beginning and '$' at the end */ if (spec->multibyte) { +#if GNULIB_MCEL_PREFER + char const *iter = string; + for (mcel_t g; *iter; iter += g.len) + { + g = mcel_scanz (iter); + /* We know that special contains only ASCII characters. */ + length += g.len == 1 && strchr (special, *iter); + } + length += iter - string; +#else mbui_iterator_t iter; for (mbui_init (iter, string); mbui_avail (iter); mbui_advance (iter)) @@ -148,6 +163,7 @@ regex_quote_length (const char *string, const struct regex_quote_spec *spec) length += 1; length += mb_len (mbui_cur (iter)); } +#endif } else { @@ -173,6 +189,17 @@ regex_quote_copy (char *p, const char *string, const struct regex_quote_spec *sp *p++ = '^'; if (spec->multibyte) { +#if GNULIB_MCEL_PREFER + for (char const *iter = string; *iter; ) + { + mcel_t g = mcel_scanz (iter); + *p = '\\'; + /* We know that special contains only ASCII characters. */ + p += g.len == 1 && strchr (special, *iter); + p = mempcpy (p, iter, g.len); + iter += g.len; + } +#else mbui_iterator_t iter; for (mbui_init (iter, string); mbui_avail (iter); mbui_advance (iter)) diff --git a/modules/mbmemcasecmp b/modules/mbmemcasecmp index 0034893cd0..856c84b361 100644 --- a/modules/mbmemcasecmp +++ b/modules/mbmemcasecmp @@ -6,6 +6,7 @@ lib/mbmemcasecmp.h lib/mbmemcasecmp.c Depends-on: +c32tolower mbiterf configure.ac: diff --git a/modules/mbsncasecmp b/modules/mbsncasecmp index 9d6e2af5ce..e01a1a8652 100644 --- a/modules/mbsncasecmp +++ b/modules/mbsncasecmp @@ -5,6 +5,7 @@ Files: lib/mbsncasecmp.c Depends-on: +c32tolower mbuiterf string diff --git a/modules/mbspcasecmp b/modules/mbspcasecmp index 7f2b463aaa..ffb58bc4f6 100644 --- a/modules/mbspcasecmp +++ b/modules/mbspcasecmp @@ -5,6 +5,7 @@ Files: lib/mbspcasecmp.c Depends-on: +c32tolower mbuiterf string diff --git a/modules/regex-quote b/modules/regex-quote index c88d14f95a..bdfaeb7d9d 100644 --- a/modules/regex-quote +++ b/modules/regex-quote @@ -9,6 +9,7 @@ Depends-on: stdbool xalloc mbuiter +mempcpy configure.ac: AC_REQUIRE([AC_C_RESTRICT])