Update.
[glibc.git] / stdlib / grouping.h
blob71b89f9133d39e89155040032af8b5a50da5f7b2
1 /* Internal header for proving correct grouping in strings of numbers.
2 Copyright (C) 1995, 1996, 1997, 1998 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 wchar_t thousands, const char *grouping)
35 if (grouping == NULL)
36 return end;
38 if (*grouping == '\0')
40 /* No grouping allowed. Accept all characters up to the first
41 thousands separator. */
42 while (begin < end && *begin != thousands)
43 ++begin;
44 return begin;
47 while (end > begin)
49 const STRING_TYPE *cp = end - 1;
50 const char *gp = grouping;
52 /* Check first group. */
53 while (cp >= begin && (wchar_t) *cp != thousands)
54 --cp;
56 /* We allow the representation to contain no grouping at all even if
57 the locale specifies we can have grouping. */
58 if (cp < begin)
59 return end;
61 if (end - cp == (int) *gp + 1)
63 /* This group matches the specification. */
65 const STRING_TYPE *new_end;
67 if (cp < begin)
68 /* There is just one complete group. We are done. */
69 return end;
71 /* CP points to a thousands separator character. The preceding
72 remainder of the string from BEGIN to NEW_END is the part we
73 will consider if there is a grouping error in this trailing
74 portion from CP to END. */
75 new_end = cp - 1;
77 /* Loop while the grouping is correct. */
78 while (1)
80 /* Get the next grouping rule. */
81 ++gp;
82 if (*gp == 0)
83 /* If end is reached use last rule. */
84 --gp;
86 /* Skip the thousands separator. */
87 --cp;
89 if (*gp == CHAR_MAX
90 #if CHAR_MIN < 0
91 || *gp < 0
92 #endif
95 /* No more thousands separators are allowed to follow. */
96 while (cp >= begin && (wchar_t) *cp != thousands)
97 --cp;
99 if (cp < begin)
100 /* OK, only digits followed. */
101 return end;
103 else
105 /* Check the next group. */
106 const STRING_TYPE *group_end = cp;
108 while (cp >= begin && (wchar_t) *cp != thousands)
109 --cp;
111 if (cp < begin && group_end - cp <= (int) *gp)
112 /* Final group is correct. */
113 return end;
115 if (cp < begin || group_end - cp != (int) *gp)
116 /* Incorrect group. Punt. */
117 break;
121 /* The trailing portion of the string starting at NEW_END
122 contains a grouping error. So we will look for a correctly
123 grouped number in the preceding portion instead. */
124 end = new_end;
126 else
128 /* Even the first group was wrong; determine maximum shift. */
129 if (end - cp > (int) *gp + 1)
130 end = cp + (int) *gp + 1;
131 else if (cp < begin)
132 /* This number does not fill the first group, but is correct. */
133 return end;
134 else
135 /* CP points to a thousands separator character. */
136 end = cp;
140 return MAX (begin, end);