]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
libgettextpo: New function po_message_get_format.
authorBruno Haible <bruno@clisp.org>
Sat, 27 Sep 2025 11:20:39 +0000 (13:20 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 27 Sep 2025 11:20:39 +0000 (13:20 +0200)
Reported by Alexander Potashev <aspotashev@gmail.com> in
<https://savannah.gnu.org/bugs/?67485>.

* gettext-tools/libgettextpo/gettext-po.in.h (po_message_get_format): New
declaration.
* gettext-tools/libgettextpo/gettext-po.c (po_message_get_format): New function.
* gettext-tools/tests/gettextpo-1-prg.c (main): Test the return value of
po_message_get_format. Add a test case of po_message_set_format with value -1.
* gettext-tools/doc/gettext.texi (po_message_t API): Document
po_message_get_format.
* NEWS: Mention the change.

NEWS
gettext-tools/doc/gettext.texi
gettext-tools/libgettextpo/gettext-po.c
gettext-tools/libgettextpo/gettext-po.in.h
gettext-tools/tests/gettextpo-1-prg.c

diff --git a/NEWS b/NEWS
index 2fb058f0a6ce0adca5d3b0844ecc2b1578a4b096..c28890deb755f71e21577807a7bc4b038150e188 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -7,6 +7,10 @@ Version 0.27 - September 2025
       strings.
     - A new example 'hello-ocaml' has been added.
 
+# libgettextpo library:
+  * The function 'po_message_get_format' now supports distinguishing whether
+    a negative format string mark, such as 'no-c-format', is set or not.
+
 Version 0.26 - July 2025
 
 # Programming languages support:
index 976abe8caada82c557e8e677e999abed2e40e18c..b8e8ff12a8d17e5a8d515c67336c7c14700ab063 100644 (file)
@@ -6557,6 +6557,16 @@ The @code{po_message_is_format} function returns true when the message
 is marked as being a format string of @var{format_type}.
 @end deftypefun
 
+@deftypefun {int} po_message_get_format (po_message_t@tie{}@var{message}, const@tie{}char@tie{}*@var{format_type})
+The @code{po_message_get_format} function returns an indicator
+whether the message is marked as being a format string of @var{format_type}
+or the opposite (@code{no-@var{format_type}}).
+More precisely,
+it returns 1 if the @var{format_type} mark is set,
+0 if the @code{no-@var{format_type}} mark is set,
+or -1 of neither the mark nor the opposite mark is set.
+@end deftypefun
+
 @deftypefun {void} po_message_set_format (po_message_t@tie{}@var{message}, const@tie{}char@tie{}*@var{format_type}, int@tie{}@var{value})
 The @code{po_message_set_format} function changes
 the format string mark of the message for the @var{format_type} provided.
index 94384dd2f6d6105524f463a76ab668275650edbd..c364ae00313ca6527c5ca59615569810e5a3ce04 100644 (file)
@@ -950,11 +950,41 @@ po_message_is_format (po_message_t message, const char *format_type)
       if (strlen (format_language[i]) == len - 7
           && memcmp (format_language[i], format_type, len - 7) == 0)
         /* The given format_type corresponds to (enum format_type) i.  */
+        /* See make_format_description_string in write-po.c.  */
         return (possible_format_p (mp->is_format[i]) ? 1 : 0);
   return 0;
 }
 
 
+/* Return the format string mark for a given type (e.g. "c-format") of a
+   message.  Returns 1 if the the mark is set, 0 if the opposite mark ("no-*")
+   is set, -1 if neither the mark nor the opposite mark is set.  */
+
+int
+po_message_get_format (po_message_t message, const char *format_type)
+{
+  message_ty *mp = (message_ty *) message;
+  size_t len = strlen (format_type);
+  size_t i;
+
+  if (len >= 7 && memcmp (format_type + len - 7, "-format", 7) == 0)
+    for (i = 0; i < NFORMATS; i++)
+      if (strlen (format_language[i]) == len - 7
+          && memcmp (format_language[i], format_type, len - 7) == 0)
+        /* The given format_type corresponds to (enum format_type) i.  */
+        {
+          enum is_format is_format = mp->is_format[i];
+          /* See significant_format_p and make_format_description_string
+             in write-po.c.  */
+          if (is_format != undecided && is_format != impossible)
+            return (possible_format_p (is_format) ? 1 : 0);
+          else
+            return -1;
+        }
+  return -1;
+}
+
+
 /* Change the format string mark for a given type of a message.  */
 
 void
