annotate Objects/stringlib/formatter.h @ 59028:73cd83706bc4 3.1

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