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