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