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