.
[glibc/history.git] / stdlib / grouping.c
blob4e01dc3bddca03ad227f6ed8a9b17e3e6b86e930
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
19 02111-1307 USA. */
21 #include <limits.h>
22 #include <stddef.h>
23 #include <string.h>
25 #ifndef MAX
26 #define MAX(a,b) ({ typeof(a) _a = (a); typeof(b) _b = (b); \
27 _a > _b ? _a : _b; })
28 #endif
30 #ifdef USE_WIDE_CHAR
31 # include <wctype.h>
32 # define L_(Ch) L##Ch
33 # define UCHAR_TYPE wint_t
34 # define STRING_TYPE wchar_t
35 #else
36 # define L_(Ch) Ch
37 # define UCHAR_TYPE unsigned char
38 # define STRING_TYPE char
39 #endif
41 #include "grouping.h"
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. */
47 const STRING_TYPE *
48 #ifdef USE_WIDE_CHAR
49 __correctly_grouped_prefixwc (const STRING_TYPE *begin, const STRING_TYPE *end,
50 wchar_t thousands,
51 #else
52 __correctly_grouped_prefixmb (const STRING_TYPE *begin, const STRING_TYPE *end,
53 const char *thousands,
54 #endif
55 const char *grouping)
57 #ifndef USE_WIDE_CHAR
58 size_t thousands_len;
59 int cnt;
60 #endif
62 if (grouping == NULL)
63 return end;
65 #ifndef USE_WIDE_CHAR
66 thousands_len = strlen (thousands);
67 #endif
69 while (end > begin)
71 const STRING_TYPE *cp = end - 1;
72 const char *gp = grouping;
74 /* Check first group. */
75 while (cp >= begin)
77 #ifdef USE_WIDE_CHAR
78 if (*cp == thousands)
79 break;
80 #else
81 if (cp[thousands_len - 1] == *thousands)
83 for (cnt = 1; thousands[cnt] != '\0'; ++cnt)
84 if (thousands[cnt] != cp[thousands_len - 1 - cnt])
85 break;
86 if (thousands[cnt] == '\0')
87 break;
89 #endif
90 --cp;
93 /* We allow the representation to contain no grouping at all even if
94 the locale specifies we can have grouping. */
95 if (cp < begin)
96 return end;
98 if (end - cp == (int) *gp + 1)
100 /* This group matches the specification. */
102 const STRING_TYPE *new_end;
104 if (cp < begin)
105 /* There is just one complete group. We are done. */
106 return end;
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. */
112 new_end = cp - 1;
114 /* Loop while the grouping is correct. */
115 while (1)
117 /* Get the next grouping rule. */
118 ++gp;
119 if (*gp == 0)
120 /* If end is reached use last rule. */
121 --gp;
123 /* Skip the thousands separator. */
124 --cp;
126 if (*gp == CHAR_MAX
127 #if CHAR_MIN < 0
128 || *gp < 0
129 #endif
132 /* No more thousands separators are allowed to follow. */
133 while (cp >= begin)
135 #ifdef USE_WIDE_CHAR
136 if (*cp == thousands)
137 break;
138 #else
139 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
140 if (thousands[cnt] != cp[thousands_len - cnt - 1])
141 break;
142 if (thousands[cnt] == '\0')
143 break;
144 #endif
145 --cp;
148 if (cp < begin)
149 /* OK, only digits followed. */
150 return end;
152 else
154 /* Check the next group. */
155 const STRING_TYPE *group_end = cp;
157 while (cp >= begin)
159 #ifdef USE_WIDE_CHAR
160 if (*cp == thousands)
161 break;
162 #else
163 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
164 if (thousands[cnt] != cp[thousands_len - cnt - 1])
165 break;
166 if (thousands[cnt] == '\0')
167 break;
168 #endif
169 --cp;
172 if (cp < begin && group_end - cp <= (int) *gp)
173 /* Final group is correct. */
174 return end;
176 if (cp < begin || group_end - cp != (int) *gp)
177 /* Incorrect group. Punt. */
178 break;
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. */
185 end = new_end;
187 else
189 /* Even the first group was wrong; determine maximum shift. */
190 if (end - cp > (int) *gp + 1)
191 end = cp + (int) *gp + 1;
192 else if (cp < begin)
193 /* This number does not fill the first group, but is correct. */
194 return end;
195 else
196 /* CP points to a thousands separator character. */
197 end = cp;
201 return MAX (begin, end);