bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/2] Don’t worry about Version 7 tolower


From: Paul Eggert
Subject: [PATCH 1/2] Don’t worry about Version 7 tolower
Date: Wed, 19 Jul 2023 13:53:16 -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           | 14 ++++++++++++++
 lib/mbmemcasecmp.c  |  6 ++----
 lib/mbmemcasecoll.c |  4 +---
 lib/mbscasecmp.c    |  6 ++----
 lib/mbscasestr.c    | 12 +++++-------
 lib/mbsncasecmp.c   |  6 ++----
 lib/mbspcasecmp.c   |  6 ++----
 lib/strcasecmp.c    |  6 ++----
 lib/strcasestr.c    |  8 +++-----
 lib/strncasecmp.c   |  6 ++----
 10 files changed, 35 insertions(+), 39 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index ba8fc38e8a..6a4d92d218 100644
--- 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.
diff --git a/lib/mbmemcasecmp.c b/lib/mbmemcasecmp.c
index e53ffd99ae..f4a397033e 100644
--- a/lib/mbmemcasecmp.c
+++ b/lib/mbmemcasecmp.c
@@ -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)
diff --git a/lib/mbmemcasecoll.c b/lib/mbmemcasecoll.c
index 08ea1df6bb..56a6e5f32a 100644
--- a/lib/mbmemcasecoll.c
+++ b/lib/mbmemcasecoll.c
@@ -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++;
     }
diff --git a/lib/mbscasecmp.c b/lib/mbscasecmp.c
index 58c0c7c56e..3a20cb7f3f 100644
--- a/lib/mbscasecmp.c
+++ b/lib/mbscasecmp.c
@@ -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;
diff --git a/lib/mbscasestr.c b/lib/mbscasestr.c
index 0753aeb864..6946fff21c 100644
--- a/lib/mbscasestr.c
+++ b/lib/mbscasestr.c
@@ -27,11 +27,9 @@
 #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;
                     }
diff --git a/lib/mbsncasecmp.c b/lib/mbsncasecmp.c
index d7b71ad188..0b7027d91d 100644
--- a/lib/mbsncasecmp.c
+++ b/lib/mbsncasecmp.c
@@ -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;
diff --git a/lib/mbspcasecmp.c b/lib/mbspcasecmp.c
index b7c3d85c96..daec2ffda3 100644
--- a/lib/mbspcasecmp.c
+++ b/lib/mbspcasecmp.c
@@ -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;
diff --git a/lib/strcasecmp.c b/lib/strcasecmp.c
index 3a5ce3e1fd..38a30ce0d7 100644
--- a/lib/strcasecmp.c
+++ b/lib/strcasecmp.c
@@ -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;
diff --git a/lib/strcasestr.c b/lib/strcasestr.c
index 8eea435cd8..d8e9601239 100644
--- a/lib/strcasestr.c
+++ b/lib/strcasestr.c
@@ -23,14 +23,12 @@
 #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++;
     }
diff --git a/lib/strncasecmp.c b/lib/strncasecmp.c
index c5c2cd3569..825ad5b25d 100644
--- a/lib/strncasecmp.c
+++ b/lib/strncasecmp.c
@@ -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;
-- 
2.41.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]