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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 #define MAX(a,b) ({ typeof(a) _a = (a); typeof(b) _b = (b); \
33 # define UCHAR_TYPE wint_t
34 # define STRING_TYPE wchar_t
37 # define UCHAR_TYPE unsigned char
38 # define STRING_TYPE char
43 /* Find the maximum prefix of the string between BEGIN and END which
44 satisfies the grouping rules. It is assumed that at least one digit
45 follows BEGIN directly. */
49 __correctly_grouped_prefixwc (const STRING_TYPE
*begin
, const STRING_TYPE
*end
,
52 __correctly_grouped_prefixmb (const STRING_TYPE
*begin
, const STRING_TYPE
*end
,
53 const char *thousands
,
66 thousands_len
= strlen (thousands
);
71 const STRING_TYPE
*cp
= end
- 1;
72 const char *gp
= grouping
;
74 /* Check first group. */
81 if (cp
[thousands_len
- 1] == *thousands
)
83 for (cnt
= 1; thousands
[cnt
] != '\0'; ++cnt
)
84 if (thousands
[cnt
] != cp
[thousands_len
- 1 - cnt
])
86 if (thousands
[cnt
] == '\0')
93 /* We allow the representation to contain no grouping at all even if
94 the locale specifies we can have grouping. */
98 if (end
- cp
== (int) *gp
+ 1)
100 /* This group matches the specification. */
102 const STRING_TYPE
*new_end
;
105 /* There is just one complete group. We are done. */
108 /* CP points to a thousands separator character. The preceding
109 remainder of the string from BEGIN to NEW_END is the part we
110 will consider if there is a grouping error in this trailing
111 portion from CP to END. */
114 /* Loop while the grouping is correct. */
117 /* Get the next grouping rule. */
120 /* If end is reached use last rule. */
123 /* Skip the thousands separator. */
132 /* No more thousands separators are allowed to follow. */
136 if (*cp
== thousands
)
139 for (cnt
= 0; thousands
[cnt
] != '\0'; ++cnt
)
140 if (thousands
[cnt
] != cp
[thousands_len
- cnt
- 1])
142 if (thousands
[cnt
] == '\0')
149 /* OK, only digits followed. */
154 /* Check the next group. */
155 const STRING_TYPE
*group_end
= cp
;
160 if (*cp
== thousands
)
163 for (cnt
= 0; thousands
[cnt
] != '\0'; ++cnt
)
164 if (thousands
[cnt
] != cp
[thousands_len
- cnt
- 1])
166 if (thousands
[cnt
] == '\0')
172 if (cp
< begin
&& group_end
- cp
<= (int) *gp
)
173 /* Final group is correct. */
176 if (cp
< begin
|| group_end
- cp
!= (int) *gp
)
177 /* Incorrect group. Punt. */
182 /* The trailing portion of the string starting at NEW_END
183 contains a grouping error. So we will look for a correctly
184 grouped number in the preceding portion instead. */
189 /* Even the first group was wrong; determine maximum shift. */
190 if (end
- cp
> (int) *gp
+ 1)
191 end
= cp
+ (int) *gp
+ 1;
193 /* This number does not fill the first group, but is correct. */
196 /* CP points to a thousands separator character. */
201 return MAX (begin
, end
);