From: Bruno Haible Date: Tue, 26 Mar 2019 22:05:09 +0000 (+0100) Subject: its: Fix test failure. X-Git-Tag: v0.20~110 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=474466b7cf7e168dca376695be60c425580ae195;p=thirdparty%2Fgettext.git its: Fix test failure. * gettext-tools/src/its.c (normalize_whitespace): Fix detection of paragraph boundary. Get rid of initial trim() call. * gettext-tools/tests/xgettext-its-1: Fix typo. Verify removal of whitespace at the end of the last paragraph. * autogen.sh (GNULIB_MODULES_TOOLS_FOR_SRC): Add memchr. --- diff --git a/autogen.sh b/autogen.sh index 7f6a1c027..f67c05ed8 100755 --- a/autogen.sh +++ b/autogen.sh @@ -226,6 +226,7 @@ if ! $skip_gnulib; then locale localename lock + memchr memmove memset minmax diff --git a/gettext-tools/src/its.c b/gettext-tools/src/its.c index c542db476..8b72e1e19 100644 --- a/gettext-tools/src/its.c +++ b/gettext-tools/src/its.c @@ -403,61 +403,78 @@ normalize_whitespace (const char *text, enum its_whitespace_type_ty whitespace) /* Normalize whitespaces within the text, keeping paragraph boundaries. */ { - char *result, *p, *out; - - result = trim (text); - for (p = out = result; *p != '\0';) + char *result = xstrdup (text); + /* Go through the string, shrinking it, reading from *p++ + and writing to *out++. (result <= out <= p.) */ + const char *start_of_paragraph; + char *out; + + out = result; + for (start_of_paragraph = result; *start_of_paragraph != '\0';) { - char *pp, *pend = NULL, *next = NULL; - bool last_ws = false; + const char *end_of_paragraph; + const char *next_paragraph; - /* Find a paragraph boundary. */ - for (pp = p; *pp != '\0';) - { - char *nl = strchrnul (pp, '\n'); - if (*nl == '\0') - { - pend = nl; - next = pend; - break; - } - pp = nl + 1; - pp += strspn (pp, " \t\n"); - if (*pp == '\n') - { - pend = nl; - next = pp + 1; - break; - } - } + /* Find the next paragraph boundary. */ + { + const char *p; - /* Normalize whitespaces in the paragraph. */ - assert (pend != NULL); - for (pp = p; pp < pend; pp++) - if (!(*pp == ' ' || *pp == '\t' || *pp == '\n')) - break; - for (; pp < pend; pp++) - { - if (*pp == ' ' || *pp == '\t' || *pp == '\n') + for (p = start_of_paragraph;;) + { + const char *nl = strchrnul (p, '\n'); + if (*nl == '\0') + { + end_of_paragraph = nl; + next_paragraph = end_of_paragraph; + break; + } + p = nl + 1; { - if (!last_ws) + const char *past_whitespace = p + strspn (p, " \t\n"); + if (memchr (p, '\n', past_whitespace - p) != NULL) { - *out++ = ' '; - last_ws = true; + end_of_paragraph = nl; + next_paragraph = past_whitespace; + break; } + p = past_whitespace; } - else - { - *out++ = *pp; - last_ws = false; - } - } - if (*pend != '\0') + } + } + + /* Normalize whitespaces in the paragraph. */ + { + const char *p; + + /* Remove whitespace at the beginning of the paragraph. */ + for (p = start_of_paragraph; p < end_of_paragraph; p++) + if (!(*p == ' ' || *p == '\t' || *p == '\n')) + break; + + for (; p < end_of_paragraph;) + { + if (*p == ' ' || *p == '\t' || *p == '\n') + { + /* Normalize whitespace inside the paragraph, and + remove whitespace at the end of the paragraph. */ + do + p++; + while (p < end_of_paragraph + && (*p == ' ' || *p == '\t' || *p == '\n')); + if (p < end_of_paragraph) + *out++ = ' '; + } + else + *out++ = *p++; + } + } + + if (*next_paragraph != '\0') { memcpy (out, "\n\n", 2); out += 2; } - p = next; + start_of_paragraph = next_paragraph; } *out = '\0'; return result; diff --git a/gettext-tools/tests/xgettext-its-1 b/gettext-tools/tests/xgettext-its-1 index 975a547cd..22e9163ec 100755 --- a/gettext-tools/tests/xgettext-its-1 +++ b/gettext-tools/tests/xgettext-its-1 @@ -176,10 +176,10 @@ cat <<\EOF >messages.xml This is the first paragraph with a newline. - This is the second paragprah with spaces. + This is the second paragraph with spaces. - This is the last paragraph.

+ This is the last paragraph.

This is the only one paragraph

@@ -277,7 +277,7 @@ msgstr "" msgid "" "This is the first paragraph with a newline.\n" "\n" -"This is the second paragprah with spaces.\n" +"This is the second paragraph with spaces.\n" "\n" "This is the last paragraph." msgstr ""