Coverage Report

Created: 2026-03-12 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gettext/gettext-tools/src/format-qt-plural.c
Line
Count
Source
1
/* Qt plural format strings.
2
   Copyright (C) 2003-2026 Free Software Foundation, Inc.
3
4
   This program is free software: you can redistribute it and/or modify
5
   it under the terms of the GNU General Public License as published by
6
   the Free Software Foundation; either version 3 of the License, or
7
   (at your option) any later version.
8
9
   This program is distributed in the hope that it will be useful,
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
   GNU General Public License for more details.
13
14
   You should have received a copy of the GNU General Public License
15
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
16
17
/* Written by Bruno Haible.  */
18
19
#include <config.h>
20
21
#include <stdbool.h>
22
#include <stdlib.h>
23
24
#include "format.h"
25
#include "xalloc.h"
26
#include "gettext.h"
27
28
0
#define _(str) gettext (str)
29
30
/* Qt plural format strings are processed by QObject::tr and are documented in
31
   qt-x11-opensource-src-4.3.1/doc/html/qobject.html#tr.
32
   A directive
33
     - starts with '%',
34
     - is optionally followed by 'L' (a no-op),
35
     - is followed by 'n'.
36
   Every directive is replaced by the numeric argument N passed to QObject::tr.
37
 */
38
39
struct spec
40
{
41
  /* Number of format directives.  */
42
  size_t directives;
43
};
44
45
46
static void *
47
format_parse (const char *format, bool translated, char *fdi,
48
              char **invalid_reason)
49
0
{
50
0
  const char *const format_start = format;
51
52
0
  struct spec spec;
53
0
  spec.directives = 0;
54
55
0
  for (; *format != '\0';)
56
0
    if (*format++ == '%')
57
0
      {
58
0
        const char *dir_start = format - 1;
59
60
0
        if (*format == 'L')
61
0
          format++;
62
0
        if (*format == 'n')
63
0
          {
64
            /* A directive.  */
65
0
            FDI_SET (dir_start, FMTDIR_START);
66
0
            spec.directives++;
67
0
            FDI_SET (format, FMTDIR_END);
68
69
0
            format++;
70
0
          }
71
0
      }
72
73
0
  struct spec *result = XMALLOC (struct spec);
74
0
  *result = spec;
75
0
  return result;
76
0
}
77
78
static void
79
format_free (void *descr)
80
0
{
81
0
  struct spec *spec = (struct spec *) descr;
82
83
0
  free (spec);
84
0
}
85
86
static int
87
format_get_number_of_directives (void *descr)
88
0
{
89
0
  struct spec *spec = (struct spec *) descr;
90
91
0
  return spec->directives;
92
0
}
93
94
static bool
95
format_check (void *msgid_descr, void *msgstr_descr, bool equality,
96
              formatstring_error_logger_t error_logger, void *error_logger_data,
97
              const char *pretty_msgid, const char *pretty_msgstr)
98
0
{
99
0
  struct spec *spec1 = (struct spec *) msgid_descr;
100
0
  struct spec *spec2 = (struct spec *) msgstr_descr;
101
0
  bool err = false;
102
103
  /* Check the argument is used.  */
104
0
  if ((spec1->directives == 0 && spec2->directives > 0)
105
0
      || (equality && spec1->directives > 0 && spec2->directives == 0))
106
0
    {
107
0
      if (error_logger)
108
0
        error_logger (error_logger_data,
109
0
                      _("number of format specifications in '%s' and '%s' does not match"),
110
0
                      pretty_msgid, pretty_msgstr);
111
0
      err = true;
112
0
    }
113
114
0
  return err;
115
0
}
116
117
118
struct formatstring_parser formatstring_qt_plural =
119
{
120
  format_parse,
121
  format_free,
122
  format_get_number_of_directives,
123
  NULL,
124
  format_check
125
};
126
127
128
#ifdef TEST
129
130
/* Test program: Print the argument list specification returned by
131
   format_parse for strings read from standard input.  */
132
133
#include <stdio.h>
134
135
static void
136
format_print (void *descr)
137
{
138
  struct spec *spec = (struct spec *) descr;
139
140
  if (spec == NULL)
141
    {
142
      printf ("INVALID");
143
      return;
144
    }
145
146
  printf ("(");
147
  if (spec->directives > 0)
148
    printf ("*");
149
  else
150
    printf ("_");
151
  printf (")");
152
}
153
154
int
155
main ()
156
{
157
  for (;;)
158
    {
159
      char *line = NULL;
160
      size_t line_size = 0;
161
      int line_len = getline (&line, &line_size, stdin);
162
      if (line_len < 0)
163
        break;
164
      if (line_len > 0 && line[line_len - 1] == '\n')
165
        line[--line_len] = '\0';
166
167
      char *invalid_reason = NULL;
168
      void *descr = format_parse (line, false, NULL, &invalid_reason);
169
170
      format_print (descr);
171
      printf ("\n");
172
      if (descr == NULL)
173
        printf ("%s\n", invalid_reason);
174
175
      free (invalid_reason);
176
      free (line);
177
    }
178
179
  return 0;
180
}
181
182
/*
183
 * For Emacs M-x compile
184
 * Local Variables:
185
 * compile-command: "/bin/sh ../libtool --tag=CC --mode=link gcc -o a.out -static -O -g -Wall -I.. -I../gnulib-lib -I../../gettext-runtime/intl -DTEST format-qt-plural.c ../gnulib-lib/libgettextlib.la"
186
 * End:
187
 */
188
189
#endif /* TEST */