]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
xgettext: Tcl: Improve heuristic for format strings.
authorBruno Haible <bruno@clisp.org>
Sun, 1 Jun 2025 23:39:20 +0000 (01:39 +0200)
committerBruno Haible <bruno@clisp.org>
Mon, 2 Jun 2025 00:22:57 +0000 (02:22 +0200)
* gettext-tools/src/format-tcl.c (struct spec): Add field
'likely_intentional_directives'.
(format_parse): Set it to the number of directives that don't contain a space.
(format_is_unlikely_intentional): New function.
(formatstring_tcl): Use it.
* gettext-tools/tests/format-tcl-3: New file.
* gettext-tools/tests/Makefile.am (TESTS): Add it.

gettext-tools/src/format-tcl.c
gettext-tools/tests/Makefile.am
gettext-tools/tests/format-tcl-3 [new file with mode: 0755]

index 8dbdee2c1816ddbab7cbeb247660dc84fae2ff7b..c98a4646929c96b9a2000899cb96595c427f2d46 100644 (file)
@@ -75,6 +75,11 @@ struct numbered_arg
 struct spec
 {
   unsigned int directives;
+  /* We consider a directive as "likely intentional" if it does not contain a
+     space.  This prevents xgettext from flagging strings like "100% complete"
+     as 'tcl-format' if they don't occur in a context that requires a format
+     string.  */
+  unsigned int likely_intentional_directives;
   unsigned int numbered_arg_count;
   struct numbered_arg *numbered;
 };
@@ -102,6 +107,7 @@ format_parse (const char *format, bool translated, char *fdi,
   unsigned int number;
 
   spec.directives = 0;
+  spec.likely_intentional_directives = 0;
   spec.numbered_arg_count = 0;
   spec.numbered = NULL;
   numbered_allocated = 0;
@@ -114,6 +120,8 @@ format_parse (const char *format, bool translated, char *fdi,
     if (*format++ == '%')
       {
         /* A directive.  */
+        bool likely_intentional = true;
+
         FDI_SET (format - 1, FMTDIR_START);
         spec.directives++;
 
@@ -174,7 +182,11 @@ format_parse (const char *format, bool translated, char *fdi,
             /* Parse flags.  */
             while (*format == ' ' || *format == '+' || *format == '-'
                    || *format == '#' || *format == '0')
-              format++;
+              {
+                if (*format == ' ')
+                  likely_intentional = false;
+                format++;
+              }
 
             /* Parse width.  */
             if (*format == '*')
@@ -274,6 +286,8 @@ format_parse (const char *format, bool translated, char *fdi,
             number++;
           }
 
+        if (likely_intentional)
+          spec.likely_intentional_directives++;
         FDI_SET (format, FMTDIR_END);
 
         format++;
@@ -354,6 +368,14 @@ format_get_number_of_directives (void *descr)
   return spec->directives;
 }
 
+static bool
+format_is_unlikely_intentional (void *descr)
+{
+  struct spec *spec = (struct spec *) descr;
+
+  return spec->likely_intentional_directives == 0;
+}
+
 static bool
 format_check (void *msgid_descr, void *msgstr_descr, bool equality,
               formatstring_error_logger_t error_logger, void *error_logger_data,
@@ -438,7 +460,7 @@ struct formatstring_parser formatstring_tcl =
   format_parse,
   format_free,
   format_get_number_of_directives,
-  NULL,
+  format_is_unlikely_intentional,
   format_check
 };
 
index 7958785180d19e93b8498327514d54b3b81869d1..91b1e1ad03cb9329b7ec5cc9c5824a199fc3c6f1 100644 (file)
@@ -227,7 +227,7 @@ TESTS = gettext-1 gettext-2 \
        format-rust-1 format-rust-2 \
        format-scheme-1 format-scheme-2 \
        format-sh-1 format-sh-2 \
-       format-tcl-1 format-tcl-2 \
+       format-tcl-1 format-tcl-2 format-tcl-3 \
        format-ycp-1 format-ycp-2 \
        plural-1 plural-2 plural-3 plural-4 \
        gettextpo-1 sentence-1 \
diff --git a/gettext-tools/tests/format-tcl-3 b/gettext-tools/tests/format-tcl-3
new file mode 100755 (executable)
index 0000000..aa652a8
--- /dev/null
@@ -0,0 +1,47 @@
+#! /bin/sh
+. "${srcdir=.}/init.sh"; path_prepend_ . ../src
+
+# Test heuristic recognition of Tcl format strings like "100% complete".
+
+cat <<\EOF > f-t-3.tcl
+[msgcat::mc "Test 1a is 100% complete"]
+[format [msgcat::mc "Test 1b is 100% complete"), 120]]
+[msgcat::mc "Test 2a from 0% complete to 100% complete"]
+[format [msgcat::mc "Test 2b from 0% complete to 100% complete"), 120, 121]]
+[msgcat::mc "Test 3a of %s is 100% complete"]
+[format [msgcat::mc "Test 3b of %s is 100% complete"), "foo", 120]]
+EOF
+
+: ${XGETTEXT=xgettext}
+${XGETTEXT} --omit-header --no-location -d f-t-3.tmp f-t-3.tcl || Exit 1
+LC_ALL=C tr -d '\r' < f-t-3.tmp.po > f-t-3.po || Exit 1
+
+cat <<\EOF > f-t-3.ok
+msgid "Test 1a is 100% complete"
+msgstr ""
+
+#, tcl-format
+msgid "Test 1b is 100% complete"
+msgstr ""
+
+msgid "Test 2a from 0% complete to 100% complete"
+msgstr ""
+
+#, tcl-format
+msgid "Test 2b from 0% complete to 100% complete"
+msgstr ""
+
+#, tcl-format
+msgid "Test 3a of %s is 100% complete"
+msgstr ""
+
+#, tcl-format
+msgid "Test 3b of %s is 100% complete"
+msgstr ""
+EOF
+
+: ${DIFF=diff}
+${DIFF} f-t-3.ok f-t-3.po
+result=$?
+
+exit $result