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