]> diplodocus.org Git - nmh/commitdiff
cpstripped(), single-byte: Simplify logic.
authorRalph Corderoy <ralph@inputplus.co.uk>
Sat, 5 Aug 2017 17:20:07 +0000 (18:20 +0100)
committerRalph Corderoy <ralph@inputplus.co.uk>
Sat, 5 Aug 2017 17:20:07 +0000 (18:20 +0100)
Now that it doesn't have to fit in with the multi-byte version's logic,
it can be re-structured to be simpler.  No functional change intended.

sbr/fmt_scan.c

index 83e48b8..e5e1ff8 100644 (file)
@@ -299,38 +299,27 @@ cpstripped (charstring_t dest, size_t max, char *str)
 static void
 cpstripped (charstring_t dest, size_t max, char *str)
 {
 static void
 cpstripped (charstring_t dest, size_t max, char *str)
 {
-    int prevCtrl = 1;  /* This is 1 so we strip out leading spaces */
-    int len;
+    bool squash;
+    int c;
 
 
-    if (!str) {
+    if (!str)
        return;
        return;
-    }
-
-    len = strlen(str);
-
-    /*
-     * Process each character at a time; if we have multibyte support
-     * then deal with that here.
-     */
-
-    while (*str != '\0' && len > 0 && max > 0) {
-       int c = (unsigned char) *str;
-       len--;
-       if (iscntrl(c) || isspace(c)) {
-           str++;
-           if (! prevCtrl) {
-               charstring_push_back (dest, ' ');
-               --max;
-           }
-
-           prevCtrl = 1;
-           continue;
-       }
-
-       prevCtrl = 0;
 
 
-       charstring_push_back (dest, *str++);
-        --max;
+    squash = true; /* Strip leading cases. */
+    while (max--) {
+        c = (unsigned char)*str++;
+        if (!c)
+            return;
+
+       if (isspace(c) || iscntrl(c)) {
+            if (squash)
+                continue;
+            c = ' ';
+            squash = true;
+        } else
+            squash = false;
+
+       charstring_push_back(dest, (char)c);
     }
 }
 #endif
     }
 }
 #endif