1 /** @file
2     stringlib: locale related helpers implementation.
3 
4     Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
5     This program and the accompanying materials are licensed and made available under
6     the terms and conditions of the BSD License that accompanies this distribution.
7     The full text of the license may be found at
8     http://opensource.org/licenses/bsd-license.
9 
10     THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11     WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 **/
13 
14 #ifndef STRINGLIB_LOCALEUTIL_H
15 #define STRINGLIB_LOCALEUTIL_H
16 
17 #include <locale.h>
18 
19 // Prevent conflicts with EFI
20 #undef  MAX
21 #undef  MIN
22 
23 #define MAX(x, y) ((x) < (y) ? (y) : (x))
24 #define MIN(x, y) ((x) < (y) ? (x) : (y))
25 
26 typedef struct {
27     const char *grouping;
28     char previous;
29     Py_ssize_t i; /* Where we're currently pointing in grouping. */
30 } GroupGenerator;
31 
32 static void
_GroupGenerator_init(GroupGenerator * self,const char * grouping)33 _GroupGenerator_init(GroupGenerator *self, const char *grouping)
34 {
35     self->grouping = grouping;
36     self->i = 0;
37     self->previous = 0;
38 }
39 
40 /* Returns the next grouping, or 0 to signify end. */
41 static Py_ssize_t
_GroupGenerator_next(GroupGenerator * self)42 _GroupGenerator_next(GroupGenerator *self)
43 {
44     /* Note that we don't really do much error checking here. If a
45        grouping string contains just CHAR_MAX, for example, then just
46        terminate the generator. That shouldn't happen, but at least we
47        fail gracefully. */
48     switch (self->grouping[self->i]) {
49     case 0:
50         return self->previous;
51     case CHAR_MAX:
52         /* Stop the generator. */
53         return 0;
54     default: {
55         char ch = self->grouping[self->i];
56         self->previous = ch;
57         self->i++;
58         return (Py_ssize_t)ch;
59     }
60     }
61 }
62 
63 /* Fill in some digits, leading zeros, and thousands separator. All
64    are optional, depending on when we're called. */
65 static void
fill(STRINGLIB_CHAR ** digits_end,STRINGLIB_CHAR ** buffer_end,Py_ssize_t n_chars,Py_ssize_t n_zeros,const char * thousands_sep,Py_ssize_t thousands_sep_len)66 fill(STRINGLIB_CHAR **digits_end, STRINGLIB_CHAR **buffer_end,
67      Py_ssize_t n_chars, Py_ssize_t n_zeros, const char* thousands_sep,
68      Py_ssize_t thousands_sep_len)
69 {
70 #if STRINGLIB_IS_UNICODE
71     Py_ssize_t i;
72 #endif
73 
74     if (thousands_sep) {
75         *buffer_end -= thousands_sep_len;
76 
77         /* Copy the thousands_sep chars into the buffer. */
78 #if STRINGLIB_IS_UNICODE
79         /* Convert from the char's of the thousands_sep from
80            the locale into unicode. */
81         for (i = 0; i < thousands_sep_len; ++i)
82             (*buffer_end)[i] = thousands_sep[i];
83 #else
84         /* No conversion, just memcpy the thousands_sep. */
85         memcpy(*buffer_end, thousands_sep, thousands_sep_len);
86 #endif
87     }
88 
89     *buffer_end -= n_chars;
90     *digits_end -= n_chars;
91     memcpy(*buffer_end, *digits_end, n_chars * sizeof(STRINGLIB_CHAR));
92 
93     *buffer_end -= n_zeros;
94     STRINGLIB_FILL(*buffer_end, '0', n_zeros);
95 }
96 
97 /**
98  * _Py_InsertThousandsGrouping:
99  * @buffer: A pointer to the start of a string.
100  * @n_buffer: Number of characters in @buffer.
101  * @digits: A pointer to the digits we're reading from. If count
102  *          is non-NULL, this is unused.
103  * @n_digits: The number of digits in the string, in which we want
104  *            to put the grouping chars.
105  * @min_width: The minimum width of the digits in the output string.
106  *             Output will be zero-padded on the left to fill.
107  * @grouping: see definition in localeconv().
108  * @thousands_sep: see definition in localeconv().
109  *
110  * There are 2 modes: counting and filling. If @buffer is NULL,
111  *  we are in counting mode, else filling mode.
112  * If counting, the required buffer size is returned.
113  * If filling, we know the buffer will be large enough, so we don't
114  *  need to pass in the buffer size.
115  * Inserts thousand grouping characters (as defined by grouping and
116  *  thousands_sep) into the string between buffer and buffer+n_digits.
117  *
118  * Return value: 0 on error, else 1.  Note that no error can occur if
119  *  count is non-NULL.
120  *
121  * This name won't be used, the includer of this file should define
122  *  it to be the actual function name, based on unicode or string.
123  *
124  * As closely as possible, this code mimics the logic in decimal.py's
125     _insert_thousands_sep().
126  **/
127 Py_ssize_t
_Py_InsertThousandsGrouping(STRINGLIB_CHAR * buffer,Py_ssize_t n_buffer,STRINGLIB_CHAR * digits,Py_ssize_t n_digits,Py_ssize_t min_width,const char * grouping,const char * thousands_sep)128 _Py_InsertThousandsGrouping(STRINGLIB_CHAR *buffer,
129                             Py_ssize_t n_buffer,
130                             STRINGLIB_CHAR *digits,
131                             Py_ssize_t n_digits,
132                             Py_ssize_t min_width,
133                             const char *grouping,
134                             const char *thousands_sep)
135 {
136     Py_ssize_t count = 0;
137     Py_ssize_t n_zeros;
138     int loop_broken = 0;
139     int use_separator = 0; /* First time through, don't append the
140                               separator. They only go between
141                               groups. */
142     STRINGLIB_CHAR *buffer_end = NULL;
143     STRINGLIB_CHAR *digits_end = NULL;
144     Py_ssize_t l;
145     Py_ssize_t n_chars;
146     Py_ssize_t thousands_sep_len = strlen(thousands_sep);
147     Py_ssize_t remaining = n_digits; /* Number of chars remaining to
148                                         be looked at */
149     /* A generator that returns all of the grouping widths, until it
150        returns 0. */
151     GroupGenerator groupgen;
152     _GroupGenerator_init(&groupgen, grouping);
153 
154     if (buffer) {
155         buffer_end = buffer + n_buffer;
156         digits_end = digits + n_digits;
157     }
158 
159     while ((l = _GroupGenerator_next(&groupgen)) > 0) {
160         l = MIN(l, MAX(MAX(remaining, min_width), 1));
161         n_zeros = MAX(0, l - remaining);
162         n_chars = MAX(0, MIN(remaining, l));
163 
164         /* Use n_zero zero's and n_chars chars */
165 
166         /* Count only, don't do anything. */
167         count += (use_separator ? thousands_sep_len : 0) + n_zeros + n_chars;
168 
169         if (buffer) {
170             /* Copy into the output buffer. */
171             fill(&digits_end, &buffer_end, n_chars, n_zeros,
172                  use_separator ? thousands_sep : NULL, thousands_sep_len);
173         }
174 
175         /* Use a separator next time. */
176         use_separator = 1;
177 
178         remaining -= n_chars;
179         min_width -= l;
180 
181         if (remaining <= 0 && min_width <= 0) {
182             loop_broken = 1;
183             break;
184         }
185         min_width -= thousands_sep_len;
186     }
187     if (!loop_broken) {
188         /* We left the loop without using a break statement. */
189 
190         l = MAX(MAX(remaining, min_width), 1);
191         n_zeros = MAX(0, l - remaining);
192         n_chars = MAX(0, MIN(remaining, l));
193 
194         /* Use n_zero zero's and n_chars chars */
195         count += (use_separator ? thousands_sep_len : 0) + n_zeros + n_chars;
196         if (buffer) {
197             /* Copy into the output buffer. */
198             fill(&digits_end, &buffer_end, n_chars, n_zeros,
199                  use_separator ? thousands_sep : NULL, thousands_sep_len);
200         }
201     }
202     return count;
203 }
204 
205 /**
206  * _Py_InsertThousandsGroupingLocale:
207  * @buffer: A pointer to the start of a string.
208  * @n_digits: The number of digits in the string, in which we want
209  *            to put the grouping chars.
210  *
211  * Reads thee current locale and calls _Py_InsertThousandsGrouping().
212  **/
213 Py_ssize_t
_Py_InsertThousandsGroupingLocale(STRINGLIB_CHAR * buffer,Py_ssize_t n_buffer,STRINGLIB_CHAR * digits,Py_ssize_t n_digits,Py_ssize_t min_width)214 _Py_InsertThousandsGroupingLocale(STRINGLIB_CHAR *buffer,
215                                   Py_ssize_t n_buffer,
216                                   STRINGLIB_CHAR *digits,
217                                   Py_ssize_t n_digits,
218                                   Py_ssize_t min_width)
219 {
220         struct lconv *locale_data = localeconv();
221         const char *grouping = locale_data->grouping;
222         const char *thousands_sep = locale_data->thousands_sep;
223 
224         return _Py_InsertThousandsGrouping(buffer, n_buffer, digits, n_digits,
225                                            min_width, grouping, thousands_sep);
226 }
227 #endif /* STRINGLIB_LOCALEUTIL_H */
228