1 /* Internal header for proving correct grouping in strings of numbers.
2 Copyright (C) 1995,1996,1997,1998,2000,2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
25 #define MAX(a,b) ({ typeof(a) _a = (a); typeof(b) _b = (b); \
32 # define UCHAR_TYPE wint_t
33 # define STRING_TYPE wchar_t
36 # define UCHAR_TYPE unsigned char
37 # define STRING_TYPE char
42 /* Find the maximum prefix of the string between BEGIN and END which
43 satisfies the grouping rules. It is assumed that at least one digit
44 follows BEGIN directly. */
48 __correctly_grouped_prefixwc (const STRING_TYPE
*begin
, const STRING_TYPE
*end
,
51 __correctly_grouped_prefixmb (const STRING_TYPE
*begin
, const STRING_TYPE
*end
,
52 const char *thousands
,
65 thousands_len
= strlen (thousands
);
70 const STRING_TYPE
*cp
= end
- 1;
71 const char *gp
= grouping
;
73 /* Check first group. */
80 if (cp
[thousands_len
- 1] == *thousands
)
82 for (cnt
= 1; thousands
[cnt
] != '\0'; ++cnt
)
83 if (thousands
[cnt
] != cp
[thousands_len
- 1 - cnt
])
85 if (thousands
[cnt
] == '\0')
92 /* We allow the representation to contain no grouping at all even if
93 the locale specifies we can have grouping. */
97 if (end
- cp
== (int) *gp
+ 1)
99 /* This group matches the specification. */
101 const STRING_TYPE
*new_end
;
104 /* There is just one complete group. We are done. */
107 /* CP points to a thousands separator character. The preceding
108 remainder of the string from BEGIN to NEW_END is the part we
109 will consider if there is a grouping error in this trailing
110 portion from CP to END. */
113 /* Loop while the grouping is correct. */
116 /* Get the next grouping rule. */
119 /* If end is reached use last rule. */
122 /* Skip the thousands separator. */
131 /* No more thousands separators are allowed to follow. */
135 if (*cp
== thousands
)
138 for (cnt
= 0; thousands
[cnt
] != '\0'; ++cnt
)
139 if (thousands
[cnt
] != cp
[thousands_len
- cnt
- 1])
141 if (thousands
[cnt
] == '\0')
148 /* OK, only digits followed. */
153 /* Check the next group. */
154 const STRING_TYPE
*group_end
= cp
;
159 if (*cp
== thousands
)
162 for (cnt
= 0; thousands
[cnt
] != '\0'; ++cnt
)
163 if (thousands
[cnt
] != cp
[thousands_len
- cnt
- 1])
165 if (thousands
[cnt
] == '\0')
171 if (cp
< begin
&& group_end
- cp
<= (int) *gp
)
172 /* Final group is correct. */
175 if (cp
< begin
|| group_end
- cp
!= (int) *gp
)
176 /* Incorrect group. Punt. */
181 /* The trailing portion of the string starting at NEW_END
182 contains a grouping error. So we will look for a correctly
183 grouped number in the preceding portion instead. */
188 /* Even the first group was wrong; determine maximum shift. */
189 if (end
- cp
> (int) *gp
+ 1)
190 end
= cp
+ (int) *gp
+ 1;
192 /* This number does not fill the first group, but is correct. */
195 /* CP points to a thousands separator character. */
200 return MAX (begin
, end
);