xref: /haiku/src/system/libroot/posix/glibc/stdlib/grouping.h (revision 5af32e752606778be5dd7379f319fe43cb3f6b8c)
1*5af32e75SAxel Dörfler /* Internal header for proving correct grouping in strings of numbers.
2*5af32e75SAxel Dörfler    Copyright (C) 1995, 1996, 1997, 1998, 2000 Free Software Foundation, Inc.
3*5af32e75SAxel Dörfler    This file is part of the GNU C Library.
4*5af32e75SAxel Dörfler    Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
5*5af32e75SAxel Dörfler 
6*5af32e75SAxel Dörfler    The GNU C Library is free software; you can redistribute it and/or
7*5af32e75SAxel Dörfler    modify it under the terms of the GNU Lesser General Public
8*5af32e75SAxel Dörfler    License as published by the Free Software Foundation; either
9*5af32e75SAxel Dörfler    version 2.1 of the License, or (at your option) any later version.
10*5af32e75SAxel Dörfler 
11*5af32e75SAxel Dörfler    The GNU C Library is distributed in the hope that it will be useful,
12*5af32e75SAxel Dörfler    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*5af32e75SAxel Dörfler    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14*5af32e75SAxel Dörfler    Lesser General Public License for more details.
15*5af32e75SAxel Dörfler 
16*5af32e75SAxel Dörfler    You should have received a copy of the GNU Lesser General Public
17*5af32e75SAxel Dörfler    License along with the GNU C Library; if not, write to the Free
18*5af32e75SAxel Dörfler    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19*5af32e75SAxel Dörfler    02111-1307 USA.  */
20*5af32e75SAxel Dörfler 
21*5af32e75SAxel Dörfler #include <limits.h>
22*5af32e75SAxel Dörfler 
23*5af32e75SAxel Dörfler #ifndef MAX
24*5af32e75SAxel Dörfler #define MAX(a,b)	({ typeof(a) _a = (a); typeof(b) _b = (b); \
25*5af32e75SAxel Dörfler 			   _a > _b ? _a : _b; })
26*5af32e75SAxel Dörfler #endif
27*5af32e75SAxel Dörfler 
28*5af32e75SAxel Dörfler /* Find the maximum prefix of the string between BEGIN and END which
29*5af32e75SAxel Dörfler    satisfies the grouping rules.  It is assumed that at least one digit
30*5af32e75SAxel Dörfler    follows BEGIN directly.  */
31*5af32e75SAxel Dörfler 
32*5af32e75SAxel Dörfler static inline const STRING_TYPE *
correctly_grouped_prefix(const STRING_TYPE * begin,const STRING_TYPE * end,wchar_t thousands,const char * grouping)33*5af32e75SAxel Dörfler correctly_grouped_prefix (const STRING_TYPE *begin, const STRING_TYPE *end,
34*5af32e75SAxel Dörfler #ifdef USE_WIDE_CHAR
35*5af32e75SAxel Dörfler 			  wchar_t thousands,
36*5af32e75SAxel Dörfler #else
37*5af32e75SAxel Dörfler 			  const char *thousands,
38*5af32e75SAxel Dörfler #endif
39*5af32e75SAxel Dörfler 			  const char *grouping)
40*5af32e75SAxel Dörfler {
41*5af32e75SAxel Dörfler #ifndef USE_WIDE_CHAR
42*5af32e75SAxel Dörfler   size_t thousands_len;
43*5af32e75SAxel Dörfler   int cnt;
44*5af32e75SAxel Dörfler #endif
45*5af32e75SAxel Dörfler 
46*5af32e75SAxel Dörfler   if (grouping == NULL)
47*5af32e75SAxel Dörfler     return end;
48*5af32e75SAxel Dörfler 
49*5af32e75SAxel Dörfler #ifndef USE_WIDE_CHAR
50*5af32e75SAxel Dörfler   thousands_len = strlen (thousands);
51*5af32e75SAxel Dörfler #endif
52*5af32e75SAxel Dörfler 
53*5af32e75SAxel Dörfler   while (end > begin)
54*5af32e75SAxel Dörfler     {
55*5af32e75SAxel Dörfler       const STRING_TYPE *cp = end - 1;
56*5af32e75SAxel Dörfler       const char *gp = grouping;
57*5af32e75SAxel Dörfler 
58*5af32e75SAxel Dörfler       /* Check first group.  */
59*5af32e75SAxel Dörfler       while (cp >= begin)
60*5af32e75SAxel Dörfler 	{
61*5af32e75SAxel Dörfler #ifdef USE_WIDE_CHAR
62*5af32e75SAxel Dörfler 	  if (*cp == thousands)
63*5af32e75SAxel Dörfler 	    break;
64*5af32e75SAxel Dörfler #else
65*5af32e75SAxel Dörfler 	  if (cp[thousands_len - 1] == *thousands)
66*5af32e75SAxel Dörfler 	    {
67*5af32e75SAxel Dörfler 	      for (cnt = 1; thousands[cnt] != '\0'; ++cnt)
68*5af32e75SAxel Dörfler 		if (thousands[cnt] != cp[thousands_len - 1 - cnt])
69*5af32e75SAxel Dörfler 		  break;
70*5af32e75SAxel Dörfler 	      if (thousands[cnt] == '\0')
71*5af32e75SAxel Dörfler 		break;
72*5af32e75SAxel Dörfler 	    }
73*5af32e75SAxel Dörfler #endif
74*5af32e75SAxel Dörfler 	  --cp;
75*5af32e75SAxel Dörfler 	}
76*5af32e75SAxel Dörfler 
77*5af32e75SAxel Dörfler       /* We allow the representation to contain no grouping at all even if
78*5af32e75SAxel Dörfler 	 the locale specifies we can have grouping.  */
79*5af32e75SAxel Dörfler       if (cp < begin)
80*5af32e75SAxel Dörfler 	return end;
81*5af32e75SAxel Dörfler 
82*5af32e75SAxel Dörfler       if (end - cp == (int) *gp + 1)
83*5af32e75SAxel Dörfler 	{
84*5af32e75SAxel Dörfler 	  /* This group matches the specification.  */
85*5af32e75SAxel Dörfler 
86*5af32e75SAxel Dörfler 	  const STRING_TYPE *new_end;
87*5af32e75SAxel Dörfler 
88*5af32e75SAxel Dörfler 	  if (cp < begin)
89*5af32e75SAxel Dörfler 	    /* There is just one complete group.  We are done.  */
90*5af32e75SAxel Dörfler 	    return end;
91*5af32e75SAxel Dörfler 
92*5af32e75SAxel Dörfler 	  /* CP points to a thousands separator character.  The preceding
93*5af32e75SAxel Dörfler 	     remainder of the string from BEGIN to NEW_END is the part we
94*5af32e75SAxel Dörfler 	     will consider if there is a grouping error in this trailing
95*5af32e75SAxel Dörfler 	     portion from CP to END.  */
96*5af32e75SAxel Dörfler 	  new_end = cp - 1;
97*5af32e75SAxel Dörfler 
98*5af32e75SAxel Dörfler 	  /* Loop while the grouping is correct.  */
99*5af32e75SAxel Dörfler 	  while (1)
100*5af32e75SAxel Dörfler 	    {
101*5af32e75SAxel Dörfler 	      /* Get the next grouping rule.  */
102*5af32e75SAxel Dörfler 	      ++gp;
103*5af32e75SAxel Dörfler 	      if (*gp == 0)
104*5af32e75SAxel Dörfler 		/* If end is reached use last rule.  */
105*5af32e75SAxel Dörfler 	        --gp;
106*5af32e75SAxel Dörfler 
107*5af32e75SAxel Dörfler 	      /* Skip the thousands separator.  */
108*5af32e75SAxel Dörfler 	      --cp;
109*5af32e75SAxel Dörfler 
110*5af32e75SAxel Dörfler 	      if (*gp == CHAR_MAX
111*5af32e75SAxel Dörfler #if CHAR_MIN < 0
112*5af32e75SAxel Dörfler 		  || *gp < 0
113*5af32e75SAxel Dörfler #endif
114*5af32e75SAxel Dörfler 		  )
115*5af32e75SAxel Dörfler 	        {
116*5af32e75SAxel Dörfler 	          /* No more thousands separators are allowed to follow.  */
117*5af32e75SAxel Dörfler 	          while (cp >= begin)
118*5af32e75SAxel Dörfler 		    {
119*5af32e75SAxel Dörfler #ifdef USE_WIDE_CHAR
120*5af32e75SAxel Dörfler 		      if (*cp == thousands)
121*5af32e75SAxel Dörfler 			break;
122*5af32e75SAxel Dörfler #else
123*5af32e75SAxel Dörfler 		      for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
124*5af32e75SAxel Dörfler 			if (thousands[cnt] != cp[thousands_len - cnt - 1])
125*5af32e75SAxel Dörfler 			  break;
126*5af32e75SAxel Dörfler 		      if (thousands[cnt] == '\0')
127*5af32e75SAxel Dörfler 			break;
128*5af32e75SAxel Dörfler #endif
129*5af32e75SAxel Dörfler 		      --cp;
130*5af32e75SAxel Dörfler 		    }
131*5af32e75SAxel Dörfler 
132*5af32e75SAxel Dörfler 	          if (cp < begin)
133*5af32e75SAxel Dörfler 		    /* OK, only digits followed.  */
134*5af32e75SAxel Dörfler 		    return end;
135*5af32e75SAxel Dörfler 	        }
136*5af32e75SAxel Dörfler 	      else
137*5af32e75SAxel Dörfler 	        {
138*5af32e75SAxel Dörfler 		  /* Check the next group.  */
139*5af32e75SAxel Dörfler 	          const STRING_TYPE *group_end = cp;
140*5af32e75SAxel Dörfler 
141*5af32e75SAxel Dörfler 		  while (cp >= begin)
142*5af32e75SAxel Dörfler 		    {
143*5af32e75SAxel Dörfler #ifdef USE_WIDE_CHAR
144*5af32e75SAxel Dörfler 		      if (*cp == thousands)
145*5af32e75SAxel Dörfler 			break;
146*5af32e75SAxel Dörfler #else
147*5af32e75SAxel Dörfler 		      for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
148*5af32e75SAxel Dörfler 			if (thousands[cnt] != cp[thousands_len - cnt - 1])
149*5af32e75SAxel Dörfler 			  break;
150*5af32e75SAxel Dörfler 		      if (thousands[cnt] == '\0')
151*5af32e75SAxel Dörfler 			break;
152*5af32e75SAxel Dörfler #endif
153*5af32e75SAxel Dörfler 		      --cp;
154*5af32e75SAxel Dörfler 		    }
155*5af32e75SAxel Dörfler 
156*5af32e75SAxel Dörfler 		  if (cp < begin && group_end - cp <= (int) *gp)
157*5af32e75SAxel Dörfler 		    /* Final group is correct.  */
158*5af32e75SAxel Dörfler 		    return end;
159*5af32e75SAxel Dörfler 
160*5af32e75SAxel Dörfler 		  if (cp < begin || group_end - cp != (int) *gp)
161*5af32e75SAxel Dörfler 		    /* Incorrect group.  Punt.  */
162*5af32e75SAxel Dörfler 		    break;
163*5af32e75SAxel Dörfler 		}
164*5af32e75SAxel Dörfler 	    }
165*5af32e75SAxel Dörfler 
166*5af32e75SAxel Dörfler 	  /* The trailing portion of the string starting at NEW_END
167*5af32e75SAxel Dörfler 	     contains a grouping error.  So we will look for a correctly
168*5af32e75SAxel Dörfler 	     grouped number in the preceding portion instead.  */
169*5af32e75SAxel Dörfler 	  end = new_end;
170*5af32e75SAxel Dörfler 	}
171*5af32e75SAxel Dörfler       else
172*5af32e75SAxel Dörfler 	{
173*5af32e75SAxel Dörfler 	  /* Even the first group was wrong; determine maximum shift.  */
174*5af32e75SAxel Dörfler 	  if (end - cp > (int) *gp + 1)
175*5af32e75SAxel Dörfler 	    end = cp + (int) *gp + 1;
176*5af32e75SAxel Dörfler 	  else if (cp < begin)
177*5af32e75SAxel Dörfler 	    /* This number does not fill the first group, but is correct.  */
178*5af32e75SAxel Dörfler 	    return end;
179*5af32e75SAxel Dörfler 	  else
180*5af32e75SAxel Dörfler 	    /* CP points to a thousands separator character.  */
181*5af32e75SAxel Dörfler 	    end = cp;
182*5af32e75SAxel Dörfler 	}
183*5af32e75SAxel Dörfler     }
184*5af32e75SAxel Dörfler 
185*5af32e75SAxel Dörfler   return MAX (begin, end);
186*5af32e75SAxel Dörfler }
187