index 8c8f287432135970ecc43faceb29bdf4aa4d5b64..8b2887f39e6a539db73a53f8d29ed0ff2541a766 100644 (file)
@@ -298,6 +298,13 @@ extern void po_message_set_fuzzy (po_message_t message, int fuzzy);
    type (e.g. "c-format").  */
 extern int po_message_is_format (po_message_t message, const char *format_type);
 
+/* Return the format string mark for a given type (e.g. "c-format") of a
+   message.
+   Returns 1 if the the mark is set,
+   0 if the opposite mark ("no-*") is set,
+   -1 if neither the mark nor the opposite mark is set.  */
+extern int po_message_get_format (po_message_t message, const char *format_type);
+
 /* Change the format string mark for a given type of a message.
    Pass value = 1 to assert the format string mark (e.g. "c-format"),
    value = 0 to assert the opposite (leading to e.g. "no-c-format"),
index 1bee8bff2e00556e5a0929bb7f10569ebce72209..eb652366296332501abcfe1f675271e900ebe984 100644 (file)
@@ -254,6 +254,8 @@ main (int argc, char *argv[])
         ASSERT (!po_message_is_fuzzy (msg));
         ASSERT (!po_message_is_format (msg, "c-format"));
         ASSERT (!po_message_is_format (msg, "java-format"));
+        ASSERT (po_message_get_format (msg, "c-format") == -1);
+        ASSERT (po_message_get_format (msg, "java-format") == -1);
         ASSERT (!po_message_is_range (msg, &min, &max));
       }
       {
@@ -283,6 +285,8 @@ main (int argc, char *argv[])
         ASSERT (po_message_is_fuzzy (msg));
         ASSERT (po_message_is_format (msg, "c-format"));
         ASSERT (!po_message_is_format (msg, "java-format"));
+        ASSERT (po_message_get_format (msg, "c-format") == 1);
+        ASSERT (po_message_get_format (msg, "java-format") == -1);
         ASSERT (!po_message_is_range (msg, &min, &max));
       }
       {
@@ -324,6 +328,8 @@ main (int argc, char *argv[])
         ASSERT (!po_message_is_fuzzy (msg));
         ASSERT (po_message_is_format (msg, "c-format"));
         ASSERT (!po_message_is_format (msg, "java-format"));
+        ASSERT (po_message_get_format (msg, "c-format") == 1);
+        ASSERT (po_message_get_format (msg, "java-format") == -1);
         ASSERT (!po_message_is_range (msg, &min, &max));
       }
       {
@@ -346,6 +352,8 @@ main (int argc, char *argv[])
         ASSERT (!po_message_is_fuzzy (msg));
         ASSERT (!po_message_is_format (msg, "c-format"));
         ASSERT (!po_message_is_format (msg, "java-format"));
+        ASSERT (po_message_get_format (msg, "c-format") == -1);
+        ASSERT (po_message_get_format (msg, "java-format") == -1);
         ASSERT (!po_message_is_range (msg, &min, &max));
       }
       {
@@ -375,6 +383,8 @@ main (int argc, char *argv[])
         ASSERT (!po_message_is_fuzzy (msg));
         ASSERT (!po_message_is_format (msg, "c-format"));
         ASSERT (po_message_is_format (msg, "java-format"));
+        ASSERT (po_message_get_format (msg, "c-format") == -1);
+        ASSERT (po_message_get_format (msg, "java-format") == 1);
         ASSERT (!po_message_is_range (msg, &min, &max));
       }
       {
@@ -396,6 +406,8 @@ main (int argc, char *argv[])
         ASSERT (!po_message_is_fuzzy (msg));
         ASSERT (!po_message_is_format (msg, "c-format"));
         ASSERT (!po_message_is_format (msg, "java-format"));
+        ASSERT (po_message_get_format (msg, "c-format") == -1);
+        ASSERT (po_message_get_format (msg, "java-format") == -1);
         ASSERT (!po_message_is_range (msg, &min, &max));
       }
       {
@@ -910,26 +922,57 @@ main (int argc, char *argv[])
     ASSERT (!po_message_is_format (msg, "c-format"));
     ASSERT (!po_message_is_format (msg, "java-format"));
     ASSERT (!po_message_is_format (msg, "xyzzy-format"));
+    ASSERT (po_message_get_format (msg, "c-format") == -1);
+    ASSERT (po_message_get_format (msg, "java-format") == -1);
+    ASSERT (po_message_get_format (msg, "xyzzy-format") == -1);
+
     po_message_set_format (msg, "c-format", 1);
     ASSERT (po_message_is_format (msg, "c-format"));
     ASSERT (!po_message_is_format (msg, "java-format"));
     ASSERT (!po_message_is_format (msg, "xyzzy-format"));
+    ASSERT (po_message_get_format (msg, "c-format") == 1);
+    ASSERT (po_message_get_format (msg, "java-format") == -1);
+    ASSERT (po_message_get_format (msg, "xyzzy-format") == -1);
+
     po_message_set_format (msg, "c-format", 1);
     ASSERT (po_message_is_format (msg, "c-format"));
     ASSERT (!po_message_is_format (msg, "java-format"));
     ASSERT (!po_message_is_format (msg, "xyzzy-format"));
+    ASSERT (po_message_get_format (msg, "c-format") == 1);
+    ASSERT (po_message_get_format (msg, "java-format") == -1);
+    ASSERT (po_message_get_format (msg, "xyzzy-format") == -1);
+
     po_message_set_format (msg, "java-format", 1);
     ASSERT (po_message_is_format (msg, "c-format"));
     ASSERT (po_message_is_format (msg, "java-format"));
     ASSERT (!po_message_is_format (msg, "xyzzy-format"));
+    ASSERT (po_message_get_format (msg, "c-format") == 1);
+    ASSERT (po_message_get_format (msg, "java-format") == 1);
+    ASSERT (po_message_get_format (msg, "xyzzy-format") == -1);
+
     po_message_set_format (msg, "c-format", 0);
     ASSERT (!po_message_is_format (msg, "c-format"));
     ASSERT (po_message_is_format (msg, "java-format"));
     ASSERT (!po_message_is_format (msg, "xyzzy-format"));
+    ASSERT (po_message_get_format (msg, "c-format") == 0);
+    ASSERT (po_message_get_format (msg, "java-format") == 1);
+    ASSERT (po_message_get_format (msg, "xyzzy-format") == -1);
+
     po_message_set_format (msg, "xyzzy-format", 1);
     ASSERT (!po_message_is_format (msg, "c-format"));
     ASSERT (po_message_is_format (msg, "java-format"));
     ASSERT (!po_message_is_format (msg, "xyzzy-format"));
+    ASSERT (po_message_get_format (msg, "c-format") == 0);
+    ASSERT (po_message_get_format (msg, "java-format") == 1);
+    ASSERT (po_message_get_format (msg, "xyzzy-format") == -1);
+
+    po_message_set_format (msg, "java-format", -1);
+    ASSERT (!po_message_is_format (msg, "c-format"));
+    ASSERT (!po_message_is_format (msg, "java-format"));
+    ASSERT (!po_message_is_format (msg, "xyzzy-format"));
+    ASSERT (po_message_get_format (msg, "c-format") == 0);
+    ASSERT (po_message_get_format (msg, "java-format") == -1);
+    ASSERT (po_message_get_format (msg, "xyzzy-format") == -1);
   }
 
   /* Test po_message_set_range.  */