1 /* Internal header for proving correct grouping in strings of numbers.
2 Copyright (C) 1995, 1996, 1997, 1998, 2000 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
24 #define MAX(a,b) ({ typeof(a) _a = (a); typeof(b) _b = (b); \
28 /* Find the maximum prefix of the string between BEGIN and END which
29 satisfies the grouping rules. It is assumed that at least one digit
30 follows BEGIN directly. */
32 static inline const STRING_TYPE
*
33 correctly_grouped_prefix (const STRING_TYPE
*begin
, const STRING_TYPE
*end
,
37 const char *thousands
,
50 thousands_len
= strlen (thousands
);
55 const STRING_TYPE
*cp
= end
- 1;
56 const char *gp
= grouping
;
58 /* Check first group. */
65 if (cp
[thousands_len
- 1] == *thousands
)
67 for (cnt
= 1; thousands
[cnt
] != '\0'; ++cnt
)
68 if (thousands
[cnt
] != cp
[thousands_len
- 1 - cnt
])
70 if (thousands
[cnt
] == '\0')
77 /* We allow the representation to contain no grouping at all even if
78 the locale specifies we can have grouping. */
82 if (end
- cp
== (int) *gp
+ 1)
84 /* This group matches the specification. */
86 const STRING_TYPE
*new_end
;
89 /* There is just one complete group. We are done. */
92 /* CP points to a thousands separator character. The preceding
93 remainder of the string from BEGIN to NEW_END is the part we
94 will consider if there is a grouping error in this trailing
95 portion from CP to END. */
98 /* Loop while the grouping is correct. */
101 /* Get the next grouping rule. */
104 /* If end is reached use last rule. */
107 /* Skip the thousands separator. */
116 /* No more thousands separators are allowed to follow. */
120 if (*cp
== thousands
)
123 for (cnt
= 0; thousands
[cnt
] != '\0'; ++cnt
)
124 if (thousands
[cnt
] != cp
[thousands_len
- cnt
- 1])
126 if (thousands
[cnt
] == '\0')
133 /* OK, only digits followed. */
138 /* Check the next group. */
139 const STRING_TYPE
*group_end
= cp
;
144 if (*cp
== thousands
)
147 for (cnt
= 0; thousands
[cnt
] != '\0'; ++cnt
)
148 if (thousands
[cnt
] != cp
[thousands_len
- cnt
- 1])
150 if (thousands
[cnt
] == '\0')
156 if (cp
< begin
&& group_end
- cp
<= (int) *gp
)
157 /* Final group is correct. */
160 if (cp
< begin
|| group_end
- cp
!= (int) *gp
)
161 /* Incorrect group. Punt. */
166 /* The trailing portion of the string starting at NEW_END
167 contains a grouping error. So we will look for a correctly
168 grouped number in the preceding portion instead. */
173 /* Even the first group was wrong; determine maximum shift. */
174 if (end
- cp
> (int) *gp
+ 1)
175 end
= cp
+ (int) *gp
+ 1;
177 /* This number does not fill the first group, but is correct. */
180 /* CP points to a thousands separator character. */
185 return MAX (begin
, end
);