* sysdeps/m68k/dl-machine.h (_dl_start_user): Pass correct
[glibc.git] / stdlib / grouping.h
blobca760c7978f3af32267ede7076cabe6ddf863f61
1 /* Internal header for proving correct grouping in strings of numbers.
2 Copyright (C) 1995, 1996, 1997, 1998, 2000 Free Software Foundation, Inc.
3 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <limits.h>
22 #ifndef MAX
23 #define MAX(a,b) ({ typeof(a) _a = (a); typeof(b) _b = (b); \
24 _a > _b ? _a : _b; })
25 #endif
27 /* Find the maximum prefix of the string between BEGIN and END which
28 satisfies the grouping rules. It is assumed that at least one digit
29 follows BEGIN directly. */
31 static inline const STRING_TYPE *
32 correctly_grouped_prefix (const STRING_TYPE *begin, const STRING_TYPE *end,
33 #ifdef USE_WIDE_CHAR
34 wchar_t thousands,
35 #else
36 const char *thousands,
37 #endif
38 const char *grouping)
40 #ifndef USE_WIDE_CHAR
41 size_t thousands_len;
42 int cnt;
43 #endif
45 if (grouping == NULL)
46 return end;
48 #ifndef USE_WIDE_CHAR
49 thousands_len = strlen (thousands);
50 #endif
52 while (end > begin)
54 const STRING_TYPE *cp = end - 1;
55 const char *gp = grouping;
57 /* Check first group. */
58 while (cp >= begin)
60 #ifdef USE_WIDE_CHAR
61 if (*cp == thousands)
62 break;
63 #else
64 if (cp[thousands_len - 1] == *thousands)
66 for (cnt = 1; thousands[cnt] != '\0'; ++cnt)
67 if (thousands[cnt] != cp[thousands_len - 1 - cnt])
68 break;
69 if (thousands[cnt] == '\0')
70 break;
72 #endif
73 --cp;
76 /* We allow the representation to contain no grouping at all even if
77 the locale specifies we can have grouping. */
78 if (cp < begin)
79 return end;
81 if (end - cp == (int) *gp + 1)
83 /* This group matches the specification. */
85 const STRING_TYPE *new_end;
87 if (cp < begin)
88 /* There is just one complete group. We are done. */
89 return end;
91 /* CP points to a thousands separator character. The preceding
92 remainder of the string from BEGIN to NEW_END is the part we
93 will consider if there is a grouping error in this trailing
94 portion from CP to END. */
95 new_end = cp - 1;
97 /* Loop while the grouping is correct. */
98 while (1)
100 /* Get the next grouping rule. */
101 ++gp;
102 if (*gp == 0)
103 /* If end is reached use last rule. */
104 --gp;
106 /* Skip the thousands separator. */
107 --cp;
109 if (*gp == CHAR_MAX
110 #if CHAR_MIN < 0
111 || *gp < 0
112 #endif
115 /* No more thousands separators are allowed to follow. */
116 while (cp >= begin)
118 #ifdef USE_WIDE_CHAR
119 if (*cp == thousands)
120 break;
121 #else
122 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
123 if (thousands[cnt] != cp[thousands_len - cnt - 1])
124 break;
125 if (thousands[cnt] == '\0')
126 break;
127 #endif
128 --cp;
131 if (cp < begin)
132 /* OK, only digits followed. */
133 return end;
135 else
137 /* Check the next group. */
138 const STRING_TYPE *group_end = cp;
140 while (cp >= begin)
142 #ifdef USE_WIDE_CHAR
143 if (*cp == thousands)
144 break;
145 #else
146 for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
147 if (thousands[cnt] != cp[thousands_len - cnt - 1])
148 break;
149 if (thousands[cnt] == '\0')
150 break;
151 #endif
152 --cp;
155 if (cp < begin && group_end - cp <= (int) *gp)
156 /* Final group is correct. */
157 return end;
159 if (cp < begin || group_end - cp != (int) *gp)
160 /* Incorrect group. Punt. */
161 break;
165 /* The trailing portion of the string starting at NEW_END
166 contains a grouping error. So we will look for a correctly
167 grouped number in the preceding portion instead. */
168 end = new_end;
170 else
172 /* Even the first group was wrong; determine maximum shift. */
173 if (end - cp > (int) *gp + 1)
174 end = cp + (int) *gp + 1;
175 else if (cp < begin)
176 /* This number does not fill the first group, but is correct. */
177 return end;
178 else
179 /* CP points to a thousands separator character. */
180 end = cp;
184 return MAX (begin, end);