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.
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.
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.
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/>. */
24 #define MAX(a,b) ({ typeof(a) _a = (a); typeof(b) _b = (b); \
31 # define UCHAR_TYPE wint_t
32 # define STRING_TYPE wchar_t
35 # define UCHAR_TYPE unsigned char
36 # define STRING_TYPE char
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. */
47 __correctly_grouped_prefixwc (const STRING_TYPE
*begin
, const STRING_TYPE
*end
,
50 __correctly_grouped_prefixmb (const STRING_TYPE
*begin
, const STRING_TYPE
*end
,
51 const char *thousands
,
64 thousands_len
= strlen (thousands
);
69 const STRING_TYPE
*cp
= end
- 1;
70 const char *gp
= grouping
;
72 /* Check first group. */
79 if (cp
[thousands_len
- 1] == *thousands
)
81 for (cnt
= 1; thousands
[cnt
] != '\0'; ++cnt
)
82 if (thousands
[cnt
] != cp
[thousands_len
- 1 - cnt
])
84 if (thousands
[cnt
] == '\0')
91 /* We allow the representation to contain no grouping at all even if
92 the locale specifies we can have grouping. */
96 if (end
- cp
== (int) *gp
+ 1)
98 /* This group matches the specification. */
100 const STRING_TYPE
*new_end
;
103 /* There is just one complete group. We are done. */
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. */
112 /* Loop while the grouping is correct. */
115 /* Get the next grouping rule. */
118 /* If end is reached use last rule. */
121 /* Skip the thousands separator. */
130 /* No more thousands separators are allowed to follow. */
134 if (*cp
== thousands
)
137 for (cnt
= 0; thousands
[cnt
] != '\0'; ++cnt
)
138 if (thousands
[cnt
] != cp
[thousands_len
- cnt
- 1])
140 if (thousands
[cnt
] == '\0')
147 /* OK, only digits followed. */
152 /* Check the next group. */
153 const STRING_TYPE
*group_end
= cp
;
158 if (*cp
== thousands
)
161 for (cnt
= 0; thousands
[cnt
] != '\0'; ++cnt
)
162 if (thousands
[cnt
] != cp
[thousands_len
- cnt
- 1])
164 if (thousands
[cnt
] == '\0')
170 if (cp
< begin
&& group_end
- cp
<= (int) *gp
)
171 /* Final group is correct. */
174 if (cp
< begin
|| group_end
- cp
!= (int) *gp
)
175 /* Incorrect group. Punt. */
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. */
187 /* Even the first group was wrong; determine maximum shift. */
188 if (end
- cp
> (int) *gp
+ 1)
189 end
= cp
+ (int) *gp
+ 1;
191 /* This number does not fill the first group, but is correct. */
194 /* CP points to a thousands separator character. */
199 return MAX (begin
, end
);