/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 = strstr (nullentry, "plural="); |
67 | 0 | const char *nplurals = strstr (nullentry, "nplurals="); |
68 | 0 | if (plural == NULL || nplurals == NULL) |
69 | 0 | goto no_plural; |
70 | 0 | else |
71 | 0 | { |
72 | | /* First get the number. */ |
73 | 0 | nplurals += 9; |
74 | 0 | while (*nplurals != '\0' && isspace ((unsigned char) *nplurals)) |
75 | 0 | ++nplurals; |
76 | 0 | if (!(*nplurals >= '0' && *nplurals <= '9')) |
77 | 0 | goto no_plural; |
78 | 0 | char *endp; |
79 | 0 | unsigned long int n = strtoul (nplurals, &endp, 10); |
80 | 0 | if (nplurals == endp) |
81 | 0 | goto no_plural; |
82 | 0 | *npluralsp = n; |
83 | | |
84 | | /* Due to the restrictions bison imposes onto the interface of the |
85 | | scanner function we have to put the input string and the result |
86 | | passed up from the parser into the same structure which address |
87 | | is passed down to the parser. */ |
88 | 0 | plural += 7; |
89 | 0 | struct parse_args args; |
90 | 0 | args.cp = plural; |
91 | 0 | if (PLURAL_PARSE (&args) != 0) |
92 | 0 | goto no_plural; |
93 | 0 | *pluralp = args.res; |
94 | 0 | } |
95 | 0 | } |
96 | 0 | else |
97 | 0 | { |
98 | | /* By default we are using the Germanic form: singular form only |
99 | | for `one', the plural form otherwise. Yes, this is also what |
100 | | English is using since English is a Germanic language. */ |
101 | 0 | no_plural: |
102 | 0 | *pluralp = &GERMANIC_PLURAL; |
103 | 0 | *npluralsp = 2; |
104 | 0 | } |
105 | 0 | } |