]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
its: Make whitespace normalization logic simpler
authorDaiki Ueno <ueno@gnu.org>
Fri, 15 Feb 2019 11:49:01 +0000 (12:49 +0100)
committerDaiki Ueno <ueno@gnu.org>
Fri, 15 Feb 2019 11:51:42 +0000 (12:51 +0100)
* gettext-tools/src/its.c (normalize_whitespace): Use parallel
pointers for normalizing whitespaces, instead of memmove.
Suggested by Bruno Haible.

gettext-tools/src/its.c

index 9b4b397e5e3683dc55d87093f355901e8eeadd82..8b31e0e1e51ac6e1c9a4dfb9cfb9b8e976c376f8 100644 (file)
@@ -403,23 +403,27 @@ normalize_whitespace (const char *text, enum its_whitespace_type_ty whitespace)
       /* Normalize whitespaces within the text, but not at the beginning
          nor the end of the text.  */
       {
-        char *result, *p, *end;
+        char *result, *p, *out;
+        bool last_ws;
 
         result = xstrdup (text);
-        end = result + strlen (result);
-        for (p = result; *p != '\0';)
+        for (p = out = result; *p != '\0'; p++)
           {
-            size_t len = strspn (p, " \t\n");
-            if (len > 0)
+            if (*p == ' ' || *p == '\t' || *p == '\n')
               {
-                *p = ' ';
-                memmove (p + 1, p + len, end - (p + len));
-                end -= len - 1;
-                *end = '\0';
-                p++;
+                if (!last_ws)
+                  {
+                    *out++ = ' ';
+                    last_ws = true;
+                  }
+              }
+            else
+              {
+                *out++ = *p;
+                last_ws = false;
               }
-            p += strcspn (p, " \t\n");
           }
+        *out = '\0';
         return result;
       }
     }