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 | |
| 4 | /* Before including this, you must include either: |
| 5 | stringlib/unicodedefs.h |
| 6 | stringlib/stringdefs.h |
| 7 | |
| 8 | Also, you should define the names: |
| 9 | FORMAT_STRING |
| 10 | FORMAT_LONG |
| 11 | FORMAT_FLOAT |
| 12 | to be whatever you want the public names of these functions to |
| 13 | be. These are the only non-static functions defined here. |
| 14 | */ |
| 15 | |
Eric Smith | b7f5ba1 | 2007-08-29 12:38:45 +0000 | [diff] [blame] | 16 | #define ALLOW_PARENS_FOR_SIGN 0 |
| 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 | 4430095 | 2007-08-29 12:43:12 +0000 | [diff] [blame] | 107 | #if ALLOW_PARENS_FOR_SIGN |
Eric Smith | b7f5ba1 | 2007-08-29 12:38:45 +0000 | [diff] [blame] | 108 | case '(': |
Eric Smith | 4430095 | 2007-08-29 12:43:12 +0000 | [diff] [blame] | 109 | #endif |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 110 | return 1; |
| 111 | default: |
| 112 | return 0; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | |
| 117 | typedef struct { |
| 118 | STRINGLIB_CHAR fill_char; |
| 119 | STRINGLIB_CHAR align; |
Eric Smith | b1ebcc6 | 2008-07-15 13:02:41 +0000 | [diff] [blame] | 120 | int alternate; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 121 | STRINGLIB_CHAR sign; |
| 122 | Py_ssize_t width; |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame^] | 123 | int thousands_separators; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 124 | Py_ssize_t precision; |
| 125 | STRINGLIB_CHAR type; |
| 126 | } InternalFormatSpec; |
| 127 | |
| 128 | /* |
| 129 | ptr points to the start of the format_spec, end points just past its end. |
| 130 | fills in format with the parsed information. |
| 131 | returns 1 on success, 0 on failure. |
| 132 | if failure, sets the exception |
| 133 | */ |
| 134 | static int |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 135 | parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec, |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 136 | Py_ssize_t format_spec_len, |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 137 | InternalFormatSpec *format, |
| 138 | char default_type) |
| 139 | { |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 140 | STRINGLIB_CHAR *ptr = format_spec; |
| 141 | STRINGLIB_CHAR *end = format_spec + format_spec_len; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 142 | |
| 143 | /* end-ptr is used throughout this code to specify the length of |
| 144 | the input string */ |
| 145 | |
| 146 | Py_ssize_t specified_width; |
| 147 | |
| 148 | format->fill_char = '\0'; |
| 149 | format->align = '\0'; |
Eric Smith | b1ebcc6 | 2008-07-15 13:02:41 +0000 | [diff] [blame] | 150 | format->alternate = 0; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 151 | format->sign = '\0'; |
| 152 | format->width = -1; |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame^] | 153 | format->thousands_separators = 0; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 154 | format->precision = -1; |
| 155 | format->type = default_type; |
| 156 | |
| 157 | /* If the second char is an alignment token, |
| 158 | then parse the fill char */ |
| 159 | if (end-ptr >= 2 && is_alignment_token(ptr[1])) { |
| 160 | format->align = ptr[1]; |
| 161 | format->fill_char = ptr[0]; |
| 162 | ptr += 2; |
Eric Smith | 0cb431c | 2007-08-28 01:07:27 +0000 | [diff] [blame] | 163 | } |
| 164 | else if (end-ptr >= 1 && is_alignment_token(ptr[0])) { |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 165 | format->align = ptr[0]; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 166 | ++ptr; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /* Parse the various sign options */ |
| 170 | if (end-ptr >= 1 && is_sign_element(ptr[0])) { |
| 171 | format->sign = ptr[0]; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 172 | ++ptr; |
Eric Smith | b7f5ba1 | 2007-08-29 12:38:45 +0000 | [diff] [blame] | 173 | #if ALLOW_PARENS_FOR_SIGN |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 174 | if (end-ptr >= 1 && ptr[0] == ')') { |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 175 | ++ptr; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 176 | } |
Eric Smith | b7f5ba1 | 2007-08-29 12:38:45 +0000 | [diff] [blame] | 177 | #endif |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 180 | /* If the next character is #, we're in alternate mode. This only |
| 181 | applies to integers. */ |
| 182 | if (end-ptr >= 1 && ptr[0] == '#') { |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 183 | format->alternate = 1; |
| 184 | ++ptr; |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 187 | /* The special case for 0-padding (backwards compat) */ |
Eric Smith | 185e30c | 2007-08-30 22:23:08 +0000 | [diff] [blame] | 188 | if (format->fill_char == '\0' && end-ptr >= 1 && ptr[0] == '0') { |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 189 | format->fill_char = '0'; |
| 190 | if (format->align == '\0') { |
| 191 | format->align = '='; |
| 192 | } |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 193 | ++ptr; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* XXX add error checking */ |
| 197 | specified_width = get_integer(&ptr, end, &format->width); |
| 198 | |
| 199 | /* if specified_width is 0, we didn't consume any characters for |
| 200 | the width. in that case, reset the width to -1, because |
| 201 | get_integer() will have set it to zero */ |
| 202 | if (specified_width == 0) { |
| 203 | format->width = -1; |
| 204 | } |
| 205 | |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame^] | 206 | /* Comma signifies add thousands separators */ |
| 207 | if (end-ptr && ptr[0] == ',') { |
| 208 | format->thousands_separators = 1; |
| 209 | ++ptr; |
| 210 | } |
| 211 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 212 | /* Parse field precision */ |
| 213 | if (end-ptr && ptr[0] == '.') { |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 214 | ++ptr; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 215 | |
| 216 | /* XXX add error checking */ |
| 217 | specified_width = get_integer(&ptr, end, &format->precision); |
| 218 | |
| 219 | /* not having a precision after a dot is an error */ |
| 220 | if (specified_width == 0) { |
| 221 | PyErr_Format(PyExc_ValueError, |
| 222 | "Format specifier missing precision"); |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | } |
| 227 | |
| 228 | /* Finally, parse the type field */ |
| 229 | |
| 230 | if (end-ptr > 1) { |
| 231 | /* invalid conversion spec */ |
| 232 | PyErr_Format(PyExc_ValueError, "Invalid conversion specification"); |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | if (end-ptr == 1) { |
| 237 | format->type = ptr[0]; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 238 | ++ptr; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame^] | 241 | if (format->type == 'n' && format->thousands_separators) { |
| 242 | PyErr_Format(PyExc_ValueError, "Cannot specify ',' with 'n'."); |
| 243 | return 0; |
| 244 | } |
| 245 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 246 | return 1; |
| 247 | } |
| 248 | |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 249 | #if defined FORMAT_FLOAT || defined FORMAT_LONG |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 250 | /************************************************************************/ |
| 251 | /*********** common routines for numeric formatting *********************/ |
| 252 | /************************************************************************/ |
| 253 | |
| 254 | /* describes the layout for an integer, see the comment in |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 255 | calc_number_widths() for details */ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 256 | typedef struct { |
| 257 | Py_ssize_t n_lpadding; |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 258 | Py_ssize_t n_prefix; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 259 | Py_ssize_t n_spadding; |
| 260 | Py_ssize_t n_rpadding; |
| 261 | char lsign; |
| 262 | Py_ssize_t n_lsign; |
| 263 | char rsign; |
| 264 | Py_ssize_t n_rsign; |
| 265 | Py_ssize_t n_total; /* just a convenience, it's derivable from the |
| 266 | other fields */ |
| 267 | } NumberFieldWidths; |
| 268 | |
| 269 | /* not all fields of format are used. for example, precision is |
| 270 | unused. should this take discrete params in order to be more clear |
| 271 | about what it does? or is passing a single format parameter easier |
| 272 | and more efficient enough to justify a little obfuscation? */ |
| 273 | static void |
Eric Smith | 05212a1 | 2008-07-16 19:41:14 +0000 | [diff] [blame] | 274 | calc_number_widths(NumberFieldWidths *spec, STRINGLIB_CHAR actual_sign, |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 275 | Py_ssize_t n_prefix, Py_ssize_t n_digits, |
| 276 | const InternalFormatSpec *format) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 277 | { |
Eric Smith | 05212a1 | 2008-07-16 19:41:14 +0000 | [diff] [blame] | 278 | spec->n_lpadding = 0; |
| 279 | spec->n_prefix = 0; |
| 280 | spec->n_spadding = 0; |
| 281 | spec->n_rpadding = 0; |
| 282 | spec->lsign = '\0'; |
| 283 | spec->n_lsign = 0; |
| 284 | spec->rsign = '\0'; |
| 285 | spec->n_rsign = 0; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 286 | |
| 287 | /* the output will look like: |
Eric Smith | b1ebcc6 | 2008-07-15 13:02:41 +0000 | [diff] [blame] | 288 | | | |
| 289 | | <lpadding> <lsign> <prefix> <spadding> <digits> <rsign> <rpadding> | |
| 290 | | | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 291 | |
| 292 | lsign and rsign are computed from format->sign and the actual |
| 293 | sign of the number |
| 294 | |
Eric Smith | b1ebcc6 | 2008-07-15 13:02:41 +0000 | [diff] [blame] | 295 | prefix is given (it's for the '0x' prefix) |
| 296 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 297 | digits is already known |
| 298 | |
| 299 | the total width is either given, or computed from the |
| 300 | actual digits |
| 301 | |
| 302 | only one of lpadding, spadding, and rpadding can be non-zero, |
| 303 | and it's calculated from the width and other fields |
| 304 | */ |
| 305 | |
| 306 | /* compute the various parts we're going to write */ |
| 307 | if (format->sign == '+') { |
| 308 | /* always put a + or - */ |
Eric Smith | 05212a1 | 2008-07-16 19:41:14 +0000 | [diff] [blame] | 309 | spec->n_lsign = 1; |
| 310 | spec->lsign = (actual_sign == '-' ? '-' : '+'); |
Eric Smith | 0cb431c | 2007-08-28 01:07:27 +0000 | [diff] [blame] | 311 | } |
Eric Smith | b7f5ba1 | 2007-08-29 12:38:45 +0000 | [diff] [blame] | 312 | #if ALLOW_PARENS_FOR_SIGN |
Eric Smith | 0cb431c | 2007-08-28 01:07:27 +0000 | [diff] [blame] | 313 | else if (format->sign == '(') { |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 314 | if (actual_sign == '-') { |
Eric Smith | 05212a1 | 2008-07-16 19:41:14 +0000 | [diff] [blame] | 315 | spec->n_lsign = 1; |
| 316 | spec->lsign = '('; |
| 317 | spec->n_rsign = 1; |
| 318 | spec->rsign = ')'; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 319 | } |
Eric Smith | 0cb431c | 2007-08-28 01:07:27 +0000 | [diff] [blame] | 320 | } |
Eric Smith | b7f5ba1 | 2007-08-29 12:38:45 +0000 | [diff] [blame] | 321 | #endif |
Eric Smith | 0cb431c | 2007-08-28 01:07:27 +0000 | [diff] [blame] | 322 | else if (format->sign == ' ') { |
Eric Smith | 05212a1 | 2008-07-16 19:41:14 +0000 | [diff] [blame] | 323 | spec->n_lsign = 1; |
| 324 | spec->lsign = (actual_sign == '-' ? '-' : ' '); |
Eric Smith | 0cb431c | 2007-08-28 01:07:27 +0000 | [diff] [blame] | 325 | } |
| 326 | else { |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 327 | /* non specified, or the default (-) */ |
| 328 | if (actual_sign == '-') { |
Eric Smith | 05212a1 | 2008-07-16 19:41:14 +0000 | [diff] [blame] | 329 | spec->n_lsign = 1; |
| 330 | spec->lsign = '-'; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
Eric Smith | 05212a1 | 2008-07-16 19:41:14 +0000 | [diff] [blame] | 334 | spec->n_prefix = n_prefix; |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 335 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 336 | /* now the number of padding characters */ |
| 337 | if (format->width == -1) { |
| 338 | /* no padding at all, nothing to do */ |
Eric Smith | 0cb431c | 2007-08-28 01:07:27 +0000 | [diff] [blame] | 339 | } |
| 340 | else { |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 341 | /* see if any padding is needed */ |
Eric Smith | 05212a1 | 2008-07-16 19:41:14 +0000 | [diff] [blame] | 342 | if (spec->n_lsign + n_digits + spec->n_rsign + |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 343 | spec->n_prefix >= format->width) { |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 344 | /* no padding needed, we're already bigger than the |
| 345 | requested width */ |
Eric Smith | 0cb431c | 2007-08-28 01:07:27 +0000 | [diff] [blame] | 346 | } |
| 347 | else { |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 348 | /* determine which of left, space, or right padding is |
| 349 | needed */ |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 350 | Py_ssize_t padding = format->width - |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 351 | (spec->n_lsign + spec->n_prefix + |
| 352 | n_digits + spec->n_rsign); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 353 | if (format->align == '<') |
Eric Smith | 05212a1 | 2008-07-16 19:41:14 +0000 | [diff] [blame] | 354 | spec->n_rpadding = padding; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 355 | else if (format->align == '>') |
Eric Smith | 05212a1 | 2008-07-16 19:41:14 +0000 | [diff] [blame] | 356 | spec->n_lpadding = padding; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 357 | else if (format->align == '^') { |
Eric Smith | 05212a1 | 2008-07-16 19:41:14 +0000 | [diff] [blame] | 358 | spec->n_lpadding = padding / 2; |
| 359 | spec->n_rpadding = padding - spec->n_lpadding; |
Eric Smith | 0cb431c | 2007-08-28 01:07:27 +0000 | [diff] [blame] | 360 | } |
Eric Smith | 185e30c | 2007-08-30 22:23:08 +0000 | [diff] [blame] | 361 | else if (format->align == '=') |
Eric Smith | 05212a1 | 2008-07-16 19:41:14 +0000 | [diff] [blame] | 362 | spec->n_spadding = padding; |
Eric Smith | 185e30c | 2007-08-30 22:23:08 +0000 | [diff] [blame] | 363 | else |
Eric Smith | 05212a1 | 2008-07-16 19:41:14 +0000 | [diff] [blame] | 364 | spec->n_lpadding = padding; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 365 | } |
| 366 | } |
Eric Smith | 05212a1 | 2008-07-16 19:41:14 +0000 | [diff] [blame] | 367 | spec->n_total = spec->n_lpadding + spec->n_lsign + spec->n_prefix + |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 368 | spec->n_spadding + n_digits + spec->n_rsign + spec->n_rpadding; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /* fill in the non-digit parts of a numbers's string representation, |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 372 | as determined in calc_number_widths(). returns the pointer to |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 373 | where the digits go. */ |
| 374 | static STRINGLIB_CHAR * |
Eric Smith | b151a45 | 2008-06-24 11:21:04 +0000 | [diff] [blame] | 375 | fill_non_digits(STRINGLIB_CHAR *p_buf, const NumberFieldWidths *spec, |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 376 | STRINGLIB_CHAR *prefix, Py_ssize_t n_digits, |
| 377 | STRINGLIB_CHAR fill_char) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 378 | { |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 379 | STRINGLIB_CHAR *p_digits; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 380 | |
| 381 | if (spec->n_lpadding) { |
| 382 | STRINGLIB_FILL(p_buf, fill_char, spec->n_lpadding); |
| 383 | p_buf += spec->n_lpadding; |
| 384 | } |
| 385 | if (spec->n_lsign == 1) { |
| 386 | *p_buf++ = spec->lsign; |
| 387 | } |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 388 | if (spec->n_prefix) { |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 389 | memmove(p_buf, |
| 390 | prefix, |
| 391 | spec->n_prefix * sizeof(STRINGLIB_CHAR)); |
| 392 | p_buf += spec->n_prefix; |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 393 | } |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 394 | if (spec->n_spadding) { |
| 395 | STRINGLIB_FILL(p_buf, fill_char, spec->n_spadding); |
| 396 | p_buf += spec->n_spadding; |
| 397 | } |
| 398 | p_digits = p_buf; |
| 399 | p_buf += n_digits; |
| 400 | if (spec->n_rsign == 1) { |
| 401 | *p_buf++ = spec->rsign; |
| 402 | } |
| 403 | if (spec->n_rpadding) { |
| 404 | STRINGLIB_FILL(p_buf, fill_char, spec->n_rpadding); |
| 405 | p_buf += spec->n_rpadding; |
| 406 | } |
| 407 | return p_digits; |
| 408 | } |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 409 | #endif /* FORMAT_FLOAT || FORMAT_LONG */ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 410 | |
| 411 | /************************************************************************/ |
| 412 | /*********** string formatting ******************************************/ |
| 413 | /************************************************************************/ |
| 414 | |
| 415 | static PyObject * |
| 416 | format_string_internal(PyObject *value, const InternalFormatSpec *format) |
| 417 | { |
| 418 | Py_ssize_t width; /* total field width */ |
| 419 | Py_ssize_t lpad; |
| 420 | STRINGLIB_CHAR *dst; |
| 421 | STRINGLIB_CHAR *src = STRINGLIB_STR(value); |
| 422 | Py_ssize_t len = STRINGLIB_LEN(value); |
| 423 | PyObject *result = NULL; |
| 424 | |
| 425 | /* sign is not allowed on strings */ |
| 426 | if (format->sign != '\0') { |
| 427 | PyErr_SetString(PyExc_ValueError, |
| 428 | "Sign not allowed in string format specifier"); |
| 429 | goto done; |
| 430 | } |
| 431 | |
Eric Smith | b1ebcc6 | 2008-07-15 13:02:41 +0000 | [diff] [blame] | 432 | /* alternate is not allowed on strings */ |
| 433 | if (format->alternate) { |
| 434 | PyErr_SetString(PyExc_ValueError, |
| 435 | "Alternate form (#) not allowed in string format " |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 436 | "specifier"); |
Eric Smith | b1ebcc6 | 2008-07-15 13:02:41 +0000 | [diff] [blame] | 437 | goto done; |
| 438 | } |
| 439 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 440 | /* '=' alignment not allowed on strings */ |
| 441 | if (format->align == '=') { |
| 442 | PyErr_SetString(PyExc_ValueError, |
| 443 | "'=' alignment not allowed " |
| 444 | "in string format specifier"); |
| 445 | goto done; |
| 446 | } |
| 447 | |
| 448 | /* if precision is specified, output no more that format.precision |
| 449 | characters */ |
| 450 | if (format->precision >= 0 && len >= format->precision) { |
| 451 | len = format->precision; |
| 452 | } |
| 453 | |
| 454 | if (format->width >= 0) { |
| 455 | width = format->width; |
| 456 | |
| 457 | /* but use at least len characters */ |
| 458 | if (len > width) { |
| 459 | width = len; |
| 460 | } |
Eric Smith | 0cb431c | 2007-08-28 01:07:27 +0000 | [diff] [blame] | 461 | } |
| 462 | else { |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 463 | /* not specified, use all of the chars and no more */ |
| 464 | width = len; |
| 465 | } |
| 466 | |
| 467 | /* allocate the resulting string */ |
| 468 | result = STRINGLIB_NEW(NULL, width); |
| 469 | if (result == NULL) |
| 470 | goto done; |
| 471 | |
| 472 | /* now write into that space */ |
| 473 | dst = STRINGLIB_STR(result); |
| 474 | |
| 475 | /* figure out how much leading space we need, based on the |
| 476 | aligning */ |
| 477 | if (format->align == '>') |
| 478 | lpad = width - len; |
| 479 | else if (format->align == '^') |
| 480 | lpad = (width - len) / 2; |
| 481 | else |
| 482 | lpad = 0; |
| 483 | |
| 484 | /* if right aligning, increment the destination allow space on the |
| 485 | left */ |
| 486 | memcpy(dst + lpad, src, len * sizeof(STRINGLIB_CHAR)); |
| 487 | |
| 488 | /* do any padding */ |
| 489 | if (width > len) { |
| 490 | STRINGLIB_CHAR fill_char = format->fill_char; |
| 491 | if (fill_char == '\0') { |
| 492 | /* use the default, if not specified */ |
| 493 | fill_char = ' '; |
| 494 | } |
| 495 | |
| 496 | /* pad on left */ |
| 497 | if (lpad) |
| 498 | STRINGLIB_FILL(dst, fill_char, lpad); |
| 499 | |
| 500 | /* pad on right */ |
| 501 | if (width - len - lpad) |
| 502 | STRINGLIB_FILL(dst + len + lpad, fill_char, width - len - lpad); |
| 503 | } |
| 504 | |
| 505 | done: |
| 506 | return result; |
| 507 | } |
| 508 | |
| 509 | |
| 510 | /************************************************************************/ |
| 511 | /*********** long formatting ********************************************/ |
| 512 | /************************************************************************/ |
| 513 | |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 514 | #if defined FORMAT_LONG || defined FORMAT_INT |
| 515 | typedef PyObject* |
| 516 | (*IntOrLongToString)(PyObject *value, int base); |
| 517 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 518 | static PyObject * |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 519 | format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format, |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 520 | IntOrLongToString tostring) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 521 | { |
| 522 | PyObject *result = NULL; |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 523 | PyObject *tmp = NULL; |
| 524 | STRINGLIB_CHAR *pnumeric_chars; |
| 525 | STRINGLIB_CHAR numeric_char; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 526 | STRINGLIB_CHAR sign = '\0'; |
| 527 | STRINGLIB_CHAR *p; |
| 528 | Py_ssize_t n_digits; /* count of digits need from the computed |
| 529 | string */ |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 530 | Py_ssize_t n_leading_chars; |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 531 | Py_ssize_t n_grouping_chars = 0; /* Count of additional chars to |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 532 | allocate, used for 'n' |
| 533 | formatting. */ |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 534 | Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */ |
| 535 | STRINGLIB_CHAR *prefix = NULL; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 536 | NumberFieldWidths spec; |
| 537 | long x; |
| 538 | |
| 539 | /* no precision allowed on integers */ |
| 540 | if (format->precision != -1) { |
| 541 | PyErr_SetString(PyExc_ValueError, |
| 542 | "Precision not allowed in integer format specifier"); |
| 543 | goto done; |
| 544 | } |
| 545 | |
| 546 | |
| 547 | /* special case for character formatting */ |
| 548 | if (format->type == 'c') { |
| 549 | /* error to specify a sign */ |
| 550 | if (format->sign != '\0') { |
| 551 | PyErr_SetString(PyExc_ValueError, |
| 552 | "Sign not allowed with integer" |
| 553 | " format specifier 'c'"); |
| 554 | goto done; |
| 555 | } |
| 556 | |
| 557 | /* taken from unicodeobject.c formatchar() */ |
| 558 | /* Integer input truncated to a character */ |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 559 | /* XXX: won't work for int */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 560 | x = PyLong_AsLong(value); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 561 | if (x == -1 && PyErr_Occurred()) |
| 562 | goto done; |
| 563 | #ifdef Py_UNICODE_WIDE |
| 564 | if (x < 0 || x > 0x10ffff) { |
| 565 | PyErr_SetString(PyExc_OverflowError, |
| 566 | "%c arg not in range(0x110000) " |
| 567 | "(wide Python build)"); |
| 568 | goto done; |
| 569 | } |
| 570 | #else |
| 571 | if (x < 0 || x > 0xffff) { |
| 572 | PyErr_SetString(PyExc_OverflowError, |
| 573 | "%c arg not in range(0x10000) " |
| 574 | "(narrow Python build)"); |
| 575 | goto done; |
| 576 | } |
| 577 | #endif |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 578 | numeric_char = (STRINGLIB_CHAR)x; |
| 579 | pnumeric_chars = &numeric_char; |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 580 | n_digits = 1; |
Eric Smith | 0cb431c | 2007-08-28 01:07:27 +0000 | [diff] [blame] | 581 | } |
| 582 | else { |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 583 | int base; |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 584 | int leading_chars_to_skip = 0; /* Number of characters added by |
| 585 | PyNumber_ToBase that we want to |
| 586 | skip over. */ |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 587 | |
| 588 | /* Compute the base and how many characters will be added by |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 589 | PyNumber_ToBase */ |
| 590 | switch (format->type) { |
| 591 | case 'b': |
| 592 | base = 2; |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 593 | leading_chars_to_skip = 2; /* 0b */ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 594 | break; |
| 595 | case 'o': |
| 596 | base = 8; |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 597 | leading_chars_to_skip = 2; /* 0o */ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 598 | break; |
| 599 | case 'x': |
| 600 | case 'X': |
| 601 | base = 16; |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 602 | leading_chars_to_skip = 2; /* 0x */ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 603 | break; |
| 604 | default: /* shouldn't be needed, but stops a compiler warning */ |
| 605 | case 'd': |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 606 | case 'n': |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 607 | base = 10; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 608 | break; |
| 609 | } |
| 610 | |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 611 | /* The number of prefix chars is the same as the leading |
| 612 | chars to skip */ |
| 613 | if (format->alternate) |
| 614 | n_prefix = leading_chars_to_skip; |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 615 | |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 616 | /* Do the hard part, converting to a string in a given base */ |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 617 | tmp = tostring(value, base); |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 618 | if (tmp == NULL) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 619 | goto done; |
| 620 | |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 621 | pnumeric_chars = STRINGLIB_STR(tmp); |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 622 | n_digits = STRINGLIB_LEN(tmp); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 623 | |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 624 | prefix = pnumeric_chars; |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 625 | |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 626 | /* Remember not to modify what pnumeric_chars points to. it |
| 627 | might be interned. Only modify it after we copy it into a |
| 628 | newly allocated output buffer. */ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 629 | |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 630 | /* Is a sign character present in the output? If so, remember it |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 631 | and skip it */ |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 632 | sign = pnumeric_chars[0]; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 633 | if (sign == '-') { |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 634 | ++prefix; |
| 635 | ++leading_chars_to_skip; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 638 | /* Skip over the leading chars (0x, 0b, etc.) */ |
| 639 | n_digits -= leading_chars_to_skip; |
| 640 | pnumeric_chars += leading_chars_to_skip; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 643 | if (format->type == 'n') |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 644 | /* Compute how many additional chars we need to allocate |
| 645 | to hold the thousands grouping. */ |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame^] | 646 | STRINGLIB_GROUPING_LOCALE(NULL, n_digits, n_digits, |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 647 | 0, &n_grouping_chars, 0); |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame^] | 648 | if (format->thousands_separators) |
| 649 | /* Compute how many additional chars we need to allocate |
| 650 | to hold the thousands grouping. */ |
| 651 | STRINGLIB_GROUPING(NULL, n_digits, n_digits, |
| 652 | 0, &n_grouping_chars, 0, "\3", ","); |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 653 | |
Eric Smith | b151a45 | 2008-06-24 11:21:04 +0000 | [diff] [blame] | 654 | /* Calculate the widths of the various leading and trailing parts */ |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 655 | calc_number_widths(&spec, sign, n_prefix, n_digits + n_grouping_chars, |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 656 | format); |
Eric Smith | b151a45 | 2008-06-24 11:21:04 +0000 | [diff] [blame] | 657 | |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 658 | /* Allocate a new string to hold the result */ |
Eric Smith | b151a45 | 2008-06-24 11:21:04 +0000 | [diff] [blame] | 659 | result = STRINGLIB_NEW(NULL, spec.n_total); |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 660 | if (!result) |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 661 | goto done; |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 662 | p = STRINGLIB_STR(result); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 663 | |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 664 | /* XXX There is too much magic here regarding the internals of |
| 665 | spec and the location of the prefix and digits. It would be |
| 666 | better if calc_number_widths returned a number of logical |
| 667 | offsets into the buffer, and those were used. Maybe in a |
| 668 | future code cleanup. */ |
| 669 | |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 670 | /* Fill in the digit parts */ |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 671 | n_leading_chars = spec.n_lpadding + spec.n_lsign + |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 672 | spec.n_prefix + spec.n_spadding; |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 673 | memmove(p + n_leading_chars, |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 674 | pnumeric_chars, |
| 675 | n_digits * sizeof(STRINGLIB_CHAR)); |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 676 | |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 677 | /* If type is 'X', convert the filled in digits to uppercase */ |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 678 | if (format->type == 'X') { |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 679 | Py_ssize_t t; |
| 680 | for (t = 0; t < n_digits; ++t) |
| 681 | p[t + n_leading_chars] = STRINGLIB_TOUPPER(p[t + n_leading_chars]); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 684 | /* Insert the grouping, if any, after the uppercasing of the digits, so |
| 685 | we can ensure that grouping chars won't be affected. */ |
Eric Smith | b151a45 | 2008-06-24 11:21:04 +0000 | [diff] [blame] | 686 | if (n_grouping_chars) { |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 687 | /* We know this can't fail, since we've already |
| 688 | reserved enough space. */ |
| 689 | STRINGLIB_CHAR *pstart = p + n_leading_chars; |
Neal Norwitz | 2f99b24 | 2008-08-24 05:48:10 +0000 | [diff] [blame] | 690 | #ifndef NDEBUG |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame^] | 691 | int r; |
Neal Norwitz | 2f99b24 | 2008-08-24 05:48:10 +0000 | [diff] [blame] | 692 | #endif |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame^] | 693 | if (format->type == 'n') |
| 694 | #ifndef NDEBUG |
| 695 | r = |
| 696 | #endif |
| 697 | STRINGLIB_GROUPING_LOCALE(pstart, n_digits, n_digits, |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 698 | spec.n_total+n_grouping_chars-n_leading_chars, |
| 699 | NULL, 0); |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame^] | 700 | else |
| 701 | #ifndef NDEBUG |
| 702 | r = |
| 703 | STRINGLIB_GROUPING(pstart, n_digits, n_digits, |
| 704 | spec.n_total+n_grouping_chars-n_leading_chars, |
| 705 | NULL, 0, "\3", ","); |
| 706 | #endif |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 707 | assert(r); |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Eric Smith | b151a45 | 2008-06-24 11:21:04 +0000 | [diff] [blame] | 710 | /* Fill in the non-digit parts (padding, sign, etc.) */ |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 711 | fill_non_digits(p, &spec, prefix, n_digits + n_grouping_chars, |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 712 | format->fill_char == '\0' ? ' ' : format->fill_char); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 713 | |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 714 | /* If type is 'X', uppercase the prefix. This has to be done after the |
| 715 | prefix is filled in by fill_non_digits */ |
| 716 | if (format->type == 'X') { |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 717 | Py_ssize_t t; |
| 718 | for (t = 0; t < n_prefix; ++t) |
| 719 | p[t + spec.n_lpadding + spec.n_lsign] = |
| 720 | STRINGLIB_TOUPPER(p[t + spec.n_lpadding + spec.n_lsign]); |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 724 | done: |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 725 | Py_XDECREF(tmp); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 726 | return result; |
| 727 | } |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 728 | #endif /* defined FORMAT_LONG || defined FORMAT_INT */ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 729 | |
| 730 | /************************************************************************/ |
| 731 | /*********** float formatting *******************************************/ |
| 732 | /************************************************************************/ |
| 733 | |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 734 | #ifdef FORMAT_FLOAT |
| 735 | #if STRINGLIB_IS_UNICODE |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 736 | /* taken from unicodeobject.c */ |
| 737 | static Py_ssize_t |
| 738 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
| 739 | { |
| 740 | register Py_ssize_t i; |
| 741 | Py_ssize_t len = strlen(charbuffer); |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 742 | for (i = len - 1; i >= 0; --i) |
Eric Smith | 185e30c | 2007-08-30 22:23:08 +0000 | [diff] [blame] | 743 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 744 | |
| 745 | return len; |
| 746 | } |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 747 | #endif |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 748 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 749 | /* see FORMATBUFLEN in unicodeobject.c */ |
| 750 | #define FLOAT_FORMATBUFLEN 120 |
| 751 | |
| 752 | /* much of this is taken from unicodeobject.c */ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 753 | static PyObject * |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 754 | format_float_internal(PyObject *value, |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 755 | const InternalFormatSpec *format) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 756 | { |
| 757 | /* fmt = '%.' + `prec` + `type` + '%%' |
| 758 | worst case length = 2 + 10 (len of INT_MAX) + 1 + 2 = 15 (use 20)*/ |
| 759 | char fmt[20]; |
| 760 | |
| 761 | /* taken from unicodeobject.c */ |
| 762 | /* Worst case length calc to ensure no buffer overrun: |
| 763 | |
| 764 | 'g' formats: |
Eric Smith | 185e30c | 2007-08-30 22:23:08 +0000 | [diff] [blame] | 765 | fmt = %#.<prec>g |
| 766 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 767 | for any double rep.) |
| 768 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 769 | |
| 770 | 'f' formats: |
Eric Smith | 185e30c | 2007-08-30 22:23:08 +0000 | [diff] [blame] | 771 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 772 | len = 1 + 50 + 1 + prec = 52 + prec |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 773 | |
| 774 | If prec=0 the effective precision is 1 (the leading digit is |
| 775 | always given), therefore increase the length by one. |
| 776 | |
| 777 | */ |
| 778 | char charbuf[FLOAT_FORMATBUFLEN]; |
| 779 | Py_ssize_t n_digits; |
| 780 | double x; |
| 781 | Py_ssize_t precision = format->precision; |
| 782 | PyObject *result = NULL; |
| 783 | STRINGLIB_CHAR sign; |
| 784 | char* trailing = ""; |
| 785 | STRINGLIB_CHAR *p; |
| 786 | NumberFieldWidths spec; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 787 | STRINGLIB_CHAR type = format->type; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 788 | |
| 789 | #if STRINGLIB_IS_UNICODE |
| 790 | Py_UNICODE unicodebuf[FLOAT_FORMATBUFLEN]; |
| 791 | #endif |
| 792 | |
Eric Smith | b1ebcc6 | 2008-07-15 13:02:41 +0000 | [diff] [blame] | 793 | /* alternate is not allowed on floats. */ |
| 794 | if (format->alternate) { |
| 795 | PyErr_SetString(PyExc_ValueError, |
| 796 | "Alternate form (#) not allowed in float format " |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 797 | "specifier"); |
Eric Smith | b1ebcc6 | 2008-07-15 13:02:41 +0000 | [diff] [blame] | 798 | goto done; |
| 799 | } |
| 800 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 801 | /* first, do the conversion as 8-bit chars, using the platform's |
| 802 | snprintf. then, if needed, convert to unicode. */ |
| 803 | |
Eric Smith | 22b85b3 | 2008-07-17 19:18:29 +0000 | [diff] [blame] | 804 | /* 'F' is the same as 'f', per the PEP */ |
| 805 | if (type == 'F') |
| 806 | type = 'f'; |
| 807 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 808 | x = PyFloat_AsDouble(value); |
| 809 | |
| 810 | if (x == -1.0 && PyErr_Occurred()) |
Eric Smith | 185e30c | 2007-08-30 22:23:08 +0000 | [diff] [blame] | 811 | goto done; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 812 | |
| 813 | if (type == '%') { |
| 814 | type = 'f'; |
| 815 | x *= 100; |
| 816 | trailing = "%"; |
| 817 | } |
| 818 | |
| 819 | if (precision < 0) |
Eric Smith | 185e30c | 2007-08-30 22:23:08 +0000 | [diff] [blame] | 820 | precision = 6; |
Mark Dickinson | c8a608c | 2009-03-29 15:19:47 +0000 | [diff] [blame] | 821 | if (type == 'f' && fabs(x) >= 1e50) |
Eric Smith | 22b85b3 | 2008-07-17 19:18:29 +0000 | [diff] [blame] | 822 | type = 'g'; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 823 | |
| 824 | /* cast "type", because if we're in unicode we need to pass a |
| 825 | 8-bit char. this is safe, because we've restricted what "type" |
| 826 | can be */ |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 827 | PyOS_snprintf(fmt, sizeof(fmt), "%%.%" PY_FORMAT_SIZE_T "d%c", precision, |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 828 | (char)type); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 829 | |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 830 | /* do the actual formatting */ |
| 831 | PyOS_ascii_formatd(charbuf, sizeof(charbuf), fmt, x); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 832 | |
| 833 | /* adding trailing to fmt with PyOS_snprintf doesn't work, not |
| 834 | sure why. we'll just concatentate it here, no harm done. we |
| 835 | know we can't have a buffer overflow from the fmt size |
| 836 | analysis */ |
| 837 | strcat(charbuf, trailing); |
| 838 | |
| 839 | /* rather than duplicate the code for snprintf for both unicode |
| 840 | and 8 bit strings, we just use the 8 bit version and then |
| 841 | convert to unicode in a separate code path. that's probably |
| 842 | the lesser of 2 evils. */ |
| 843 | #if STRINGLIB_IS_UNICODE |
| 844 | n_digits = strtounicode(unicodebuf, charbuf); |
| 845 | p = unicodebuf; |
| 846 | #else |
| 847 | /* compute the length. I believe this is done because the return |
| 848 | value from snprintf above is unreliable */ |
| 849 | n_digits = strlen(charbuf); |
| 850 | p = charbuf; |
| 851 | #endif |
| 852 | |
| 853 | /* is a sign character present in the output? if so, remember it |
| 854 | and skip it */ |
| 855 | sign = p[0]; |
| 856 | if (sign == '-') { |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 857 | ++p; |
| 858 | --n_digits; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Eric Smith | b1ebcc6 | 2008-07-15 13:02:41 +0000 | [diff] [blame] | 861 | calc_number_widths(&spec, sign, 0, n_digits, format); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 862 | |
| 863 | /* allocate a string with enough space */ |
| 864 | result = STRINGLIB_NEW(NULL, spec.n_total); |
| 865 | if (result == NULL) |
| 866 | goto done; |
| 867 | |
Eric Smith | b151a45 | 2008-06-24 11:21:04 +0000 | [diff] [blame] | 868 | /* Fill in the non-digit parts (padding, sign, etc.) */ |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 869 | fill_non_digits(STRINGLIB_STR(result), &spec, NULL, n_digits, |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 870 | format->fill_char == '\0' ? ' ' : format->fill_char); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 871 | |
| 872 | /* fill in the digit parts */ |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 873 | memmove(STRINGLIB_STR(result) + |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 874 | (spec.n_lpadding + spec.n_lsign + spec.n_spadding), |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 875 | p, |
| 876 | n_digits * sizeof(STRINGLIB_CHAR)); |
| 877 | |
| 878 | done: |
| 879 | return result; |
| 880 | } |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 881 | #endif /* FORMAT_FLOAT */ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 882 | |
| 883 | /************************************************************************/ |
| 884 | /*********** built in formatters ****************************************/ |
| 885 | /************************************************************************/ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 886 | PyObject * |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 887 | FORMAT_STRING(PyObject *obj, |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 888 | STRINGLIB_CHAR *format_spec, |
| 889 | Py_ssize_t format_spec_len) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 890 | { |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 891 | InternalFormatSpec format; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 892 | PyObject *result = NULL; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 893 | |
| 894 | /* check for the special case of zero length format spec, make |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 895 | it equivalent to str(obj) */ |
| 896 | if (format_spec_len == 0) { |
| 897 | result = STRINGLIB_TOSTR(obj); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 898 | goto done; |
| 899 | } |
| 900 | |
| 901 | /* parse the format_spec */ |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 902 | if (!parse_internal_render_format_spec(format_spec, format_spec_len, |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 903 | &format, 's')) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 904 | goto done; |
| 905 | |
| 906 | /* type conversion? */ |
| 907 | switch (format.type) { |
| 908 | case 's': |
| 909 | /* no type conversion needed, already a string. do the formatting */ |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 910 | result = format_string_internal(obj, &format); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 911 | break; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 912 | default: |
| 913 | /* unknown */ |
Eric Smith | 5e5c0db | 2009-02-20 14:25:03 +0000 | [diff] [blame] | 914 | unknown_presentation_type(format.type, obj->ob_type->tp_name); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 915 | goto done; |
| 916 | } |
| 917 | |
| 918 | done: |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 919 | return result; |
| 920 | } |
| 921 | |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 922 | #if defined FORMAT_LONG || defined FORMAT_INT |
| 923 | static PyObject* |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 924 | format_int_or_long(PyObject* obj, |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 925 | STRINGLIB_CHAR *format_spec, |
| 926 | Py_ssize_t format_spec_len, |
| 927 | IntOrLongToString tostring) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 928 | { |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 929 | PyObject *result = NULL; |
| 930 | PyObject *tmp = NULL; |
| 931 | InternalFormatSpec format; |
| 932 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 933 | /* check for the special case of zero length format spec, make |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 934 | it equivalent to str(obj) */ |
| 935 | if (format_spec_len == 0) { |
| 936 | result = STRINGLIB_TOSTR(obj); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 937 | goto done; |
| 938 | } |
| 939 | |
| 940 | /* parse the format_spec */ |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 941 | if (!parse_internal_render_format_spec(format_spec, |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 942 | format_spec_len, |
| 943 | &format, 'd')) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 944 | goto done; |
| 945 | |
| 946 | /* type conversion? */ |
| 947 | switch (format.type) { |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 948 | case 'b': |
| 949 | case 'c': |
| 950 | case 'd': |
| 951 | case 'o': |
| 952 | case 'x': |
| 953 | case 'X': |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 954 | case 'n': |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 955 | /* no type conversion needed, already an int (or long). do |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 956 | the formatting */ |
| 957 | result = format_int_or_long_internal(obj, &format, tostring); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 958 | break; |
| 959 | |
Eric Smith | fa767ef | 2008-01-28 10:59:27 +0000 | [diff] [blame] | 960 | case 'e': |
| 961 | case 'E': |
| 962 | case 'f': |
| 963 | case 'F': |
| 964 | case 'g': |
| 965 | case 'G': |
Eric Smith | fa767ef | 2008-01-28 10:59:27 +0000 | [diff] [blame] | 966 | case '%': |
| 967 | /* convert to float */ |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 968 | tmp = PyNumber_Float(obj); |
Eric Smith | fa767ef | 2008-01-28 10:59:27 +0000 | [diff] [blame] | 969 | if (tmp == NULL) |
| 970 | goto done; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 971 | result = format_float_internal(obj, &format); |
Eric Smith | fa767ef | 2008-01-28 10:59:27 +0000 | [diff] [blame] | 972 | break; |
| 973 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 974 | default: |
| 975 | /* unknown */ |
Eric Smith | 5e5c0db | 2009-02-20 14:25:03 +0000 | [diff] [blame] | 976 | unknown_presentation_type(format.type, obj->ob_type->tp_name); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 977 | goto done; |
| 978 | } |
| 979 | |
| 980 | done: |
| 981 | Py_XDECREF(tmp); |
| 982 | return result; |
| 983 | } |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 984 | #endif /* FORMAT_LONG || defined FORMAT_INT */ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 985 | |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 986 | #ifdef FORMAT_LONG |
| 987 | /* Need to define long_format as a function that will convert a long |
| 988 | to a string. In 3.0, _PyLong_Format has the correct signature. In |
| 989 | 2.x, we need to fudge a few parameters */ |
| 990 | #if PY_VERSION_HEX >= 0x03000000 |
| 991 | #define long_format _PyLong_Format |
| 992 | #else |
| 993 | static PyObject* |
| 994 | long_format(PyObject* value, int base) |
| 995 | { |
| 996 | /* Convert to base, don't add trailing 'L', and use the new octal |
| 997 | format. We already know this is a long object */ |
| 998 | assert(PyLong_Check(value)); |
| 999 | /* convert to base, don't add 'L', and use the new octal format */ |
| 1000 | return _PyLong_Format(value, base, 0, 1); |
| 1001 | } |
| 1002 | #endif |
| 1003 | |
| 1004 | PyObject * |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 1005 | FORMAT_LONG(PyObject *obj, |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 1006 | STRINGLIB_CHAR *format_spec, |
| 1007 | Py_ssize_t format_spec_len) |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 1008 | { |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 1009 | return format_int_or_long(obj, format_spec, format_spec_len, |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 1010 | long_format); |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 1011 | } |
| 1012 | #endif /* FORMAT_LONG */ |
| 1013 | |
| 1014 | #ifdef FORMAT_INT |
| 1015 | /* this is only used for 2.x, not 3.0 */ |
| 1016 | static PyObject* |
| 1017 | int_format(PyObject* value, int base) |
| 1018 | { |
| 1019 | /* Convert to base, and use the new octal format. We already |
| 1020 | know this is an int object */ |
| 1021 | assert(PyInt_Check(value)); |
| 1022 | return _PyInt_Format((PyIntObject*)value, base, 1); |
| 1023 | } |
| 1024 | |
| 1025 | PyObject * |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 1026 | FORMAT_INT(PyObject *obj, |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 1027 | STRINGLIB_CHAR *format_spec, |
| 1028 | Py_ssize_t format_spec_len) |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 1029 | { |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 1030 | return format_int_or_long(obj, format_spec, format_spec_len, |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 1031 | int_format); |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 1032 | } |
| 1033 | #endif /* FORMAT_INT */ |
| 1034 | |
| 1035 | #ifdef FORMAT_FLOAT |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 1036 | PyObject * |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 1037 | FORMAT_FLOAT(PyObject *obj, |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 1038 | STRINGLIB_CHAR *format_spec, |
| 1039 | Py_ssize_t format_spec_len) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 1040 | { |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 1041 | PyObject *result = NULL; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 1042 | InternalFormatSpec format; |
| 1043 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 1044 | /* check for the special case of zero length format spec, make |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 1045 | it equivalent to str(obj) */ |
| 1046 | if (format_spec_len == 0) { |
| 1047 | result = STRINGLIB_TOSTR(obj); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 1048 | goto done; |
| 1049 | } |
| 1050 | |
| 1051 | /* parse the format_spec */ |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 1052 | if (!parse_internal_render_format_spec(format_spec, |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 1053 | format_spec_len, |
| 1054 | &format, '\0')) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 1055 | goto done; |
| 1056 | |
| 1057 | /* type conversion? */ |
| 1058 | switch (format.type) { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 1059 | case '\0': |
Eric Smith | f8c8b6d | 2009-04-03 11:19:31 +0000 | [diff] [blame] | 1060 | /* 'Z' means like 'g', but with at least one decimal. See |
| 1061 | PyOS_ascii_formatd */ |
| 1062 | format.type = 'Z'; |
| 1063 | /* Deliberate fall through to the next case statement */ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 1064 | case 'e': |
| 1065 | case 'E': |
| 1066 | case 'f': |
| 1067 | case 'F': |
| 1068 | case 'g': |
| 1069 | case 'G': |
| 1070 | case 'n': |
| 1071 | case '%': |
| 1072 | /* no conversion, already a float. do the formatting */ |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 1073 | result = format_float_internal(obj, &format); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 1074 | break; |
| 1075 | |
| 1076 | default: |
| 1077 | /* unknown */ |
Eric Smith | 5e5c0db | 2009-02-20 14:25:03 +0000 | [diff] [blame] | 1078 | unknown_presentation_type(format.type, obj->ob_type->tp_name); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 1079 | goto done; |
| 1080 | } |
| 1081 | |
| 1082 | done: |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 1083 | return result; |
| 1084 | } |
Eric Smith | 8fd3eba | 2008-02-17 19:48:00 +0000 | [diff] [blame] | 1085 | #endif /* FORMAT_FLOAT */ |