Don’t worry about Version 7 tolower
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 19 Jul 2023 20:51:55 +0000 (13:51 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Wed, 19 Jul 2023 20:53:01 +0000 (13:53 -0700)
Some code ported back to pre-C89 libraries where tolower (C) had
undefined behavior if C is not an upper case character.
Nowadays that function is _tolower which is itself obsolete,
and much Gnulib code already assumes this part of C89 anyway.
Assume C89 or better tolower, which simplifies the code
and should improve performance slightly.
* lib/mbmemcasecmp.c, lib/mbmemcasecoll.c, lib/mbscasecmp.c:
* lib/mbscasestr.c, lib/mbsncasecmp.c, lib/mbspcasecmp.c:
* lib/strcasecmp.c, lib/strcasestr.c, lib/strncasecmp.c:
(TOLOWER): Remove.  All uses replaced by tolower.

ChangeLog
lib/mbmemcasecmp.c
lib/mbmemcasecoll.c
lib/mbscasecmp.c
lib/mbscasestr.c
lib/mbsncasecmp.c
lib/mbspcasecmp.c
lib/strcasecmp.c
lib/strcasestr.c
lib/strncasecmp.c

index ba8fc38e8acb737aeb6028dfcd34400b37032da1..6a4d92d2188466eaa28f71533ba4ee41d38529bb 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2023-07-19  Paul Eggert  <eggert@cs.ucla.edu>
+
+       Don’t worry about Version 7 tolower
+       Some code ported back to pre-C89 libraries where tolower (C) had
+       undefined behavior if C is not an upper case character.
+       Nowadays that function is _tolower which is itself obsolete,
+       and much Gnulib code already assumes this part of C89 anyway.
+       Assume C89 or better tolower, which simplifies the code
+       and should improve performance slightly.
+       * lib/mbmemcasecmp.c, lib/mbmemcasecoll.c, lib/mbscasecmp.c:
+       * lib/mbscasestr.c, lib/mbsncasecmp.c, lib/mbspcasecmp.c:
+       * lib/strcasecmp.c, lib/strcasestr.c, lib/strncasecmp.c:
+       (TOLOWER): Remove.  All uses replaced by tolower.
+
 2023-07-19  Bruno Haible  <bruno@clisp.org>
 
        c32swidth, mbszero: Fix file list.
index e53ffd99aec8ae74762e8415ec79af0e5775350a..f4a397033e7b1fdffe04c17943b88c6b88871516 100644 (file)
@@ -27,8 +27,6 @@
 
 #include "mbiterf.h"
 
-#define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
-
 int
 mbmemcasecmp (const char *s1, size_t n1, const char *s2, size_t n2)
 {
@@ -79,8 +77,8 @@ mbmemcasecmp (const char *s1, size_t n1, const char *s2, size_t n2)
 
       while (p1 < s1_end && p2 < s2_end)
         {
-          unsigned char c1 = TOLOWER (*p1);
-          unsigned char c2 = TOLOWER (*p2);
+          unsigned char c1 = tolower (*p1);
+          unsigned char c2 = tolower (*p2);
           if (c1 != c2)
             {
               if (UCHAR_MAX <= INT_MAX)
index 08ea1df6bb4c79daf8125505e8a299ba480624a3..56a6e5f32a2fb9fbc426d13b2992e7eae0a07a18 100644 (file)
@@ -37,8 +37,6 @@
 #include "memcmp2.h"
 #include "memcoll.h"
 
-#define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
-
 /* Apply c32tolower() to the multibyte character sequence in INBUF, storing the
    result as a multibyte character sequence in OUTBUF.  */
 static size_t
@@ -136,7 +134,7 @@ apply_tolower (const char *inbuf, char *outbuf, size_t bufsize)
 {
   for (; bufsize > 0; bufsize--)
     {
-      *outbuf = TOLOWER ((unsigned char) *inbuf);
+      *outbuf = tolower ((unsigned char) *inbuf);
       inbuf++;
       outbuf++;
     }
index 58c0c7c56e1ddde6472c93cb2bd6ee79c1c1e699..3a20cb7f3ff696a99712a36fd08f5b79e4c84094 100644 (file)
@@ -26,8 +26,6 @@
 
 #include "mbuiterf.h"
 
-#define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
-
 /* Compare the character strings S1 and S2, ignoring case, returning less than,
    equal to or greater than zero if S1 is lexicographically less than, equal to
    or greater than S2.
@@ -82,8 +80,8 @@ mbscasecmp (const char *s1, const char *s2)
 
       do
         {
-          c1 = TOLOWER (*p1);
-          c2 = TOLOWER (*p2);
+          c1 = tolower (*p1);
+          c2 = tolower (*p2);
 
           if (c1 == '\0')
             break;
index 0753aeb86495ad7b1c5aae52bb05a5eff36bae4c..6946fff21cb7cd43266b37d45337f348c9eabfa0 100644 (file)
 #include "malloca.h"
 #include "mbuiter.h"
 
-#define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
-
 /* Knuth-Morris-Pratt algorithm.  */
 #define UNIT unsigned char
-#define CANON_ELEMENT(c) TOLOWER (c)
+#define CANON_ELEMENT(c) tolower (c)
 #include "str-kmp.h"
 
 /* Knuth-Morris-Pratt algorithm.
@@ -340,7 +338,7 @@ mbscasestr (const char *haystack, const char *needle)
 
           /* Speed up the following searches of needle by caching its first
              character.  */
-          unsigned char b = TOLOWER ((unsigned char) *needle);
+          unsigned char b = tolower ((unsigned char) *needle);
 
           needle++;
           for (;; haystack++)
@@ -383,7 +381,7 @@ mbscasestr (const char *haystack, const char *needle)
 
               outer_loop_count++;
               comparison_count++;
-              if (TOLOWER ((unsigned char) *haystack) == b)
+              if (tolower ((unsigned char) *haystack) == b)
                 /* The first character matches.  */
                 {
                   const char *rhaystack = haystack + 1;
@@ -398,8 +396,8 @@ mbscasestr (const char *haystack, const char *needle)
                         /* No match.  */
                         return NULL;
                       comparison_count++;
-                      if (TOLOWER ((unsigned char) *rhaystack)
-                          != TOLOWER ((unsigned char) *rneedle))
+                      if (tolower ((unsigned char) *rhaystack)
+                          != tolower ((unsigned char) *rneedle))
                         /* Nothing in this round.  */
                         break;
                     }
index d7b71ad188fe0d3edfb1d79679dc66412d9813b9..0b7027d91d934de29016ea36ec27083c52c5e9d5 100644 (file)
@@ -26,8 +26,6 @@
 
 #include "mbuiterf.h"
 
-#define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
-
 /* 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
    of at most N characters, ignoring case, returning less than, equal to or
@@ -87,8 +85,8 @@ mbsncasecmp (const char *s1, const char *s2, size_t n)
 
       for (; ; p1++, p2++)
         {
-          c1 = TOLOWER (*p1);
-          c2 = TOLOWER (*p2);
+          c1 = tolower (*p1);
+          c2 = tolower (*p2);
 
           if (--n == 0 || c1 == '\0' || c1 != c2)
             break;
index b7c3d85c965d6392bee795dfd54599ab69f71492..daec2ffda383434173baf4300a0c7b16c1029254 100644 (file)
@@ -24,8 +24,6 @@
 
 #include "mbuiterf.h"
 
-#define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
-
 /* Compare the initial segment of the character string STRING consisting of
    at most mbslen (PREFIX) characters with the character string PREFIX,
    ignoring case.  If the two match, return a pointer to the first byte
@@ -83,8 +81,8 @@ mbspcasecmp (const char *string, const char *prefix)
 
       for (; ; p1++, p2++)
         {
-          c1 = TOLOWER (*p1);
-          c2 = TOLOWER (*p2);
+          c1 = tolower (*p1);
+          c2 = tolower (*p2);
 
           if (c2 == '\0' || c1 != c2)
             break;
index 3a5ce3e1fd4a06fda1cbac5d895862a6dd9e7799..38a30ce0d7df77e19a109c430a17f0458b2ebe66 100644 (file)
@@ -22,8 +22,6 @@
 #include <ctype.h>
 #include <limits.h>
 
-#define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
-
 /* Compare strings S1 and S2, ignoring case, returning less than, equal to or
    greater than zero if S1 is lexicographically less than, equal to or greater
    than S2.
@@ -41,8 +39,8 @@ strcasecmp (const char *s1, const char *s2)
 
   do
     {
-      c1 = TOLOWER (*p1);
-      c2 = TOLOWER (*p2);
+      c1 = tolower (*p1);
+      c2 = tolower (*p2);
 
       if (c1 == '\0')
         break;
index 8eea435cd8aec43697a2feb25063855f54372304..d8e960123923ccff2ff361db752ec65f6e24636d 100644 (file)
 #include <ctype.h>
 #include <strings.h>
 
-#define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
-
 /* Two-Way algorithm.  */
 #define RETURN_TYPE char *
 #define AVAILABLE(h, h_l, j, n_l)                       \
   (!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l))     \
    && ((h_l) = (j) + (n_l)))
-#define CANON_ELEMENT(c) TOLOWER (c)
+#define CANON_ELEMENT(c) tolower (c)
 #define CMP_FUNC(p1, p2, l)                             \
   strncasecmp ((const char *) (p1), (const char *) (p2), l)
 #include "str-two-way.h"
@@ -52,8 +50,8 @@ strcasestr (const char *haystack_start, const char *needle_start)
      NEEDLE if HAYSTACK is too short).  */
   while (*haystack && *needle)
     {
-      ok &= (TOLOWER ((unsigned char) *haystack)
-             == TOLOWER ((unsigned char) *needle));
+      ok &= (tolower ((unsigned char) *haystack)
+             == tolower ((unsigned char) *needle));
       haystack++;
       needle++;
     }
index c5c2cd35696f1f66d86a0f83a6b2ffd5ab88dc5a..825ad5b25dc331146003353d5b8c649b23ba78c3 100644 (file)
@@ -22,8 +22,6 @@
 #include <ctype.h>
 #include <limits.h>
 
-#define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
-
 /* Compare no more than N bytes of strings S1 and S2, ignoring case,
    returning less than, equal to or greater than zero if S1 is
    lexicographically less than, equal to or greater than S2.
@@ -41,8 +39,8 @@ strncasecmp (const char *s1, const char *s2, size_t n)
 
   do
     {
-      c1 = TOLOWER (*p1);
-      c2 = TOLOWER (*p2);
+      c1 = tolower (*p1);
+      c2 = tolower (*p2);
 
       if (--n == 0 || c1 == '\0')
         break;