malloc/Makefile: Split and sort tests
[glibc.git] / stdlib / grouping.c
blob7657d6a7f33451e6bf3b81f90007fcc9da65b23b
1 /* Internal header for proving correct grouping in strings of numbers.
2 Copyright (C) 1995-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <limits.h>
20 #include <stddef.h>
21 #include <string.h>
23 #ifndef MAX
24 #define MAX(a,b) ({ typeof(a) _a = (a); typeof(b) _b = (b); \
25 _a > _b ? _a : _b; })
26 #endif
28 #ifdef USE_WIDE_CHAR
29 # include <wctype.h>
30 # define L_(Ch) L##Ch
31 # define UCHAR_TYPE wint_t
32 # define STRING_TYPE wchar_t
33 #else
34 # define L_(Ch) Ch
35 # define UCHAR_TYPE unsigned char
36 # define STRING_TYPE char
37 #endif
39 #include "grouping.h"
41 /* Find the maximum prefix of the string between BEGIN and END which
42 satisfies the grouping rules. It is assumed that at least one digit
43 follows BEGIN directly. */
45 const STRING_TYPE *
46 #ifdef USE_WIDE_CHAR
47 __correctly_grouped_prefixwc (const STRING_TYPE *begin, const STRING_TYPE *end,
48 wchar_t thousands,
49 #else
50 __correctly_grouped_prefixmb (const STRING_TYPE *begin, const STRING_TYPE *end,
51 const char *thousands,
52 #endif
53 const char *grouping)
55 if (grouping == NULL)
56 return end;
58 #ifdef USE_WIDE_CHAR
59 size_t thousands_len = 1;
60 #else
61 size_t thousands_len = strlen (thousands);
62 #endif
64 while (end - begin >= thousands_len)
66 const STRING_TYPE *cp = end - thousands_len;
67 const char *gp = grouping;
69 /* Check first group. */
70 while (cp >= begin)
72 #ifdef USE_WIDE_CHAR
73 if (*cp == thousands)
74 break;
75 #else
76 if (memcmp (cp, thousands, thousands_len) == 0)
77 break;
78 #endif
79 --cp;
82 /* We allow the representation to contain no grouping at all even if
83 the locale specifies we can have grouping. */
84 if (cp < begin)
85 return end;
87 if (end - cp == (int) *gp + thousands_len)
89 /* This group matches the specification. */
91 const STRING_TYPE *new_end;
93 if (cp < begin)
94 /* There is just one complete group. We are done. */
95 return end;
97 /* CP points to a thousands separator character. The preceding
98 remainder of the string from BEGIN to NEW_END is the part we
99 will consider if there is a grouping error in this trailing
100 portion from CP to END. */
101 new_end = cp;
103 /* Loop while the grouping is correct. */
104 while (1)
106 /* Get the next grouping rule. */
107 ++gp;
108 if (*gp == 0)
109 /* If end is reached use last rule. */
110 --gp;
112 /* Skip the thousands separator. */
113 --cp;
115 if (*gp == CHAR_MAX
116 #if CHAR_MIN < 0
117 || *gp < 0
118 #endif
121 /* No more thousands separators are allowed to follow. */
122 while (cp >= begin)
124 #ifdef USE_WIDE_CHAR
125 if (*cp == thousands)
126 break;
127 #else
128 if (memcmp (cp, thousands, thousands_len) == 0)
129 break;
130 #endif
131 --cp;
134 if (cp < begin)
135 /* OK, only digits followed. */
136 return end;
138 else
140 /* Check the next group. */
141 const STRING_TYPE *group_end = cp;
143 while (cp >= begin)
145 #ifdef USE_WIDE_CHAR
146 if (*cp == thousands)
147 break;
148 #else
149 if (memcmp (cp, thousands, thousands_len) == 0)
150 break;
151 #endif
152 --cp;
155 if (cp < begin && group_end - cp <= (int) *gp + thousands_len - 1)
156 /* Final group is correct. */
157 return end;
159 if (cp < begin || group_end - cp != (int) *gp + thousands_len - 1)
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 + thousands_len)
174 end = cp + (int) *gp + thousands_len;
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);