]> Savannah Git Hosting - gnulib.git/commitdiff
propername: tune single-byte code
authorPaul Eggert <eggert@cs.ucla.edu>
Sun, 27 Aug 2023 06:19:11 +0000 (23:19 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sun, 27 Aug 2023 06:34:01 +0000 (23:34 -0700)
* lib/propername.c (mbsstr_trimmed_wordbounded): Cache MB_CUR_MAX.
Simplify word boundary detection in single-byte code.

ChangeLog
lib/propername.c

index de52b57755eeeaeacda1c6f07a6c154c1dffe1d5..64db236853b4740f2cdfa6fef2e47fb18e92b860 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2023-08-26  Paul Eggert  <eggert@cs.ucla.edu>
+
+       propername: tune single-byte code
+       * lib/propername.c (mbsstr_trimmed_wordbounded): Cache MB_CUR_MAX.
+       Simplify word boundary detection in single-byte code.
+
 2023-08-26  Paul Eggert  <eggert@cs.ucla.edu>
 
        Tune single-byte code involving tolower
index f9eae490db3aa7a8e2b28bef9047e6acc92427b1..dbd96b87729153571ea2fa1f8d488aa599a86dbe 100644 (file)
@@ -55,15 +55,19 @@ mbsstr_trimmed_wordbounded (const char *string, const char *sub)
 {
   char *tsub = trim (sub);
   bool found = false;
+  bool multibyte_locale = MB_CUR_MAX > 1;
+  size_t tsublen;
+  if (! multibyte_locale)
+    tsublen = strlen (tsub);
 
-  for (; *string != '\0';)
+  while (*string != '\0')
     {
       const char *tsub_in_string = mbsstr (string, tsub);
       if (tsub_in_string == NULL)
         break;
       else
         {
-          if (MB_CUR_MAX > 1)
+          if (multibyte_locale)
             {
               mbui_iterator_t string_iter;
               bool word_boundary_before;
@@ -120,22 +124,9 @@ mbsstr_trimmed_wordbounded (const char *string, const char *sub)
             }
           else
             {
-              bool word_boundary_before;
-              const char *p;
-              bool word_boundary_after;
-
-              word_boundary_before = true;
-              if (string < tsub_in_string)
-                if (isalnum ((unsigned char) tsub_in_string[-1]))
-                  word_boundary_before = false;
-
-              p = tsub_in_string + strlen (tsub);
-              word_boundary_after = true;
-              if (*p != '\0')
-                if (isalnum ((unsigned char) *p))
-                  word_boundary_after = false;
-
-              if (word_boundary_before && word_boundary_after)
+              if ((string == tsub_in_string
+                   || !isalnum ((unsigned char) tsub_in_string[-1]))
+                  && !isalnum ((unsigned char) tsub_in_string[tsublen]))
                 {
                   found = true;
                   break;