]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
xgettext: Add support for C11 string literals.
authorBruno Haible <bruno@clisp.org>
Sun, 4 Nov 2018 19:19:56 +0000 (20:19 +0100)
committerBruno Haible <bruno@clisp.org>
Sun, 4 Nov 2018 23:25:33 +0000 (00:25 +0100)
* gettext-tools/src/x-c.c (phase5_get): Recognize the u8"..." syntax.
* gettext-tools/tests/xgettext-c-25: New file.
* gettext-tools/tests/Makefile.am (TESTS): Add it.
* NEWS: Mention the change.

NEWS
gettext-tools/src/x-c.c
gettext-tools/tests/Makefile.am
gettext-tools/tests/xgettext-c-25 [new file with mode: 0755]

diff --git a/NEWS b/NEWS
index 219bc280dc9dc9d9c5b6e9d2bd00838ddc3a4244..ed54883ea75e3b587fd53d5c27948e18ae1d86a3 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -11,6 +11,9 @@
   - The .pot file in a 'po' directory is now erased by "make maintainer-clean".
 
 * Programming languages support:
+  - C, C++:
+    xgettext now supports strings in u8"..." syntax, as specified in C11
+    and C++11.
   - C, C++:
     xgettext now supports 'p'/'P' exponent markers in number tokens, as
     specified in C99 and C++17.
index 1b81dfd5f5bfc80ea35783c59a6aeeabd47ed6b6..d8d9d0fc0e22c6f9dfa37ecc2eac730bf0a73026 100644 (file)
@@ -1274,6 +1274,17 @@ phase5_get (token_ty *tp)
             case '5': case '6': case '7': case '8': case '9':
               continue;
 
+            case '"':
+              /* Recognize C11 / C++11 string literals.
+                 See (for C) ISO 9899:2011 section 6.4.5
+                 and (for C++) ISO C++ 11 section 2.14.5 [lex.string].
+                 Note: The programmer who passes an UTF-8 encoded string to
+                 gettext() or similar API functions will have to have called
+                 bind_textdomain_codeset (DOMAIN, "UTF-8") first.  */
+              if (bufpos == 2 && buffer[0] == 'u' && buffer[1] == '8')
+                goto string_literal;
+              /* FALLTHROUGH */
+
             default:
               phase4_ungetc (c);
               break;
@@ -1449,6 +1460,13 @@ phase5_get (token_ty *tp)
       return;
 
     case '"':
+    string_literal:
+      /* We could worry about the 'L' or 'u' or 'U' before wide string
+         constants, but since gettext's argument is a 'const char *', not
+         a 'const wchar_t *' (for 'L') nor a 'const char16_t *' (for 'u')
+         nor a 'const char32_t *' (for 'U'), the compiler would complain
+         about the argument not matching the prototype.  Just pretend it
+         won't happen.  */
       {
         struct mixed_string_buffer *bp;
 
@@ -1457,10 +1475,6 @@ phase5_get (token_ty *tp)
                                         logical_file_name,
                                         line_number);
 
-        /* We could worry about the 'L' before wide string constants,
-           but since gettext's argument is not a wide character string,
-           let the compiler complain about the argument not matching the
-           prototype.  Just pretend it won't happen.  */
         for (;;)
           {
             c = phase7_getc ();
index d79627a48ebabe4f8101de42e346b2f06125206c..9bca237ca651d930020c587c89678ebea67241a3 100644 (file)
@@ -82,7 +82,7 @@ TESTS = gettext-1 gettext-2 \
        xgettext-c-6 xgettext-c-7 xgettext-c-8 xgettext-c-9 xgettext-c-10 \
        xgettext-c-11 xgettext-c-12 xgettext-c-13 xgettext-c-14 xgettext-c-15 \
        xgettext-c-16 xgettext-c-17 xgettext-c-18 xgettext-c-19 xgettext-c-20 \
-       xgettext-c-21 xgettext-c-22 xgettext-c-23 xgettext-c-24 \
+       xgettext-c-21 xgettext-c-22 xgettext-c-23 xgettext-c-24 xgettext-c-25 \
        xgettext-csharp-1 xgettext-csharp-2 xgettext-csharp-3 \
        xgettext-csharp-4 xgettext-csharp-5 xgettext-csharp-6 \
        xgettext-csharp-7 xgettext-csharp-8 \
diff --git a/gettext-tools/tests/xgettext-c-25 b/gettext-tools/tests/xgettext-c-25
new file mode 100755 (executable)
index 0000000..490791b
--- /dev/null
@@ -0,0 +1,44 @@
+#! /bin/sh
+. "${srcdir=.}/init.sh"; path_prepend_ . ../src
+
+# Test C support: C11 string literals.
+
+: ${XGETTEXT=xgettext}
+
+cat <<\EOF > xg-c-25.c
+gettext("Choose a " u8"rosé wine à la carte");
+EOF
+
+${XGETTEXT} --from-code=ISO-8859-1 --no-location \
+            -o xg-c-25.tmp xg-c-25.c || Exit 1
+grep -v 'POT-Creation-Date' < xg-c-25.tmp > xg-c-25.tmq || Exit 1
+LC_ALL=C tr -d '\r' < xg-c-25.tmq > xg-c-25.po || Exit 1
+
+cat <<\EOF > xg-c-25.ok
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+#
+#, fuzzy
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"Report-Msgid-Bugs-To: \n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+"Language-Team: LANGUAGE <LL@li.org>\n"
+"Language: \n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+
+msgid "Choose a rosé wine à la carte"
+msgstr ""
+EOF
+
+: ${DIFF=diff}
+${DIFF} xg-c-25.ok xg-c-25.po
+result=$?
+
+exit $result