Sun Dec 17 15:56:35 1995 Miles Bader <miles@gnu.ai.mit.edu>
[glibc.git] / stdlib / grouping.h
blob566f6a61091f5d842d8796c59442287a243aa54d
1 /* Internal header for proving correct grouping in strings of numbers.
2 Copyright (C) 1995 Free Software Foundation, Inc.
3 Contributed by Ulrich Drepper.
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
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, 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 char *
32 correctly_grouped_prefix (const char *begin, const char *end,
33 wchar_t thousands, const char *grouping)
35 if (! grouping)
36 return end;
38 while (end > begin)
40 const char *cp = end - 1;
41 const char *gp = grouping;
43 /* Check first group. */
44 while (cp >= begin && (wchar_t) *cp != thousands)
45 --cp;
47 if (end - cp == (int) *gp + 1)
49 /* This group matches the specification. */
51 const char *new_end;
53 if (cp < begin)
54 /* There is just one complete group. We are done. */
55 return end;
57 /* CP points to a thousands separator character. The preceding
58 remainder of the string from BEGIN to NEW_END is the part we
59 will consider if there is a grouping error in this trailing
60 portion from CP to END. */
61 new_end = cp - 1;
63 /* Loop while the grouping is correct. */
64 while (1)
66 /* Get the next grouping rule. */
67 ++gp;
68 if (*gp == 0)
69 /* If end is reached use last rule. */
70 --gp;
72 /* Skip the thousands separator. */
73 --cp;
75 if (*gp == CHAR_MAX || *gp < 0)
77 /* No more thousands separators are allowed to follow. */
78 while (cp >= begin && (wchar_t) *cp != thousands)
79 --cp;
81 if (cp < begin)
82 /* OK, only digits followed. */
83 return end;
85 else
87 /* Check the next group. */
88 const char *group_end = cp;
90 while (cp >= begin && (wchar_t) *cp != thousands)
91 --cp;
93 if (cp < begin && group_end - cp <= (int) *gp)
94 /* Final group is correct. */
95 return end;
97 if (cp < begin || group_end - cp != (int) *gp)
98 /* Incorrect group. Punt. */
99 break;
103 /* The trailing portion of the string starting at NEW_END
104 contains a grouping error. So we will look for a correctly
105 gouped number in the preceding portion instead. */
106 end = new_end;
108 else
110 /* Even the first group was wrong; determine maximum shift. */
111 if (end - cp > (int) *gp + 1)
112 end = cp + (int) *gp + 1;
113 else if (cp < begin)
114 /* This number does not fill the first group, but is correct. */
115 return end;
116 else
117 /* CP points to a thousands seperator character. */
118 end = cp;
122 return MAX (begin, end);