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