1 | /* Internal header for proving correct grouping in strings of numbers. |
2 | Copyright (C) 1995-2022 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library 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 GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <limits.h> |
20 | #include <stddef.h> |
21 | #include <string.h> |
22 | |
23 | #ifndef MAX |
24 | #define MAX(a,b) ({ typeof(a) _a = (a); typeof(b) _b = (b); \ |
25 | _a > _b ? _a : _b; }) |
26 | #endif |
27 | |
28 | #ifdef USE_WIDE_CHAR |
29 | # include <wctype.h> |
30 | # define L_(Ch) L##Ch |
31 | # define UCHAR_TYPE wint_t |
32 | # define STRING_TYPE wchar_t |
33 | #else |
34 | # define L_(Ch) Ch |
35 | # define UCHAR_TYPE unsigned char |
36 | # define STRING_TYPE char |
37 | #endif |
38 | |
39 | #include "grouping.h" |
40 | |
41 | /* Find the maximum prefix of the string between BEGIN and END which |
42 | satisfies the grouping rules. It is assumed that at least one digit |
43 | follows BEGIN directly. */ |
44 | |
45 | const STRING_TYPE * |
46 | #ifdef USE_WIDE_CHAR |
47 | __correctly_grouped_prefixwc (const STRING_TYPE *begin, const STRING_TYPE *end, |
48 | wchar_t thousands, |
49 | #else |
50 | __correctly_grouped_prefixmb (const STRING_TYPE *begin, const STRING_TYPE *end, |
51 | const char *thousands, |
52 | #endif |
53 | const char *grouping) |
54 | { |
55 | #ifndef USE_WIDE_CHAR |
56 | size_t thousands_len; |
57 | int cnt; |
58 | #endif |
59 | |
60 | if (grouping == NULL) |
61 | return end; |
62 | |
63 | #ifndef USE_WIDE_CHAR |
64 | thousands_len = strlen (thousands); |
65 | #endif |
66 | |
67 | while (end > begin) |
68 | { |
69 | const STRING_TYPE *cp = end - 1; |
70 | const char *gp = grouping; |
71 | |
72 | /* Check first group. */ |
73 | while (cp >= begin) |
74 | { |
75 | #ifdef USE_WIDE_CHAR |
76 | if (*cp == thousands) |
77 | break; |
78 | #else |
79 | if (cp[thousands_len - 1] == *thousands) |
80 | { |
81 | for (cnt = 1; thousands[cnt] != '\0'; ++cnt) |
82 | if (thousands[cnt] != cp[thousands_len - 1 - cnt]) |
83 | break; |
84 | if (thousands[cnt] == '\0') |
85 | break; |
86 | } |
87 | #endif |
88 | --cp; |
89 | } |
90 | |
91 | /* We allow the representation to contain no grouping at all even if |
92 | the locale specifies we can have grouping. */ |
93 | if (cp < begin) |
94 | return end; |
95 | |
96 | if (end - cp == (int) *gp + 1) |
97 | { |
98 | /* This group matches the specification. */ |
99 | |
100 | const STRING_TYPE *new_end; |
101 | |
102 | if (cp < begin) |
103 | /* There is just one complete group. We are done. */ |
104 | return end; |
105 | |
106 | /* CP points to a thousands separator character. The preceding |
107 | remainder of the string from BEGIN to NEW_END is the part we |
108 | will consider if there is a grouping error in this trailing |
109 | portion from CP to END. */ |
110 | new_end = cp - 1; |
111 | |
112 | /* Loop while the grouping is correct. */ |
113 | while (1) |
114 | { |
115 | /* Get the next grouping rule. */ |
116 | ++gp; |
117 | if (*gp == 0) |
118 | /* If end is reached use last rule. */ |
119 | --gp; |
120 | |
121 | /* Skip the thousands separator. */ |
122 | --cp; |
123 | |
124 | if (*gp == CHAR_MAX |
125 | #if CHAR_MIN < 0 |
126 | || *gp < 0 |
127 | #endif |
128 | ) |
129 | { |
130 | /* No more thousands separators are allowed to follow. */ |
131 | while (cp >= begin) |
132 | { |
133 | #ifdef USE_WIDE_CHAR |
134 | if (*cp == thousands) |
135 | break; |
136 | #else |
137 | for (cnt = 0; thousands[cnt] != '\0'; ++cnt) |
138 | if (thousands[cnt] != cp[thousands_len - cnt - 1]) |
139 | break; |
140 | if (thousands[cnt] == '\0') |
141 | break; |
142 | #endif |
143 | --cp; |
144 | } |
145 | |
146 | if (cp < begin) |
147 | /* OK, only digits followed. */ |
148 | return end; |
149 | } |
150 | else |
151 | { |
152 | /* Check the next group. */ |
153 | const STRING_TYPE *group_end = cp; |
154 | |
155 | while (cp >= begin) |
156 | { |
157 | #ifdef USE_WIDE_CHAR |
158 | if (*cp == thousands) |
159 | break; |
160 | #else |
161 | for (cnt = 0; thousands[cnt] != '\0'; ++cnt) |
162 | if (thousands[cnt] != cp[thousands_len - cnt - 1]) |
163 | break; |
164 | if (thousands[cnt] == '\0') |
165 | break; |
166 | #endif |
167 | --cp; |
168 | } |
169 | |
170 | if (cp < begin && group_end - cp <= (int) *gp) |
171 | /* Final group is correct. */ |
172 | return end; |
173 | |
174 | if (cp < begin || group_end - cp != (int) *gp) |
175 | /* Incorrect group. Punt. */ |
176 | break; |
177 | } |
178 | } |
179 | |
180 | /* The trailing portion of the string starting at NEW_END |
181 | contains a grouping error. So we will look for a correctly |
182 | grouped number in the preceding portion instead. */ |
183 | end = new_end; |
184 | } |
185 | else |
186 | { |
187 | /* Even the first group was wrong; determine maximum shift. */ |
188 | if (end - cp > (int) *gp + 1) |
189 | end = cp + (int) *gp + 1; |
190 | else if (cp < begin) |
191 | /* This number does not fill the first group, but is correct. */ |
192 | return end; |
193 | else |
194 | /* CP points to a thousands separator character. */ |
195 | end = cp; |
196 | } |
197 | } |
198 | |
199 | return MAX (begin, end); |
200 | } |
201 | |