/src/gettext/gettext-runtime/intl/plural-exp.c
Line | Count | Source |
1 | | /* Expression parsing for plural form selection. |
2 | | Copyright (C) 2000-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 Lesser General Public License as published by |
6 | | the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details. |
13 | | |
14 | | You should have received a copy of the GNU Lesser General Public License |
15 | | along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
16 | | |
17 | | /* Written by Ulrich Drepper and Bruno Haible. */ |
18 | | |
19 | | #ifdef HAVE_CONFIG_H |
20 | | # include <config.h> |
21 | | #endif |
22 | | |
23 | | #include <ctype.h> |
24 | | #include <stdlib.h> |
25 | | #include <string.h> |
26 | | |
27 | | #include <plural-exp.h> |
28 | | |
29 | | /* These structs are the constant expression for the germanic plural |
30 | | form determination. It represents the expression "n != 1". */ |
31 | | static const struct expression plvar = |
32 | | { |
33 | | .nargs = 0, |
34 | | .operation = var, |
35 | | }; |
36 | | static const struct expression plone = |
37 | | { |
38 | | .nargs = 0, |
39 | | .operation = num, |
40 | | .val = |
41 | | { |
42 | | .num = 1 |
43 | | } |
44 | | }; |
45 | | const struct expression GERMANIC_PLURAL = |
46 | | { |
47 | | .nargs = 2, |
48 | | .operation = not_equal, |
49 | | .val = |
50 | | { |
51 | | .args = |
52 | | { |
53 | | [0] = (struct expression *) &plvar, |
54 | | [1] = (struct expression *) &plone |
55 | | } |
56 | | } |
57 | | }; |
58 | | |
59 | | void |
60 | | EXTRACT_PLURAL_EXPRESSION (const char *nullentry, |
61 | | const struct expression **pluralp, |
62 | | unsigned long int *npluralsp) |
63 | 0 | { |
64 | 0 | if (nullentry != NULL) |
65 | 0 | { |
66 | 0 | const char *plural; |
67 | 0 | const char *nplurals; |
68 | |
|
69 | 0 | plural = strstr (nullentry, "plural="); |
70 | 0 | nplurals = strstr (nullentry, "nplurals="); |
71 | 0 | if (plural == NULL || nplurals == NULL) |
72 | 0 | goto no_plural; |
73 | 0 | else |
74 | 0 | { |
75 | 0 | char *endp; |
76 | 0 | unsigned long int n; |
77 | 0 | struct parse_args args; |
78 | | |
79 | | /* First get the number. */ |
80 | 0 | nplurals += 9; |
81 | 0 | while (*nplurals != '\0' && isspace ((unsigned char) *nplurals)) |
82 | 0 | ++nplurals; |
83 | 0 | if (!(*nplurals >= '0' && *nplurals <= '9')) |
84 | 0 | goto no_plural; |
85 | 0 | n = strtoul (nplurals, &endp, 10); |
86 | 0 | if (nplurals == endp) |
87 | 0 | goto no_plural; |
88 | 0 | *npluralsp = n; |
89 | | |
90 | | /* Due to the restrictions bison imposes onto the interface of the |
91 | | scanner function we have to put the input string and the result |
92 | | passed up from the parser into the same structure which address |
93 | | is passed down to the parser. */ |
94 | 0 | plural += 7; |
95 | 0 | args.cp = plural; |
96 | 0 | if (PLURAL_PARSE (&args) != 0) |
97 | 0 | goto no_plural; |
98 | 0 | *pluralp = args.res; |
99 | 0 | } |
100 | 0 | } |
101 | 0 | else |
102 | 0 | { |
103 | | /* By default we are using the Germanic form: singular form only |
104 | | for `one', the plural form otherwise. Yes, this is also what |
105 | | English is using since English is a Germanic language. */ |
106 | 0 | no_plural: |
107 | 0 | *pluralp = &GERMANIC_PLURAL; |
108 | 0 | *npluralsp = 2; |
109 | 0 | } |
110 | 0 | } |