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