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