From: Bruno Haible Date: Sun, 1 Jun 2025 23:01:50 +0000 (+0200) Subject: xgettext: Go: Improve heuristic for format strings. X-Git-Tag: v0.26~133 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=417ca6acd579d18c16a0313e157cc08f3e6af271;p=thirdparty%2Fgettext.git xgettext: Go: Improve heuristic for format strings. * gettext-tools/src/format-go.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_go): Use it. * gettext-tools/tests/format-go-3: New file. * gettext-tools/tests/Makefile.am (TESTS): Add it. --- diff --git a/gettext-tools/src/format-go.c b/gettext-tools/src/format-go.c index 259a3b397..dcf88e127 100644 --- a/gettext-tools/src/format-go.c +++ b/gettext-tools/src/format-go.c @@ -93,6 +93,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 'go-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; }; @@ -121,6 +126,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; allocated = 0; @@ -131,6 +137,7 @@ format_parse (const char *format, bool translated, char *fdi, { /* A directive. */ enum format_arg_type type; + bool likely_intentional = true; FDI_SET (format - 1, FMTDIR_START); spec.directives++; @@ -138,7 +145,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++; + } if (*format == '[') { @@ -452,6 +463,8 @@ format_parse (const char *format, bool translated, char *fdi, number++; } + if (likely_intentional) + spec.likely_intentional_directives++; FDI_SET (format, FMTDIR_END); format++; @@ -529,6 +542,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, @@ -619,7 +640,7 @@ struct formatstring_parser formatstring_go = format_parse, format_free, format_get_number_of_directives, - NULL, + format_is_unlikely_intentional, format_check }; diff --git a/gettext-tools/tests/Makefile.am b/gettext-tools/tests/Makefile.am index b35ae6066..8d70da071 100644 --- a/gettext-tools/tests/Makefile.am +++ b/gettext-tools/tests/Makefile.am @@ -204,7 +204,7 @@ TESTS = gettext-1 gettext-2 \ format-elisp-1 format-elisp-2 format-elisp-3 \ format-gcc-internal-1 format-gcc-internal-2 \ format-gfc-internal-1 format-gfc-internal-2 \ - format-go-1 format-go-2 \ + format-go-1 format-go-2 format-go-3 \ format-java-1 format-java-2 \ format-java-printf-1 format-java-printf-2 \ format-javascript-1 format-javascript-2 \ diff --git a/gettext-tools/tests/format-go-3 b/gettext-tools/tests/format-go-3 new file mode 100755 index 000000000..a15fbe9b8 --- /dev/null +++ b/gettext-tools/tests/format-go-3 @@ -0,0 +1,51 @@ +#! /bin/sh +. "${srcdir=.}/init.sh"; path_prepend_ . ../src + +# Test heuristic recognition of Go format strings like "100% complete". + +cat <<\EOF > f-go-3.go +import "fmt" +func main () { + Gettext("Test 1a is 100% complete"); + fmt.Println(Gettext("Test 1b is 100% complete"), 120); + Gettext("Test 2a from 0% complete to 100% complete"); + fmt.Println(Gettext("Test 2b from 0% complete to 100% complete"), 120, 121); + Gettext("Test 3a of %s is 100% complete"); + fmt.Println(Gettext("Test 3b of %s is 100% complete"), "foo", 120); +} +EOF + +: ${XGETTEXT=xgettext} +${XGETTEXT} --flag=Println:1:go-format \ + --omit-header --no-location -d f-go-3.tmp f-go-3.go || Exit 1 +LC_ALL=C tr -d '\r' < f-go-3.tmp.po > f-go-3.po || Exit 1 + +cat <<\EOF > f-go-3.ok +msgid "Test 1a is 100% complete" +msgstr "" + +#, go-format +msgid "Test 1b is 100% complete" +msgstr "" + +msgid "Test 2a from 0% complete to 100% complete" +msgstr "" + +#, go-format +msgid "Test 2b from 0% complete to 100% complete" +msgstr "" + +#, go-format +msgid "Test 3a of %s is 100% complete" +msgstr "" + +#, go-format +msgid "Test 3b of %s is 100% complete" +msgstr "" +EOF + +: ${DIFF=diff} +${DIFF} f-go-3.ok f-go-3.po +result=$? + +exit $result