]> Savannah Git Hosting - gnulib.git/commitdiff
trim: Fix trim_trailing result in multibyte locales.
authorBruno Haible <bruno@clisp.org>
Sun, 2 Apr 2023 19:03:55 +0000 (21:03 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 8 Apr 2023 10:56:13 +0000 (12:56 +0200)
* lib/trim.c (trim2): Simplify algorithm for trim_trailing in multibyte
locales, to use 2 instead of 3 states.
(IF_LINT): Remove macro.

ChangeLog
lib/trim.c

index 25b10419cae0625cb0bfd4c6a57b4ff0534ffc76..357c4e36b99bad0c83d50df63120649fd6bfebb8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2023-04-02  Bruno Haible  <bruno@clisp.org>
+
+       trim: Fix trim_trailing result in multibyte locales.
+       * lib/trim.c (trim2): Simplify algorithm for trim_trailing in multibyte
+       locales, to use 2 instead of 3 states.
+       (IF_LINT): Remove macro.
+
 2023-04-01  Bruno Haible  <bruno@clisp.org>
 
        stddef: Fix __need_wint_t handling in case of two stddef.h overrides.
index def08eb355ef347540bd9a6245a87200cc448bd1..d33dd13a5c1e09a6f90035323f840a8df3c421e0 100644 (file)
@@ -1,5 +1,5 @@
 /* Removes leading and/or trailing whitespaces
-   Copyright (C) 2006-2022 Free Software Foundation, Inc.
+   Copyright (C) 2006-2023 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 #include "mbiter.h"
 #include "xalloc.h"
 
-/* Use this to suppress gcc's "...may be used before initialized" warnings. */
-#if defined GCC_LINT || defined lint
-# define IF_LINT(Code) Code
-#else
-# define IF_LINT(Code) /* empty */
-#endif
-
 char *
 trim2 (const char *s, int how)
 {
@@ -65,42 +58,21 @@ trim2 (const char *s, int how)
       /* Trim trailing whitespaces. */
       if (how != TRIM_LEADING)
         {
-          unsigned int state = 0;
-          char *r IF_LINT (= NULL); /* used only while state = 2 */
+          char *start_of_spaces = NULL;
 
           mbi_init (i, d, strlen (d));
 
           for (; mbi_avail (i); mbi_advance (i))
-            {
-              if (state == 0 && mb_isspace (mbi_cur (i)))
-                continue;
-
-              if (state == 0 && !mb_isspace (mbi_cur (i)))
-                {
-                  state = 1;
-                  continue;
-                }
-
-              if (state == 1 && !mb_isspace (mbi_cur (i)))
-                continue;
-
-              if (state == 1 && mb_isspace (mbi_cur (i)))
-                {
-                  state = 2;
-                  r = (char *) mbi_cur_ptr (i);
-                }
-              else if (state == 2 && mb_isspace (mbi_cur (i)))
-                {
-                  /* empty */
-                }
-              else
-                {
-                  state = 1;
-                }
-            }
-
-          if (state == 2)
-            *r = '\0';
+            if (mb_isspace (mbi_cur (i)))
+              {
+                if (start_of_spaces == NULL)
+                  start_of_spaces = (char *) mbi_cur_ptr (i);
+              }
+            else
+              start_of_spaces = NULL;
+
+          if (start_of_spaces != NULL)
+            *start_of_spaces = '\0';
         }
     }
   else