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