From: Ralph Corderoy Date: Sat, 5 Aug 2017 17:20:07 +0000 (+0100) Subject: cpstripped(), single-byte: Simplify logic. X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/8ba2c2e0a5c0e2c087cd1684a885b959c101e010 cpstripped(), single-byte: Simplify logic. 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. --- diff --git a/sbr/fmt_scan.c b/sbr/fmt_scan.c index 83e48b8b..e5e1ff85 100644 --- a/sbr/fmt_scan.c +++ b/sbr/fmt_scan.c @@ -299,38 +299,27 @@ 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; - } - - 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