From: Daiki Ueno Date: Fri, 15 Feb 2019 11:49:01 +0000 (+0100) Subject: its: Make whitespace normalization logic simpler X-Git-Tag: v0.20~159 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=89e7d2663bd27b8cdd300f26095f49d2c7519030;p=thirdparty%2Fgettext.git its: Make whitespace normalization logic simpler * gettext-tools/src/its.c (normalize_whitespace): Use parallel pointers for normalizing whitespaces, instead of memmove. Suggested by Bruno Haible. --- diff --git a/gettext-tools/src/its.c b/gettext-tools/src/its.c index 9b4b397e5..8b31e0e1e 100644 --- a/gettext-tools/src/its.c +++ b/gettext-tools/src/its.c @@ -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; } }