]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | /* implements the string, long, and float formatters. that is,\r |
2 | string.__format__, etc. */\r | |
3 | \r | |
4 | #include <locale.h>\r | |
5 | \r | |
6 | /* Before including this, you must include either:\r | |
7 | stringlib/unicodedefs.h\r | |
8 | stringlib/stringdefs.h\r | |
9 | \r | |
10 | Also, you should define the names:\r | |
11 | FORMAT_STRING\r | |
12 | FORMAT_LONG\r | |
13 | FORMAT_FLOAT\r | |
14 | FORMAT_COMPLEX\r | |
15 | to be whatever you want the public names of these functions to\r | |
16 | be. These are the only non-static functions defined here.\r | |
17 | */\r | |
18 | \r | |
19 | /* Raises an exception about an unknown presentation type for this\r | |
20 | * type. */\r | |
21 | \r | |
22 | static void\r | |
23 | unknown_presentation_type(STRINGLIB_CHAR presentation_type,\r | |
24 | const char* type_name)\r | |
25 | {\r | |
26 | #if STRINGLIB_IS_UNICODE\r | |
27 | /* If STRINGLIB_CHAR is Py_UNICODE, %c might be out-of-range,\r | |
28 | hence the two cases. If it is char, gcc complains that the\r | |
29 | condition below is always true, hence the ifdef. */\r | |
30 | if (presentation_type > 32 && presentation_type < 128)\r | |
31 | #endif\r | |
32 | PyErr_Format(PyExc_ValueError,\r | |
33 | "Unknown format code '%c' "\r | |
34 | "for object of type '%.200s'",\r | |
35 | (char)presentation_type,\r | |
36 | type_name);\r | |
37 | #if STRINGLIB_IS_UNICODE\r | |
38 | else\r | |
39 | PyErr_Format(PyExc_ValueError,\r | |
40 | "Unknown format code '\\x%x' "\r | |
41 | "for object of type '%.200s'",\r | |
42 | (unsigned int)presentation_type,\r | |
43 | type_name);\r | |
44 | #endif\r | |
45 | }\r | |
46 | \r | |
47 | static void\r | |
48 | invalid_comma_type(STRINGLIB_CHAR presentation_type)\r | |
49 | {\r | |
50 | #if STRINGLIB_IS_UNICODE\r | |
51 | /* See comment in unknown_presentation_type */\r | |
52 | if (presentation_type > 32 && presentation_type < 128)\r | |
53 | #endif\r | |
54 | PyErr_Format(PyExc_ValueError,\r | |
55 | "Cannot specify ',' with '%c'.",\r | |
56 | (char)presentation_type);\r | |
57 | #if STRINGLIB_IS_UNICODE\r | |
58 | else\r | |
59 | PyErr_Format(PyExc_ValueError,\r | |
60 | "Cannot specify ',' with '\\x%x'.",\r | |
61 | (unsigned int)presentation_type);\r | |
62 | #endif\r | |
63 | }\r | |
64 | \r | |
65 | /*\r | |
66 | get_integer consumes 0 or more decimal digit characters from an\r | |
67 | input string, updates *result with the corresponding positive\r | |
68 | integer, and returns the number of digits consumed.\r | |
69 | \r | |
70 | returns -1 on error.\r | |
71 | */\r | |
72 | static int\r | |
73 | get_integer(STRINGLIB_CHAR **ptr, STRINGLIB_CHAR *end,\r | |
74 | Py_ssize_t *result)\r | |
75 | {\r | |
76 | Py_ssize_t accumulator, digitval, oldaccumulator;\r | |
77 | int numdigits;\r | |
78 | accumulator = numdigits = 0;\r | |
79 | for (;;(*ptr)++, numdigits++) {\r | |
80 | if (*ptr >= end)\r | |
81 | break;\r | |
82 | digitval = STRINGLIB_TODECIMAL(**ptr);\r | |
83 | if (digitval < 0)\r | |
84 | break;\r | |
85 | /*\r | |
86 | This trick was copied from old Unicode format code. It's cute,\r | |
87 | but would really suck on an old machine with a slow divide\r | |
88 | implementation. Fortunately, in the normal case we do not\r | |
89 | expect too many digits.\r | |
90 | */\r | |
91 | oldaccumulator = accumulator;\r | |
92 | accumulator *= 10;\r | |
93 | if ((accumulator+10)/10 != oldaccumulator+1) {\r | |
94 | PyErr_Format(PyExc_ValueError,\r | |
95 | "Too many decimal digits in format string");\r | |
96 | return -1;\r | |
97 | }\r | |
98 | accumulator += digitval;\r | |
99 | }\r | |
100 | *result = accumulator;\r | |
101 | return numdigits;\r | |
102 | }\r | |
103 | \r | |
104 | /************************************************************************/\r | |
105 | /*********** standard format specifier parsing **************************/\r | |
106 | /************************************************************************/\r | |
107 | \r | |
108 | /* returns true if this character is a specifier alignment token */\r | |
109 | Py_LOCAL_INLINE(int)\r | |
110 | is_alignment_token(STRINGLIB_CHAR c)\r | |
111 | {\r | |
112 | switch (c) {\r | |
113 | case '<': case '>': case '=': case '^':\r | |
114 | return 1;\r | |
115 | default:\r | |
116 | return 0;\r | |
117 | }\r | |
118 | }\r | |
119 | \r | |
120 | /* returns true if this character is a sign element */\r | |
121 | Py_LOCAL_INLINE(int)\r | |
122 | is_sign_element(STRINGLIB_CHAR c)\r | |
123 | {\r | |
124 | switch (c) {\r | |
125 | case ' ': case '+': case '-':\r | |
126 | return 1;\r | |
127 | default:\r | |
128 | return 0;\r | |
129 | }\r | |
130 | }\r | |
131 | \r | |
132 | \r | |
133 | typedef struct {\r | |
134 | STRINGLIB_CHAR fill_char;\r | |
135 | STRINGLIB_CHAR align;\r | |
136 | int alternate;\r | |
137 | STRINGLIB_CHAR sign;\r | |
138 | Py_ssize_t width;\r | |
139 | int thousands_separators;\r | |
140 | Py_ssize_t precision;\r | |
141 | STRINGLIB_CHAR type;\r | |
142 | } InternalFormatSpec;\r | |
143 | \r | |
144 | \r | |
145 | #if 0\r | |
146 | /* Occassionally useful for debugging. Should normally be commented out. */\r | |
147 | static void\r | |
148 | DEBUG_PRINT_FORMAT_SPEC(InternalFormatSpec *format)\r | |
149 | {\r | |
150 | printf("internal format spec: fill_char %d\n", format->fill_char);\r | |
151 | printf("internal format spec: align %d\n", format->align);\r | |
152 | printf("internal format spec: alternate %d\n", format->alternate);\r | |
153 | printf("internal format spec: sign %d\n", format->sign);\r | |
154 | printf("internal format spec: width %zd\n", format->width);\r | |
155 | printf("internal format spec: thousands_separators %d\n",\r | |
156 | format->thousands_separators);\r | |
157 | printf("internal format spec: precision %zd\n", format->precision);\r | |
158 | printf("internal format spec: type %c\n", format->type);\r | |
159 | printf("\n");\r | |
160 | }\r | |
161 | #endif\r | |
162 | \r | |
163 | \r | |
164 | /*\r | |
165 | ptr points to the start of the format_spec, end points just past its end.\r | |
166 | fills in format with the parsed information.\r | |
167 | returns 1 on success, 0 on failure.\r | |
168 | if failure, sets the exception\r | |
169 | */\r | |
170 | static int\r | |
171 | parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec,\r | |
172 | Py_ssize_t format_spec_len,\r | |
173 | InternalFormatSpec *format,\r | |
174 | char default_type,\r | |
175 | char default_align)\r | |
176 | {\r | |
177 | STRINGLIB_CHAR *ptr = format_spec;\r | |
178 | STRINGLIB_CHAR *end = format_spec + format_spec_len;\r | |
179 | \r | |
180 | /* end-ptr is used throughout this code to specify the length of\r | |
181 | the input string */\r | |
182 | \r | |
183 | Py_ssize_t consumed;\r | |
184 | int align_specified = 0;\r | |
185 | \r | |
186 | format->fill_char = '\0';\r | |
187 | format->align = default_align;\r | |
188 | format->alternate = 0;\r | |
189 | format->sign = '\0';\r | |
190 | format->width = -1;\r | |
191 | format->thousands_separators = 0;\r | |
192 | format->precision = -1;\r | |
193 | format->type = default_type;\r | |
194 | \r | |
195 | /* If the second char is an alignment token,\r | |
196 | then parse the fill char */\r | |
197 | if (end-ptr >= 2 && is_alignment_token(ptr[1])) {\r | |
198 | format->align = ptr[1];\r | |
199 | format->fill_char = ptr[0];\r | |
200 | align_specified = 1;\r | |
201 | ptr += 2;\r | |
202 | }\r | |
203 | else if (end-ptr >= 1 && is_alignment_token(ptr[0])) {\r | |
204 | format->align = ptr[0];\r | |
205 | align_specified = 1;\r | |
206 | ++ptr;\r | |
207 | }\r | |
208 | \r | |
209 | /* Parse the various sign options */\r | |
210 | if (end-ptr >= 1 && is_sign_element(ptr[0])) {\r | |
211 | format->sign = ptr[0];\r | |
212 | ++ptr;\r | |
213 | }\r | |
214 | \r | |
215 | /* If the next character is #, we're in alternate mode. This only\r | |
216 | applies to integers. */\r | |
217 | if (end-ptr >= 1 && ptr[0] == '#') {\r | |
218 | format->alternate = 1;\r | |
219 | ++ptr;\r | |
220 | }\r | |
221 | \r | |
222 | /* The special case for 0-padding (backwards compat) */\r | |
223 | if (format->fill_char == '\0' && end-ptr >= 1 && ptr[0] == '0') {\r | |
224 | format->fill_char = '0';\r | |
225 | if (!align_specified) {\r | |
226 | format->align = '=';\r | |
227 | }\r | |
228 | ++ptr;\r | |
229 | }\r | |
230 | \r | |
231 | consumed = get_integer(&ptr, end, &format->width);\r | |
232 | if (consumed == -1)\r | |
233 | /* Overflow error. Exception already set. */\r | |
234 | return 0;\r | |
235 | \r | |
236 | /* If consumed is 0, we didn't consume any characters for the\r | |
237 | width. In that case, reset the width to -1, because\r | |
238 | get_integer() will have set it to zero. -1 is how we record\r | |
239 | that the width wasn't specified. */\r | |
240 | if (consumed == 0)\r | |
241 | format->width = -1;\r | |
242 | \r | |
243 | /* Comma signifies add thousands separators */\r | |
244 | if (end-ptr && ptr[0] == ',') {\r | |
245 | format->thousands_separators = 1;\r | |
246 | ++ptr;\r | |
247 | }\r | |
248 | \r | |
249 | /* Parse field precision */\r | |
250 | if (end-ptr && ptr[0] == '.') {\r | |
251 | ++ptr;\r | |
252 | \r | |
253 | consumed = get_integer(&ptr, end, &format->precision);\r | |
254 | if (consumed == -1)\r | |
255 | /* Overflow error. Exception already set. */\r | |
256 | return 0;\r | |
257 | \r | |
258 | /* Not having a precision after a dot is an error. */\r | |
259 | if (consumed == 0) {\r | |
260 | PyErr_Format(PyExc_ValueError,\r | |
261 | "Format specifier missing precision");\r | |
262 | return 0;\r | |
263 | }\r | |
264 | \r | |
265 | }\r | |
266 | \r | |
267 | /* Finally, parse the type field. */\r | |
268 | \r | |
269 | if (end-ptr > 1) {\r | |
270 | /* More than one char remain, invalid conversion spec. */\r | |
271 | PyErr_Format(PyExc_ValueError, "Invalid conversion specification");\r | |
272 | return 0;\r | |
273 | }\r | |
274 | \r | |
275 | if (end-ptr == 1) {\r | |
276 | format->type = ptr[0];\r | |
277 | ++ptr;\r | |
278 | }\r | |
279 | \r | |
280 | /* Do as much validating as we can, just by looking at the format\r | |
281 | specifier. Do not take into account what type of formatting\r | |
282 | we're doing (int, float, string). */\r | |
283 | \r | |
284 | if (format->thousands_separators) {\r | |
285 | switch (format->type) {\r | |
286 | case 'd':\r | |
287 | case 'e':\r | |
288 | case 'f':\r | |
289 | case 'g':\r | |
290 | case 'E':\r | |
291 | case 'G':\r | |
292 | case '%':\r | |
293 | case 'F':\r | |
294 | case '\0':\r | |
295 | /* These are allowed. See PEP 378.*/\r | |
296 | break;\r | |
297 | default:\r | |
298 | invalid_comma_type(format->type);\r | |
299 | return 0;\r | |
300 | }\r | |
301 | }\r | |
302 | \r | |
303 | return 1;\r | |
304 | }\r | |
305 | \r | |
306 | /* Calculate the padding needed. */\r | |
307 | static void\r | |
308 | calc_padding(Py_ssize_t nchars, Py_ssize_t width, STRINGLIB_CHAR align,\r | |
309 | Py_ssize_t *n_lpadding, Py_ssize_t *n_rpadding,\r | |
310 | Py_ssize_t *n_total)\r | |
311 | {\r | |
312 | if (width >= 0) {\r | |
313 | if (nchars > width)\r | |
314 | *n_total = nchars;\r | |
315 | else\r | |
316 | *n_total = width;\r | |
317 | }\r | |
318 | else {\r | |
319 | /* not specified, use all of the chars and no more */\r | |
320 | *n_total = nchars;\r | |
321 | }\r | |
322 | \r | |
323 | /* Figure out how much leading space we need, based on the\r | |
324 | aligning */\r | |
325 | if (align == '>')\r | |
326 | *n_lpadding = *n_total - nchars;\r | |
327 | else if (align == '^')\r | |
328 | *n_lpadding = (*n_total - nchars) / 2;\r | |
329 | else if (align == '<' || align == '=')\r | |
330 | *n_lpadding = 0;\r | |
331 | else {\r | |
332 | /* We should never have an unspecified alignment. */\r | |
333 | *n_lpadding = 0;\r | |
334 | assert(0);\r | |
335 | }\r | |
336 | \r | |
337 | *n_rpadding = *n_total - nchars - *n_lpadding;\r | |
338 | }\r | |
339 | \r | |
340 | /* Do the padding, and return a pointer to where the caller-supplied\r | |
341 | content goes. */\r | |
342 | static STRINGLIB_CHAR *\r | |
343 | fill_padding(STRINGLIB_CHAR *p, Py_ssize_t nchars, STRINGLIB_CHAR fill_char,\r | |
344 | Py_ssize_t n_lpadding, Py_ssize_t n_rpadding)\r | |
345 | {\r | |
346 | /* Pad on left. */\r | |
347 | if (n_lpadding)\r | |
348 | STRINGLIB_FILL(p, fill_char, n_lpadding);\r | |
349 | \r | |
350 | /* Pad on right. */\r | |
351 | if (n_rpadding)\r | |
352 | STRINGLIB_FILL(p + nchars + n_lpadding, fill_char, n_rpadding);\r | |
353 | \r | |
354 | /* Pointer to the user content. */\r | |
355 | return p + n_lpadding;\r | |
356 | }\r | |
357 | \r | |
358 | #if defined FORMAT_FLOAT || defined FORMAT_LONG || defined FORMAT_COMPLEX\r | |
359 | /************************************************************************/\r | |
360 | /*********** common routines for numeric formatting *********************/\r | |
361 | /************************************************************************/\r | |
362 | \r | |
363 | /* Locale type codes. */\r | |
364 | #define LT_CURRENT_LOCALE 0\r | |
365 | #define LT_DEFAULT_LOCALE 1\r | |
366 | #define LT_NO_LOCALE 2\r | |
367 | \r | |
368 | /* Locale info needed for formatting integers and the part of floats\r | |
369 | before and including the decimal. Note that locales only support\r | |
370 | 8-bit chars, not unicode. */\r | |
371 | typedef struct {\r | |
372 | char *decimal_point;\r | |
373 | char *thousands_sep;\r | |
374 | char *grouping;\r | |
375 | } LocaleInfo;\r | |
376 | \r | |
377 | /* describes the layout for an integer, see the comment in\r | |
378 | calc_number_widths() for details */\r | |
379 | typedef struct {\r | |
380 | Py_ssize_t n_lpadding;\r | |
381 | Py_ssize_t n_prefix;\r | |
382 | Py_ssize_t n_spadding;\r | |
383 | Py_ssize_t n_rpadding;\r | |
384 | char sign;\r | |
385 | Py_ssize_t n_sign; /* number of digits needed for sign (0/1) */\r | |
386 | Py_ssize_t n_grouped_digits; /* Space taken up by the digits, including\r | |
387 | any grouping chars. */\r | |
388 | Py_ssize_t n_decimal; /* 0 if only an integer */\r | |
389 | Py_ssize_t n_remainder; /* Digits in decimal and/or exponent part,\r | |
390 | excluding the decimal itself, if\r | |
391 | present. */\r | |
392 | \r | |
393 | /* These 2 are not the widths of fields, but are needed by\r | |
394 | STRINGLIB_GROUPING. */\r | |
395 | Py_ssize_t n_digits; /* The number of digits before a decimal\r | |
396 | or exponent. */\r | |
397 | Py_ssize_t n_min_width; /* The min_width we used when we computed\r | |
398 | the n_grouped_digits width. */\r | |
399 | } NumberFieldWidths;\r | |
400 | \r | |
401 | \r | |
402 | /* Given a number of the form:\r | |
403 | digits[remainder]\r | |
404 | where ptr points to the start and end points to the end, find where\r | |
405 | the integer part ends. This could be a decimal, an exponent, both,\r | |
406 | or neither.\r | |
407 | If a decimal point is present, set *has_decimal and increment\r | |
408 | remainder beyond it.\r | |
409 | Results are undefined (but shouldn't crash) for improperly\r | |
410 | formatted strings.\r | |
411 | */\r | |
412 | static void\r | |
413 | parse_number(STRINGLIB_CHAR *ptr, Py_ssize_t len,\r | |
414 | Py_ssize_t *n_remainder, int *has_decimal)\r | |
415 | {\r | |
416 | STRINGLIB_CHAR *end = ptr + len;\r | |
417 | STRINGLIB_CHAR *remainder;\r | |
418 | \r | |
419 | while (ptr<end && isdigit(*ptr))\r | |
420 | ++ptr;\r | |
421 | remainder = ptr;\r | |
422 | \r | |
423 | /* Does remainder start with a decimal point? */\r | |
424 | *has_decimal = ptr<end && *remainder == '.';\r | |
425 | \r | |
426 | /* Skip the decimal point. */\r | |
427 | if (*has_decimal)\r | |
428 | remainder++;\r | |
429 | \r | |
430 | *n_remainder = end - remainder;\r | |
431 | }\r | |
432 | \r | |
433 | /* not all fields of format are used. for example, precision is\r | |
434 | unused. should this take discrete params in order to be more clear\r | |
435 | about what it does? or is passing a single format parameter easier\r | |
436 | and more efficient enough to justify a little obfuscation? */\r | |
437 | static Py_ssize_t\r | |
438 | calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,\r | |
439 | STRINGLIB_CHAR sign_char, STRINGLIB_CHAR *number,\r | |
440 | Py_ssize_t n_number, Py_ssize_t n_remainder,\r | |
441 | int has_decimal, const LocaleInfo *locale,\r | |
442 | const InternalFormatSpec *format)\r | |
443 | {\r | |
444 | Py_ssize_t n_non_digit_non_padding;\r | |
445 | Py_ssize_t n_padding;\r | |
446 | \r | |
447 | spec->n_digits = n_number - n_remainder - (has_decimal?1:0);\r | |
448 | spec->n_lpadding = 0;\r | |
449 | spec->n_prefix = n_prefix;\r | |
450 | spec->n_decimal = has_decimal ? strlen(locale->decimal_point) : 0;\r | |
451 | spec->n_remainder = n_remainder;\r | |
452 | spec->n_spadding = 0;\r | |
453 | spec->n_rpadding = 0;\r | |
454 | spec->sign = '\0';\r | |
455 | spec->n_sign = 0;\r | |
456 | \r | |
457 | /* the output will look like:\r | |
458 | | |\r | |
459 | | <lpadding> <sign> <prefix> <spadding> <grouped_digits> <decimal> <remainder> <rpadding> |\r | |
460 | | |\r | |
461 | \r | |
462 | sign is computed from format->sign and the actual\r | |
463 | sign of the number\r | |
464 | \r | |
465 | prefix is given (it's for the '0x' prefix)\r | |
466 | \r | |
467 | digits is already known\r | |
468 | \r | |
469 | the total width is either given, or computed from the\r | |
470 | actual digits\r | |
471 | \r | |
472 | only one of lpadding, spadding, and rpadding can be non-zero,\r | |
473 | and it's calculated from the width and other fields\r | |
474 | */\r | |
475 | \r | |
476 | /* compute the various parts we're going to write */\r | |
477 | switch (format->sign) {\r | |
478 | case '+':\r | |
479 | /* always put a + or - */\r | |
480 | spec->n_sign = 1;\r | |
481 | spec->sign = (sign_char == '-' ? '-' : '+');\r | |
482 | break;\r | |
483 | case ' ':\r | |
484 | spec->n_sign = 1;\r | |
485 | spec->sign = (sign_char == '-' ? '-' : ' ');\r | |
486 | break;\r | |
487 | default:\r | |
488 | /* Not specified, or the default (-) */\r | |
489 | if (sign_char == '-') {\r | |
490 | spec->n_sign = 1;\r | |
491 | spec->sign = '-';\r | |
492 | }\r | |
493 | }\r | |
494 | \r | |
495 | /* The number of chars used for non-digits and non-padding. */\r | |
496 | n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal +\r | |
497 | spec->n_remainder;\r | |
498 | \r | |
499 | /* min_width can go negative, that's okay. format->width == -1 means\r | |
500 | we don't care. */\r | |
501 | if (format->fill_char == '0' && format->align == '=')\r | |
502 | spec->n_min_width = format->width - n_non_digit_non_padding;\r | |
503 | else\r | |
504 | spec->n_min_width = 0;\r | |
505 | \r | |
506 | if (spec->n_digits == 0)\r | |
507 | /* This case only occurs when using 'c' formatting, we need\r | |
508 | to special case it because the grouping code always wants\r | |
509 | to have at least one character. */\r | |
510 | spec->n_grouped_digits = 0;\r | |
511 | else\r | |
512 | spec->n_grouped_digits = STRINGLIB_GROUPING(NULL, 0, NULL,\r | |
513 | spec->n_digits,\r | |
514 | spec->n_min_width,\r | |
515 | locale->grouping,\r | |
516 | locale->thousands_sep);\r | |
517 | \r | |
518 | /* Given the desired width and the total of digit and non-digit\r | |
519 | space we consume, see if we need any padding. format->width can\r | |
520 | be negative (meaning no padding), but this code still works in\r | |
521 | that case. */\r | |
522 | n_padding = format->width -\r | |
523 | (n_non_digit_non_padding + spec->n_grouped_digits);\r | |
524 | if (n_padding > 0) {\r | |
525 | /* Some padding is needed. Determine if it's left, space, or right. */\r | |
526 | switch (format->align) {\r | |
527 | case '<':\r | |
528 | spec->n_rpadding = n_padding;\r | |
529 | break;\r | |
530 | case '^':\r | |
531 | spec->n_lpadding = n_padding / 2;\r | |
532 | spec->n_rpadding = n_padding - spec->n_lpadding;\r | |
533 | break;\r | |
534 | case '=':\r | |
535 | spec->n_spadding = n_padding;\r | |
536 | break;\r | |
537 | case '>':\r | |
538 | spec->n_lpadding = n_padding;\r | |
539 | break;\r | |
540 | default:\r | |
541 | /* Shouldn't get here, but treat it as '>' */\r | |
542 | spec->n_lpadding = n_padding;\r | |
543 | assert(0);\r | |
544 | break;\r | |
545 | }\r | |
546 | }\r | |
547 | return spec->n_lpadding + spec->n_sign + spec->n_prefix +\r | |
548 | spec->n_spadding + spec->n_grouped_digits + spec->n_decimal +\r | |
549 | spec->n_remainder + spec->n_rpadding;\r | |
550 | }\r | |
551 | \r | |
552 | /* Fill in the digit parts of a numbers's string representation,\r | |
553 | as determined in calc_number_widths().\r | |
554 | No error checking, since we know the buffer is the correct size. */\r | |
555 | static void\r | |
556 | fill_number(STRINGLIB_CHAR *buf, const NumberFieldWidths *spec,\r | |
557 | STRINGLIB_CHAR *digits, Py_ssize_t n_digits,\r | |
558 | STRINGLIB_CHAR *prefix, STRINGLIB_CHAR fill_char,\r | |
559 | LocaleInfo *locale, int toupper)\r | |
560 | {\r | |
561 | /* Used to keep track of digits, decimal, and remainder. */\r | |
562 | STRINGLIB_CHAR *p = digits;\r | |
563 | \r | |
564 | #ifndef NDEBUG\r | |
565 | Py_ssize_t r;\r | |
566 | #endif\r | |
567 | \r | |
568 | if (spec->n_lpadding) {\r | |
569 | STRINGLIB_FILL(buf, fill_char, spec->n_lpadding);\r | |
570 | buf += spec->n_lpadding;\r | |
571 | }\r | |
572 | if (spec->n_sign == 1) {\r | |
573 | *buf++ = spec->sign;\r | |
574 | }\r | |
575 | if (spec->n_prefix) {\r | |
576 | memmove(buf,\r | |
577 | prefix,\r | |
578 | spec->n_prefix * sizeof(STRINGLIB_CHAR));\r | |
579 | if (toupper) {\r | |
580 | Py_ssize_t t;\r | |
581 | for (t = 0; t < spec->n_prefix; ++t)\r | |
582 | buf[t] = STRINGLIB_TOUPPER(buf[t]);\r | |
583 | }\r | |
584 | buf += spec->n_prefix;\r | |
585 | }\r | |
586 | if (spec->n_spadding) {\r | |
587 | STRINGLIB_FILL(buf, fill_char, spec->n_spadding);\r | |
588 | buf += spec->n_spadding;\r | |
589 | }\r | |
590 | \r | |
591 | /* Only for type 'c' special case, it has no digits. */\r | |
592 | if (spec->n_digits != 0) {\r | |
593 | /* Fill the digits with InsertThousandsGrouping. */\r | |
594 | #ifndef NDEBUG\r | |
595 | r =\r | |
596 | #endif\r | |
597 | STRINGLIB_GROUPING(buf, spec->n_grouped_digits, digits,\r | |
598 | spec->n_digits, spec->n_min_width,\r | |
599 | locale->grouping, locale->thousands_sep);\r | |
600 | #ifndef NDEBUG\r | |
601 | assert(r == spec->n_grouped_digits);\r | |
602 | #endif\r | |
603 | p += spec->n_digits;\r | |
604 | }\r | |
605 | if (toupper) {\r | |
606 | Py_ssize_t t;\r | |
607 | for (t = 0; t < spec->n_grouped_digits; ++t)\r | |
608 | buf[t] = STRINGLIB_TOUPPER(buf[t]);\r | |
609 | }\r | |
610 | buf += spec->n_grouped_digits;\r | |
611 | \r | |
612 | if (spec->n_decimal) {\r | |
613 | Py_ssize_t t;\r | |
614 | for (t = 0; t < spec->n_decimal; ++t)\r | |
615 | buf[t] = locale->decimal_point[t];\r | |
616 | buf += spec->n_decimal;\r | |
617 | p += 1;\r | |
618 | }\r | |
619 | \r | |
620 | if (spec->n_remainder) {\r | |
621 | memcpy(buf, p, spec->n_remainder * sizeof(STRINGLIB_CHAR));\r | |
622 | buf += spec->n_remainder;\r | |
623 | p += spec->n_remainder;\r | |
624 | }\r | |
625 | \r | |
626 | if (spec->n_rpadding) {\r | |
627 | STRINGLIB_FILL(buf, fill_char, spec->n_rpadding);\r | |
628 | buf += spec->n_rpadding;\r | |
629 | }\r | |
630 | }\r | |
631 | \r | |
632 | static char no_grouping[1] = {CHAR_MAX};\r | |
633 | \r | |
634 | /* Find the decimal point character(s?), thousands_separator(s?), and\r | |
635 | grouping description, either for the current locale if type is\r | |
636 | LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE, or\r | |
637 | none if LT_NO_LOCALE. */\r | |
638 | static void\r | |
639 | get_locale_info(int type, LocaleInfo *locale_info)\r | |
640 | {\r | |
641 | switch (type) {\r | |
642 | case LT_CURRENT_LOCALE: {\r | |
643 | struct lconv *locale_data = localeconv();\r | |
644 | locale_info->decimal_point = locale_data->decimal_point;\r | |
645 | locale_info->thousands_sep = locale_data->thousands_sep;\r | |
646 | locale_info->grouping = locale_data->grouping;\r | |
647 | break;\r | |
648 | }\r | |
649 | case LT_DEFAULT_LOCALE:\r | |
650 | locale_info->decimal_point = ".";\r | |
651 | locale_info->thousands_sep = ",";\r | |
652 | locale_info->grouping = "\3"; /* Group every 3 characters. The\r | |
653 | (implicit) trailing 0 means repeat\r | |
654 | infinitely. */\r | |
655 | break;\r | |
656 | case LT_NO_LOCALE:\r | |
657 | locale_info->decimal_point = ".";\r | |
658 | locale_info->thousands_sep = "";\r | |
659 | locale_info->grouping = no_grouping;\r | |
660 | break;\r | |
661 | default:\r | |
662 | assert(0);\r | |
663 | }\r | |
664 | }\r | |
665 | \r | |
666 | #endif /* FORMAT_FLOAT || FORMAT_LONG || FORMAT_COMPLEX */\r | |
667 | \r | |
668 | /************************************************************************/\r | |
669 | /*********** string formatting ******************************************/\r | |
670 | /************************************************************************/\r | |
671 | \r | |
672 | static PyObject *\r | |
673 | format_string_internal(PyObject *value, const InternalFormatSpec *format)\r | |
674 | {\r | |
675 | Py_ssize_t lpad;\r | |
676 | Py_ssize_t rpad;\r | |
677 | Py_ssize_t total;\r | |
678 | STRINGLIB_CHAR *p;\r | |
679 | Py_ssize_t len = STRINGLIB_LEN(value);\r | |
680 | PyObject *result = NULL;\r | |
681 | \r | |
682 | /* sign is not allowed on strings */\r | |
683 | if (format->sign != '\0') {\r | |
684 | PyErr_SetString(PyExc_ValueError,\r | |
685 | "Sign not allowed in string format specifier");\r | |
686 | goto done;\r | |
687 | }\r | |
688 | \r | |
689 | /* alternate is not allowed on strings */\r | |
690 | if (format->alternate) {\r | |
691 | PyErr_SetString(PyExc_ValueError,\r | |
692 | "Alternate form (#) not allowed in string format "\r | |
693 | "specifier");\r | |
694 | goto done;\r | |
695 | }\r | |
696 | \r | |
697 | /* '=' alignment not allowed on strings */\r | |
698 | if (format->align == '=') {\r | |
699 | PyErr_SetString(PyExc_ValueError,\r | |
700 | "'=' alignment not allowed "\r | |
701 | "in string format specifier");\r | |
702 | goto done;\r | |
703 | }\r | |
704 | \r | |
705 | /* if precision is specified, output no more that format.precision\r | |
706 | characters */\r | |
707 | if (format->precision >= 0 && len >= format->precision) {\r | |
708 | len = format->precision;\r | |
709 | }\r | |
710 | \r | |
711 | calc_padding(len, format->width, format->align, &lpad, &rpad, &total);\r | |
712 | \r | |
713 | /* allocate the resulting string */\r | |
714 | result = STRINGLIB_NEW(NULL, total);\r | |
715 | if (result == NULL)\r | |
716 | goto done;\r | |
717 | \r | |
718 | /* Write into that space. First the padding. */\r | |
719 | p = fill_padding(STRINGLIB_STR(result), len,\r | |
720 | format->fill_char=='\0'?' ':format->fill_char,\r | |
721 | lpad, rpad);\r | |
722 | \r | |
723 | /* Then the source string. */\r | |
724 | memcpy(p, STRINGLIB_STR(value), len * sizeof(STRINGLIB_CHAR));\r | |
725 | \r | |
726 | done:\r | |
727 | return result;\r | |
728 | }\r | |
729 | \r | |
730 | \r | |
731 | /************************************************************************/\r | |
732 | /*********** long formatting ********************************************/\r | |
733 | /************************************************************************/\r | |
734 | \r | |
735 | #if defined FORMAT_LONG || defined FORMAT_INT\r | |
736 | typedef PyObject*\r | |
737 | (*IntOrLongToString)(PyObject *value, int base);\r | |
738 | \r | |
739 | static PyObject *\r | |
740 | format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,\r | |
741 | IntOrLongToString tostring)\r | |
742 | {\r | |
743 | PyObject *result = NULL;\r | |
744 | PyObject *tmp = NULL;\r | |
745 | STRINGLIB_CHAR *pnumeric_chars;\r | |
746 | STRINGLIB_CHAR numeric_char;\r | |
747 | STRINGLIB_CHAR sign_char = '\0';\r | |
748 | Py_ssize_t n_digits; /* count of digits need from the computed\r | |
749 | string */\r | |
750 | Py_ssize_t n_remainder = 0; /* Used only for 'c' formatting, which\r | |
751 | produces non-digits */\r | |
752 | Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */\r | |
753 | Py_ssize_t n_total;\r | |
754 | STRINGLIB_CHAR *prefix = NULL;\r | |
755 | NumberFieldWidths spec;\r | |
756 | long x;\r | |
757 | \r | |
758 | /* Locale settings, either from the actual locale or\r | |
759 | from a hard-code pseudo-locale */\r | |
760 | LocaleInfo locale;\r | |
761 | \r | |
762 | /* no precision allowed on integers */\r | |
763 | if (format->precision != -1) {\r | |
764 | PyErr_SetString(PyExc_ValueError,\r | |
765 | "Precision not allowed in integer format specifier");\r | |
766 | goto done;\r | |
767 | }\r | |
768 | \r | |
769 | /* special case for character formatting */\r | |
770 | if (format->type == 'c') {\r | |
771 | /* error to specify a sign */\r | |
772 | if (format->sign != '\0') {\r | |
773 | PyErr_SetString(PyExc_ValueError,\r | |
774 | "Sign not allowed with integer"\r | |
775 | " format specifier 'c'");\r | |
776 | goto done;\r | |
777 | }\r | |
778 | \r | |
779 | /* Error to specify a comma. */\r | |
780 | if (format->thousands_separators) {\r | |
781 | PyErr_SetString(PyExc_ValueError,\r | |
782 | "Thousands separators not allowed with integer"\r | |
783 | " format specifier 'c'");\r | |
784 | goto done;\r | |
785 | }\r | |
786 | \r | |
787 | /* taken from unicodeobject.c formatchar() */\r | |
788 | /* Integer input truncated to a character */\r | |
789 | /* XXX: won't work for int */\r | |
790 | x = PyLong_AsLong(value);\r | |
791 | if (x == -1 && PyErr_Occurred())\r | |
792 | goto done;\r | |
793 | #ifdef Py_UNICODE_WIDE\r | |
794 | if (x < 0 || x > 0x10ffff) {\r | |
795 | PyErr_SetString(PyExc_OverflowError,\r | |
796 | "%c arg not in range(0x110000) "\r | |
797 | "(wide Python build)");\r | |
798 | goto done;\r | |
799 | }\r | |
800 | #else\r | |
801 | if (x < 0 || x > 0xffff) {\r | |
802 | PyErr_SetString(PyExc_OverflowError,\r | |
803 | "%c arg not in range(0x10000) "\r | |
804 | "(narrow Python build)");\r | |
805 | goto done;\r | |
806 | }\r | |
807 | #endif\r | |
808 | numeric_char = (STRINGLIB_CHAR)x;\r | |
809 | pnumeric_chars = &numeric_char;\r | |
810 | n_digits = 1;\r | |
811 | \r | |
812 | /* As a sort-of hack, we tell calc_number_widths that we only\r | |
813 | have "remainder" characters. calc_number_widths thinks\r | |
814 | these are characters that don't get formatted, only copied\r | |
815 | into the output string. We do this for 'c' formatting,\r | |
816 | because the characters are likely to be non-digits. */\r | |
817 | n_remainder = 1;\r | |
818 | }\r | |
819 | else {\r | |
820 | int base;\r | |
821 | int leading_chars_to_skip = 0; /* Number of characters added by\r | |
822 | PyNumber_ToBase that we want to\r | |
823 | skip over. */\r | |
824 | \r | |
825 | /* Compute the base and how many characters will be added by\r | |
826 | PyNumber_ToBase */\r | |
827 | switch (format->type) {\r | |
828 | case 'b':\r | |
829 | base = 2;\r | |
830 | leading_chars_to_skip = 2; /* 0b */\r | |
831 | break;\r | |
832 | case 'o':\r | |
833 | base = 8;\r | |
834 | leading_chars_to_skip = 2; /* 0o */\r | |
835 | break;\r | |
836 | case 'x':\r | |
837 | case 'X':\r | |
838 | base = 16;\r | |
839 | leading_chars_to_skip = 2; /* 0x */\r | |
840 | break;\r | |
841 | default: /* shouldn't be needed, but stops a compiler warning */\r | |
842 | case 'd':\r | |
843 | case 'n':\r | |
844 | base = 10;\r | |
845 | break;\r | |
846 | }\r | |
847 | \r | |
848 | /* The number of prefix chars is the same as the leading\r | |
849 | chars to skip */\r | |
850 | if (format->alternate)\r | |
851 | n_prefix = leading_chars_to_skip;\r | |
852 | \r | |
853 | /* Do the hard part, converting to a string in a given base */\r | |
854 | tmp = tostring(value, base);\r | |
855 | if (tmp == NULL)\r | |
856 | goto done;\r | |
857 | \r | |
858 | pnumeric_chars = STRINGLIB_STR(tmp);\r | |
859 | n_digits = STRINGLIB_LEN(tmp);\r | |
860 | \r | |
861 | prefix = pnumeric_chars;\r | |
862 | \r | |
863 | /* Remember not to modify what pnumeric_chars points to. it\r | |
864 | might be interned. Only modify it after we copy it into a\r | |
865 | newly allocated output buffer. */\r | |
866 | \r | |
867 | /* Is a sign character present in the output? If so, remember it\r | |
868 | and skip it */\r | |
869 | if (pnumeric_chars[0] == '-') {\r | |
870 | sign_char = pnumeric_chars[0];\r | |
871 | ++prefix;\r | |
872 | ++leading_chars_to_skip;\r | |
873 | }\r | |
874 | \r | |
875 | /* Skip over the leading chars (0x, 0b, etc.) */\r | |
876 | n_digits -= leading_chars_to_skip;\r | |
877 | pnumeric_chars += leading_chars_to_skip;\r | |
878 | }\r | |
879 | \r | |
880 | /* Determine the grouping, separator, and decimal point, if any. */\r | |
881 | get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :\r | |
882 | (format->thousands_separators ?\r | |
883 | LT_DEFAULT_LOCALE :\r | |
884 | LT_NO_LOCALE),\r | |
885 | &locale);\r | |
886 | \r | |
887 | /* Calculate how much memory we'll need. */\r | |
888 | n_total = calc_number_widths(&spec, n_prefix, sign_char, pnumeric_chars,\r | |
889 | n_digits, n_remainder, 0, &locale, format);\r | |
890 | \r | |
891 | /* Allocate the memory. */\r | |
892 | result = STRINGLIB_NEW(NULL, n_total);\r | |
893 | if (!result)\r | |
894 | goto done;\r | |
895 | \r | |
896 | /* Populate the memory. */\r | |
897 | fill_number(STRINGLIB_STR(result), &spec, pnumeric_chars, n_digits,\r | |
898 | prefix, format->fill_char == '\0' ? ' ' : format->fill_char,\r | |
899 | &locale, format->type == 'X');\r | |
900 | \r | |
901 | done:\r | |
902 | Py_XDECREF(tmp);\r | |
903 | return result;\r | |
904 | }\r | |
905 | #endif /* defined FORMAT_LONG || defined FORMAT_INT */\r | |
906 | \r | |
907 | /************************************************************************/\r | |
908 | /*********** float formatting *******************************************/\r | |
909 | /************************************************************************/\r | |
910 | \r | |
911 | #ifdef FORMAT_FLOAT\r | |
912 | #if STRINGLIB_IS_UNICODE\r | |
913 | static void\r | |
914 | strtounicode(Py_UNICODE *buffer, const char *charbuffer, Py_ssize_t len)\r | |
915 | {\r | |
916 | Py_ssize_t i;\r | |
917 | for (i = 0; i < len; ++i)\r | |
918 | buffer[i] = (Py_UNICODE)charbuffer[i];\r | |
919 | }\r | |
920 | #endif\r | |
921 | \r | |
922 | /* much of this is taken from unicodeobject.c */\r | |
923 | static PyObject *\r | |
924 | format_float_internal(PyObject *value,\r | |
925 | const InternalFormatSpec *format)\r | |
926 | {\r | |
927 | char *buf = NULL; /* buffer returned from PyOS_double_to_string */\r | |
928 | Py_ssize_t n_digits;\r | |
929 | Py_ssize_t n_remainder;\r | |
930 | Py_ssize_t n_total;\r | |
931 | int has_decimal;\r | |
932 | double val;\r | |
933 | Py_ssize_t precision = format->precision;\r | |
934 | Py_ssize_t default_precision = 6;\r | |
935 | STRINGLIB_CHAR type = format->type;\r | |
936 | int add_pct = 0;\r | |
937 | STRINGLIB_CHAR *p;\r | |
938 | NumberFieldWidths spec;\r | |
939 | int flags = 0;\r | |
940 | PyObject *result = NULL;\r | |
941 | STRINGLIB_CHAR sign_char = '\0';\r | |
942 | int float_type; /* Used to see if we have a nan, inf, or regular float. */\r | |
943 | \r | |
944 | #if STRINGLIB_IS_UNICODE\r | |
945 | Py_UNICODE *unicode_tmp = NULL;\r | |
946 | #endif\r | |
947 | \r | |
948 | /* Locale settings, either from the actual locale or\r | |
949 | from a hard-code pseudo-locale */\r | |
950 | LocaleInfo locale;\r | |
951 | \r | |
952 | /* Alternate is not allowed on floats. */\r | |
953 | if (format->alternate) {\r | |
954 | PyErr_SetString(PyExc_ValueError,\r | |
955 | "Alternate form (#) not allowed in float format "\r | |
956 | "specifier");\r | |
957 | goto done;\r | |
958 | }\r | |
959 | \r | |
960 | if (type == '\0') {\r | |
961 | /* Omitted type specifier. This is like 'g' but with at least one\r | |
962 | digit after the decimal point, and different default precision.*/\r | |
963 | type = 'g';\r | |
964 | default_precision = PyFloat_STR_PRECISION;\r | |
965 | flags |= Py_DTSF_ADD_DOT_0;\r | |
966 | }\r | |
967 | \r | |
968 | if (type == 'n')\r | |
969 | /* 'n' is the same as 'g', except for the locale used to\r | |
970 | format the result. We take care of that later. */\r | |
971 | type = 'g';\r | |
972 | \r | |
973 | val = PyFloat_AsDouble(value);\r | |
974 | if (val == -1.0 && PyErr_Occurred())\r | |
975 | goto done;\r | |
976 | \r | |
977 | if (type == '%') {\r | |
978 | type = 'f';\r | |
979 | val *= 100;\r | |
980 | add_pct = 1;\r | |
981 | }\r | |
982 | \r | |
983 | if (precision < 0)\r | |
984 | precision = default_precision;\r | |
985 | \r | |
986 | /* Cast "type", because if we're in unicode we need to pass a\r | |
987 | 8-bit char. This is safe, because we've restricted what "type"\r | |
988 | can be. */\r | |
989 | buf = PyOS_double_to_string(val, (char)type, precision, flags,\r | |
990 | &float_type);\r | |
991 | if (buf == NULL)\r | |
992 | goto done;\r | |
993 | n_digits = strlen(buf);\r | |
994 | \r | |
995 | if (add_pct) {\r | |
996 | /* We know that buf has a trailing zero (since we just called\r | |
997 | strlen() on it), and we don't use that fact any more. So we\r | |
998 | can just write over the trailing zero. */\r | |
999 | buf[n_digits] = '%';\r | |
1000 | n_digits += 1;\r | |
1001 | }\r | |
1002 | \r | |
1003 | /* Since there is no unicode version of PyOS_double_to_string,\r | |
1004 | just use the 8 bit version and then convert to unicode. */\r | |
1005 | #if STRINGLIB_IS_UNICODE\r | |
1006 | unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_digits)*sizeof(Py_UNICODE));\r | |
1007 | if (unicode_tmp == NULL) {\r | |
1008 | PyErr_NoMemory();\r | |
1009 | goto done;\r | |
1010 | }\r | |
1011 | strtounicode(unicode_tmp, buf, n_digits);\r | |
1012 | p = unicode_tmp;\r | |
1013 | #else\r | |
1014 | p = buf;\r | |
1015 | #endif\r | |
1016 | \r | |
1017 | /* Is a sign character present in the output? If so, remember it\r | |
1018 | and skip it */\r | |
1019 | if (*p == '-') {\r | |
1020 | sign_char = *p;\r | |
1021 | ++p;\r | |
1022 | --n_digits;\r | |
1023 | }\r | |
1024 | \r | |
1025 | /* Determine if we have any "remainder" (after the digits, might include\r | |
1026 | decimal or exponent or both (or neither)) */\r | |
1027 | parse_number(p, n_digits, &n_remainder, &has_decimal);\r | |
1028 | \r | |
1029 | /* Determine the grouping, separator, and decimal point, if any. */\r | |
1030 | get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :\r | |
1031 | (format->thousands_separators ?\r | |
1032 | LT_DEFAULT_LOCALE :\r | |
1033 | LT_NO_LOCALE),\r | |
1034 | &locale);\r | |
1035 | \r | |
1036 | /* Calculate how much memory we'll need. */\r | |
1037 | n_total = calc_number_widths(&spec, 0, sign_char, p, n_digits,\r | |
1038 | n_remainder, has_decimal, &locale, format);\r | |
1039 | \r | |
1040 | /* Allocate the memory. */\r | |
1041 | result = STRINGLIB_NEW(NULL, n_total);\r | |
1042 | if (result == NULL)\r | |
1043 | goto done;\r | |
1044 | \r | |
1045 | /* Populate the memory. */\r | |
1046 | fill_number(STRINGLIB_STR(result), &spec, p, n_digits, NULL,\r | |
1047 | format->fill_char == '\0' ? ' ' : format->fill_char, &locale,\r | |
1048 | 0);\r | |
1049 | \r | |
1050 | done:\r | |
1051 | PyMem_Free(buf);\r | |
1052 | #if STRINGLIB_IS_UNICODE\r | |
1053 | PyMem_Free(unicode_tmp);\r | |
1054 | #endif\r | |
1055 | return result;\r | |
1056 | }\r | |
1057 | #endif /* FORMAT_FLOAT */\r | |
1058 | \r | |
1059 | /************************************************************************/\r | |
1060 | /*********** complex formatting *****************************************/\r | |
1061 | /************************************************************************/\r | |
1062 | \r | |
1063 | #ifdef FORMAT_COMPLEX\r | |
1064 | \r | |
1065 | static PyObject *\r | |
1066 | format_complex_internal(PyObject *value,\r | |
1067 | const InternalFormatSpec *format)\r | |
1068 | {\r | |
1069 | double re;\r | |
1070 | double im;\r | |
1071 | char *re_buf = NULL; /* buffer returned from PyOS_double_to_string */\r | |
1072 | char *im_buf = NULL; /* buffer returned from PyOS_double_to_string */\r | |
1073 | \r | |
1074 | InternalFormatSpec tmp_format = *format;\r | |
1075 | Py_ssize_t n_re_digits;\r | |
1076 | Py_ssize_t n_im_digits;\r | |
1077 | Py_ssize_t n_re_remainder;\r | |
1078 | Py_ssize_t n_im_remainder;\r | |
1079 | Py_ssize_t n_re_total;\r | |
1080 | Py_ssize_t n_im_total;\r | |
1081 | int re_has_decimal;\r | |
1082 | int im_has_decimal;\r | |
1083 | Py_ssize_t precision = format->precision;\r | |
1084 | Py_ssize_t default_precision = 6;\r | |
1085 | STRINGLIB_CHAR type = format->type;\r | |
1086 | STRINGLIB_CHAR *p_re;\r | |
1087 | STRINGLIB_CHAR *p_im;\r | |
1088 | NumberFieldWidths re_spec;\r | |
1089 | NumberFieldWidths im_spec;\r | |
1090 | int flags = 0;\r | |
1091 | PyObject *result = NULL;\r | |
1092 | STRINGLIB_CHAR *p;\r | |
1093 | STRINGLIB_CHAR re_sign_char = '\0';\r | |
1094 | STRINGLIB_CHAR im_sign_char = '\0';\r | |
1095 | int re_float_type; /* Used to see if we have a nan, inf, or regular float. */\r | |
1096 | int im_float_type;\r | |
1097 | int add_parens = 0;\r | |
1098 | int skip_re = 0;\r | |
1099 | Py_ssize_t lpad;\r | |
1100 | Py_ssize_t rpad;\r | |
1101 | Py_ssize_t total;\r | |
1102 | \r | |
1103 | #if STRINGLIB_IS_UNICODE\r | |
1104 | Py_UNICODE *re_unicode_tmp = NULL;\r | |
1105 | Py_UNICODE *im_unicode_tmp = NULL;\r | |
1106 | #endif\r | |
1107 | \r | |
1108 | /* Locale settings, either from the actual locale or\r | |
1109 | from a hard-code pseudo-locale */\r | |
1110 | LocaleInfo locale;\r | |
1111 | \r | |
1112 | /* Alternate is not allowed on complex. */\r | |
1113 | if (format->alternate) {\r | |
1114 | PyErr_SetString(PyExc_ValueError,\r | |
1115 | "Alternate form (#) not allowed in complex format "\r | |
1116 | "specifier");\r | |
1117 | goto done;\r | |
1118 | }\r | |
1119 | \r | |
1120 | /* Neither is zero pading. */\r | |
1121 | if (format->fill_char == '0') {\r | |
1122 | PyErr_SetString(PyExc_ValueError,\r | |
1123 | "Zero padding is not allowed in complex format "\r | |
1124 | "specifier");\r | |
1125 | goto done;\r | |
1126 | }\r | |
1127 | \r | |
1128 | /* Neither is '=' alignment . */\r | |
1129 | if (format->align == '=') {\r | |
1130 | PyErr_SetString(PyExc_ValueError,\r | |
1131 | "'=' alignment flag is not allowed in complex format "\r | |
1132 | "specifier");\r | |
1133 | goto done;\r | |
1134 | }\r | |
1135 | \r | |
1136 | re = PyComplex_RealAsDouble(value);\r | |
1137 | if (re == -1.0 && PyErr_Occurred())\r | |
1138 | goto done;\r | |
1139 | im = PyComplex_ImagAsDouble(value);\r | |
1140 | if (im == -1.0 && PyErr_Occurred())\r | |
1141 | goto done;\r | |
1142 | \r | |
1143 | if (type == '\0') {\r | |
1144 | /* Omitted type specifier. Should be like str(self). */\r | |
1145 | type = 'g';\r | |
1146 | default_precision = PyFloat_STR_PRECISION;\r | |
1147 | if (re == 0.0 && copysign(1.0, re) == 1.0)\r | |
1148 | skip_re = 1;\r | |
1149 | else\r | |
1150 | add_parens = 1;\r | |
1151 | }\r | |
1152 | \r | |
1153 | if (type == 'n')\r | |
1154 | /* 'n' is the same as 'g', except for the locale used to\r | |
1155 | format the result. We take care of that later. */\r | |
1156 | type = 'g';\r | |
1157 | \r | |
1158 | if (precision < 0)\r | |
1159 | precision = default_precision;\r | |
1160 | \r | |
1161 | /* Cast "type", because if we're in unicode we need to pass a\r | |
1162 | 8-bit char. This is safe, because we've restricted what "type"\r | |
1163 | can be. */\r | |
1164 | re_buf = PyOS_double_to_string(re, (char)type, precision, flags,\r | |
1165 | &re_float_type);\r | |
1166 | if (re_buf == NULL)\r | |
1167 | goto done;\r | |
1168 | im_buf = PyOS_double_to_string(im, (char)type, precision, flags,\r | |
1169 | &im_float_type);\r | |
1170 | if (im_buf == NULL)\r | |
1171 | goto done;\r | |
1172 | \r | |
1173 | n_re_digits = strlen(re_buf);\r | |
1174 | n_im_digits = strlen(im_buf);\r | |
1175 | \r | |
1176 | /* Since there is no unicode version of PyOS_double_to_string,\r | |
1177 | just use the 8 bit version and then convert to unicode. */\r | |
1178 | #if STRINGLIB_IS_UNICODE\r | |
1179 | re_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_re_digits)*sizeof(Py_UNICODE));\r | |
1180 | if (re_unicode_tmp == NULL) {\r | |
1181 | PyErr_NoMemory();\r | |
1182 | goto done;\r | |
1183 | }\r | |
1184 | strtounicode(re_unicode_tmp, re_buf, n_re_digits);\r | |
1185 | p_re = re_unicode_tmp;\r | |
1186 | \r | |
1187 | im_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_im_digits)*sizeof(Py_UNICODE));\r | |
1188 | if (im_unicode_tmp == NULL) {\r | |
1189 | PyErr_NoMemory();\r | |
1190 | goto done;\r | |
1191 | }\r | |
1192 | strtounicode(im_unicode_tmp, im_buf, n_im_digits);\r | |
1193 | p_im = im_unicode_tmp;\r | |
1194 | #else\r | |
1195 | p_re = re_buf;\r | |
1196 | p_im = im_buf;\r | |
1197 | #endif\r | |
1198 | \r | |
1199 | /* Is a sign character present in the output? If so, remember it\r | |
1200 | and skip it */\r | |
1201 | if (*p_re == '-') {\r | |
1202 | re_sign_char = *p_re;\r | |
1203 | ++p_re;\r | |
1204 | --n_re_digits;\r | |
1205 | }\r | |
1206 | if (*p_im == '-') {\r | |
1207 | im_sign_char = *p_im;\r | |
1208 | ++p_im;\r | |
1209 | --n_im_digits;\r | |
1210 | }\r | |
1211 | \r | |
1212 | /* Determine if we have any "remainder" (after the digits, might include\r | |
1213 | decimal or exponent or both (or neither)) */\r | |
1214 | parse_number(p_re, n_re_digits, &n_re_remainder, &re_has_decimal);\r | |
1215 | parse_number(p_im, n_im_digits, &n_im_remainder, &im_has_decimal);\r | |
1216 | \r | |
1217 | /* Determine the grouping, separator, and decimal point, if any. */\r | |
1218 | get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :\r | |
1219 | (format->thousands_separators ?\r | |
1220 | LT_DEFAULT_LOCALE :\r | |
1221 | LT_NO_LOCALE),\r | |
1222 | &locale);\r | |
1223 | \r | |
1224 | /* Turn off any padding. We'll do it later after we've composed\r | |
1225 | the numbers without padding. */\r | |
1226 | tmp_format.fill_char = '\0';\r | |
1227 | tmp_format.align = '<';\r | |
1228 | tmp_format.width = -1;\r | |
1229 | \r | |
1230 | /* Calculate how much memory we'll need. */\r | |
1231 | n_re_total = calc_number_widths(&re_spec, 0, re_sign_char, p_re,\r | |
1232 | n_re_digits, n_re_remainder,\r | |
1233 | re_has_decimal, &locale, &tmp_format);\r | |
1234 | \r | |
1235 | /* Same formatting, but always include a sign, unless the real part is\r | |
1236 | * going to be omitted, in which case we use whatever sign convention was\r | |
1237 | * requested by the original format. */\r | |
1238 | if (!skip_re)\r | |
1239 | tmp_format.sign = '+';\r | |
1240 | n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, p_im,\r | |
1241 | n_im_digits, n_im_remainder,\r | |
1242 | im_has_decimal, &locale, &tmp_format);\r | |
1243 | \r | |
1244 | if (skip_re)\r | |
1245 | n_re_total = 0;\r | |
1246 | \r | |
1247 | /* Add 1 for the 'j', and optionally 2 for parens. */\r | |
1248 | calc_padding(n_re_total + n_im_total + 1 + add_parens * 2,\r | |
1249 | format->width, format->align, &lpad, &rpad, &total);\r | |
1250 | \r | |
1251 | result = STRINGLIB_NEW(NULL, total);\r | |
1252 | if (result == NULL)\r | |
1253 | goto done;\r | |
1254 | \r | |
1255 | /* Populate the memory. First, the padding. */\r | |
1256 | p = fill_padding(STRINGLIB_STR(result),\r | |
1257 | n_re_total + n_im_total + 1 + add_parens * 2,\r | |
1258 | format->fill_char=='\0' ? ' ' : format->fill_char,\r | |
1259 | lpad, rpad);\r | |
1260 | \r | |
1261 | if (add_parens)\r | |
1262 | *p++ = '(';\r | |
1263 | \r | |
1264 | if (!skip_re) {\r | |
1265 | fill_number(p, &re_spec, p_re, n_re_digits, NULL, 0, &locale, 0);\r | |
1266 | p += n_re_total;\r | |
1267 | }\r | |
1268 | fill_number(p, &im_spec, p_im, n_im_digits, NULL, 0, &locale, 0);\r | |
1269 | p += n_im_total;\r | |
1270 | *p++ = 'j';\r | |
1271 | \r | |
1272 | if (add_parens)\r | |
1273 | *p++ = ')';\r | |
1274 | \r | |
1275 | done:\r | |
1276 | PyMem_Free(re_buf);\r | |
1277 | PyMem_Free(im_buf);\r | |
1278 | #if STRINGLIB_IS_UNICODE\r | |
1279 | PyMem_Free(re_unicode_tmp);\r | |
1280 | PyMem_Free(im_unicode_tmp);\r | |
1281 | #endif\r | |
1282 | return result;\r | |
1283 | }\r | |
1284 | #endif /* FORMAT_COMPLEX */\r | |
1285 | \r | |
1286 | /************************************************************************/\r | |
1287 | /*********** built in formatters ****************************************/\r | |
1288 | /************************************************************************/\r | |
1289 | PyObject *\r | |
1290 | FORMAT_STRING(PyObject *obj,\r | |
1291 | STRINGLIB_CHAR *format_spec,\r | |
1292 | Py_ssize_t format_spec_len)\r | |
1293 | {\r | |
1294 | InternalFormatSpec format;\r | |
1295 | PyObject *result = NULL;\r | |
1296 | \r | |
1297 | /* check for the special case of zero length format spec, make\r | |
1298 | it equivalent to str(obj) */\r | |
1299 | if (format_spec_len == 0) {\r | |
1300 | result = STRINGLIB_TOSTR(obj);\r | |
1301 | goto done;\r | |
1302 | }\r | |
1303 | \r | |
1304 | /* parse the format_spec */\r | |
1305 | if (!parse_internal_render_format_spec(format_spec, format_spec_len,\r | |
1306 | &format, 's', '<'))\r | |
1307 | goto done;\r | |
1308 | \r | |
1309 | /* type conversion? */\r | |
1310 | switch (format.type) {\r | |
1311 | case 's':\r | |
1312 | /* no type conversion needed, already a string. do the formatting */\r | |
1313 | result = format_string_internal(obj, &format);\r | |
1314 | break;\r | |
1315 | default:\r | |
1316 | /* unknown */\r | |
1317 | unknown_presentation_type(format.type, obj->ob_type->tp_name);\r | |
1318 | goto done;\r | |
1319 | }\r | |
1320 | \r | |
1321 | done:\r | |
1322 | return result;\r | |
1323 | }\r | |
1324 | \r | |
1325 | #if defined FORMAT_LONG || defined FORMAT_INT\r | |
1326 | static PyObject*\r | |
1327 | format_int_or_long(PyObject* obj,\r | |
1328 | STRINGLIB_CHAR *format_spec,\r | |
1329 | Py_ssize_t format_spec_len,\r | |
1330 | IntOrLongToString tostring)\r | |
1331 | {\r | |
1332 | PyObject *result = NULL;\r | |
1333 | PyObject *tmp = NULL;\r | |
1334 | InternalFormatSpec format;\r | |
1335 | \r | |
1336 | /* check for the special case of zero length format spec, make\r | |
1337 | it equivalent to str(obj) */\r | |
1338 | if (format_spec_len == 0) {\r | |
1339 | result = STRINGLIB_TOSTR(obj);\r | |
1340 | goto done;\r | |
1341 | }\r | |
1342 | \r | |
1343 | /* parse the format_spec */\r | |
1344 | if (!parse_internal_render_format_spec(format_spec,\r | |
1345 | format_spec_len,\r | |
1346 | &format, 'd', '>'))\r | |
1347 | goto done;\r | |
1348 | \r | |
1349 | /* type conversion? */\r | |
1350 | switch (format.type) {\r | |
1351 | case 'b':\r | |
1352 | case 'c':\r | |
1353 | case 'd':\r | |
1354 | case 'o':\r | |
1355 | case 'x':\r | |
1356 | case 'X':\r | |
1357 | case 'n':\r | |
1358 | /* no type conversion needed, already an int (or long). do\r | |
1359 | the formatting */\r | |
1360 | result = format_int_or_long_internal(obj, &format, tostring);\r | |
1361 | break;\r | |
1362 | \r | |
1363 | case 'e':\r | |
1364 | case 'E':\r | |
1365 | case 'f':\r | |
1366 | case 'F':\r | |
1367 | case 'g':\r | |
1368 | case 'G':\r | |
1369 | case '%':\r | |
1370 | /* convert to float */\r | |
1371 | tmp = PyNumber_Float(obj);\r | |
1372 | if (tmp == NULL)\r | |
1373 | goto done;\r | |
1374 | result = format_float_internal(tmp, &format);\r | |
1375 | break;\r | |
1376 | \r | |
1377 | default:\r | |
1378 | /* unknown */\r | |
1379 | unknown_presentation_type(format.type, obj->ob_type->tp_name);\r | |
1380 | goto done;\r | |
1381 | }\r | |
1382 | \r | |
1383 | done:\r | |
1384 | Py_XDECREF(tmp);\r | |
1385 | return result;\r | |
1386 | }\r | |
1387 | #endif /* FORMAT_LONG || defined FORMAT_INT */\r | |
1388 | \r | |
1389 | #ifdef FORMAT_LONG\r | |
1390 | /* Need to define long_format as a function that will convert a long\r | |
1391 | to a string. In 3.0, _PyLong_Format has the correct signature. In\r | |
1392 | 2.x, we need to fudge a few parameters */\r | |
1393 | #if PY_VERSION_HEX >= 0x03000000\r | |
1394 | #define long_format _PyLong_Format\r | |
1395 | #else\r | |
1396 | static PyObject*\r | |
1397 | long_format(PyObject* value, int base)\r | |
1398 | {\r | |
1399 | /* Convert to base, don't add trailing 'L', and use the new octal\r | |
1400 | format. We already know this is a long object */\r | |
1401 | assert(PyLong_Check(value));\r | |
1402 | /* convert to base, don't add 'L', and use the new octal format */\r | |
1403 | return _PyLong_Format(value, base, 0, 1);\r | |
1404 | }\r | |
1405 | #endif\r | |
1406 | \r | |
1407 | PyObject *\r | |
1408 | FORMAT_LONG(PyObject *obj,\r | |
1409 | STRINGLIB_CHAR *format_spec,\r | |
1410 | Py_ssize_t format_spec_len)\r | |
1411 | {\r | |
1412 | return format_int_or_long(obj, format_spec, format_spec_len,\r | |
1413 | long_format);\r | |
1414 | }\r | |
1415 | #endif /* FORMAT_LONG */\r | |
1416 | \r | |
1417 | #ifdef FORMAT_INT\r | |
1418 | /* this is only used for 2.x, not 3.0 */\r | |
1419 | static PyObject*\r | |
1420 | int_format(PyObject* value, int base)\r | |
1421 | {\r | |
1422 | /* Convert to base, and use the new octal format. We already\r | |
1423 | know this is an int object */\r | |
1424 | assert(PyInt_Check(value));\r | |
1425 | return _PyInt_Format((PyIntObject*)value, base, 1);\r | |
1426 | }\r | |
1427 | \r | |
1428 | PyObject *\r | |
1429 | FORMAT_INT(PyObject *obj,\r | |
1430 | STRINGLIB_CHAR *format_spec,\r | |
1431 | Py_ssize_t format_spec_len)\r | |
1432 | {\r | |
1433 | return format_int_or_long(obj, format_spec, format_spec_len,\r | |
1434 | int_format);\r | |
1435 | }\r | |
1436 | #endif /* FORMAT_INT */\r | |
1437 | \r | |
1438 | #ifdef FORMAT_FLOAT\r | |
1439 | PyObject *\r | |
1440 | FORMAT_FLOAT(PyObject *obj,\r | |
1441 | STRINGLIB_CHAR *format_spec,\r | |
1442 | Py_ssize_t format_spec_len)\r | |
1443 | {\r | |
1444 | PyObject *result = NULL;\r | |
1445 | InternalFormatSpec format;\r | |
1446 | \r | |
1447 | /* check for the special case of zero length format spec, make\r | |
1448 | it equivalent to str(obj) */\r | |
1449 | if (format_spec_len == 0) {\r | |
1450 | result = STRINGLIB_TOSTR(obj);\r | |
1451 | goto done;\r | |
1452 | }\r | |
1453 | \r | |
1454 | /* parse the format_spec */\r | |
1455 | if (!parse_internal_render_format_spec(format_spec,\r | |
1456 | format_spec_len,\r | |
1457 | &format, '\0', '>'))\r | |
1458 | goto done;\r | |
1459 | \r | |
1460 | /* type conversion? */\r | |
1461 | switch (format.type) {\r | |
1462 | case '\0': /* No format code: like 'g', but with at least one decimal. */\r | |
1463 | case 'e':\r | |
1464 | case 'E':\r | |
1465 | case 'f':\r | |
1466 | case 'F':\r | |
1467 | case 'g':\r | |
1468 | case 'G':\r | |
1469 | case 'n':\r | |
1470 | case '%':\r | |
1471 | /* no conversion, already a float. do the formatting */\r | |
1472 | result = format_float_internal(obj, &format);\r | |
1473 | break;\r | |
1474 | \r | |
1475 | default:\r | |
1476 | /* unknown */\r | |
1477 | unknown_presentation_type(format.type, obj->ob_type->tp_name);\r | |
1478 | goto done;\r | |
1479 | }\r | |
1480 | \r | |
1481 | done:\r | |
1482 | return result;\r | |
1483 | }\r | |
1484 | #endif /* FORMAT_FLOAT */\r | |
1485 | \r | |
1486 | #ifdef FORMAT_COMPLEX\r | |
1487 | PyObject *\r | |
1488 | FORMAT_COMPLEX(PyObject *obj,\r | |
1489 | STRINGLIB_CHAR *format_spec,\r | |
1490 | Py_ssize_t format_spec_len)\r | |
1491 | {\r | |
1492 | PyObject *result = NULL;\r | |
1493 | InternalFormatSpec format;\r | |
1494 | \r | |
1495 | /* check for the special case of zero length format spec, make\r | |
1496 | it equivalent to str(obj) */\r | |
1497 | if (format_spec_len == 0) {\r | |
1498 | result = STRINGLIB_TOSTR(obj);\r | |
1499 | goto done;\r | |
1500 | }\r | |
1501 | \r | |
1502 | /* parse the format_spec */\r | |
1503 | if (!parse_internal_render_format_spec(format_spec,\r | |
1504 | format_spec_len,\r | |
1505 | &format, '\0', '>'))\r | |
1506 | goto done;\r | |
1507 | \r | |
1508 | /* type conversion? */\r | |
1509 | switch (format.type) {\r | |
1510 | case '\0': /* No format code: like 'g', but with at least one decimal. */\r | |
1511 | case 'e':\r | |
1512 | case 'E':\r | |
1513 | case 'f':\r | |
1514 | case 'F':\r | |
1515 | case 'g':\r | |
1516 | case 'G':\r | |
1517 | case 'n':\r | |
1518 | /* no conversion, already a complex. do the formatting */\r | |
1519 | result = format_complex_internal(obj, &format);\r | |
1520 | break;\r | |
1521 | \r | |
1522 | default:\r | |
1523 | /* unknown */\r | |
1524 | unknown_presentation_type(format.type, obj->ob_type->tp_name);\r | |
1525 | goto done;\r | |
1526 | }\r | |
1527 | \r | |
1528 | done:\r | |
1529 | return result;\r | |
1530 | }\r | |
1531 | #endif /* FORMAT_COMPLEX */\r |