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