annotate Objects/stringlib/formatter.h @ 48416:fb02fedfe647

Merged revisions 64677 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r64677 | brett.cannon | 2008-07-02 14:52:42 -0700 (Wed, 02 Jul 2008) | 2 lines Revert r64673 and instead just change the file encoding. ........
author Brett Cannon <bcannon@gmail.com>
date Wed, 02 Jul 2008 22:06:12 +0000
parents 7e747de231b3
children d4e473291a17
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
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
4 /* Before including this, you must include either:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
5 stringlib/unicodedefs.h
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
6 stringlib/stringdefs.h
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
7
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
8 Also, you should define the names:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
9 FORMAT_STRING
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
10 FORMAT_LONG
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
11 FORMAT_FLOAT
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
12 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
13 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
14 */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
15
42980
9df911d654af Added conditional compilation for '()', which was an allowed sign code in a
Eric Smith <eric@trueblade.com>
parents: 42929
diff changeset
16 #define ALLOW_PARENS_FOR_SIGN 0
9df911d654af Added conditional compilation for '()', which was an allowed sign code in a
Eric Smith <eric@trueblade.com>
parents: 42929
diff changeset
17
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
18 /*
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
19 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
20 input string, updates *result with the corresponding positive
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
21 integer, and returns the number of digits consumed.
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
22
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
23 returns -1 on error.
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
24 */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
25 static int
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
26 get_integer(STRINGLIB_CHAR **ptr, STRINGLIB_CHAR *end,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
27 Py_ssize_t *result)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
28 {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
29 Py_ssize_t accumulator, digitval, oldaccumulator;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
30 int numdigits;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
31 accumulator = numdigits = 0;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
32 for (;;(*ptr)++, numdigits++) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
33 if (*ptr >= end)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
34 break;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
35 digitval = STRINGLIB_TODECIMAL(**ptr);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
36 if (digitval < 0)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
37 break;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
38 /*
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
39 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
40 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
41 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
42 expect too many digits.
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
43 */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
44 oldaccumulator = accumulator;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
45 accumulator *= 10;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
46 if ((accumulator+10)/10 != oldaccumulator+1) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
47 PyErr_Format(PyExc_ValueError,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
48 "Too many decimal digits in format string");
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
49 return -1;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
50 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
51 accumulator += digitval;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
52 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
53 *result = accumulator;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
54 return numdigits;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
55 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
56
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 /*********** standard format specifier parsing **************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
59 /************************************************************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
60
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
61 /* 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
62 Py_LOCAL_INLINE(int)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
63 is_alignment_token(STRINGLIB_CHAR c)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
64 {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
65 switch (c) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
66 case '<': case '>': case '=': case '^':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
67 return 1;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
68 default:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
69 return 0;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
70 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
71 }
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 /* 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
74 Py_LOCAL_INLINE(int)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
75 is_sign_element(STRINGLIB_CHAR c)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
76 {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
77 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
78 case ' ': case '+': case '-':
42981
0f82fc42fd68 Corrected missed #if in r57652.
Eric Smith <eric@trueblade.com>
parents: 42980
diff changeset
79 #if ALLOW_PARENS_FOR_SIGN
42980
9df911d654af Added conditional compilation for '()', which was an allowed sign code in a
Eric Smith <eric@trueblade.com>
parents: 42929
diff changeset
80 case '(':
42981
0f82fc42fd68 Corrected missed #if in r57652.
Eric Smith <eric@trueblade.com>
parents: 42980
diff changeset
81 #endif
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
82 return 1;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
83 default:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
84 return 0;
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
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 typedef struct {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
90 STRINGLIB_CHAR fill_char;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
91 STRINGLIB_CHAR align;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
92 STRINGLIB_CHAR sign;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
93 Py_ssize_t width;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
94 Py_ssize_t precision;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
95 STRINGLIB_CHAR type;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
96 } InternalFormatSpec;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
97
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
98 /*
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
99 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
100 fills in format with the parsed information.
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
101 returns 1 on success, 0 on failure.
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
102 if failure, sets the exception
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
103 */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
104 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
105 parse_internal_render_format_spec(STRINGLIB_CHAR *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
106 Py_ssize_t format_spec_len,
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
107 InternalFormatSpec *format,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
108 char default_type)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
109 {
47393
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
110 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
111 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
112
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
113 /* 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
114 the input string */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
115
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
116 Py_ssize_t specified_width;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
117
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
118 format->fill_char = '\0';
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
119 format->align = '\0';
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
120 format->sign = '\0';
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
121 format->width = -1;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
122 format->precision = -1;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
123 format->type = default_type;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
124
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
125 /* If the second char is an alignment token,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
126 then parse the fill char */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
127 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
128 format->align = ptr[1];
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
129 format->fill_char = ptr[0];
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
130 ptr += 2;
42929
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
131 }
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
132 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
133 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
134 ++ptr;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
135 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
136
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
137 /* Parse the various sign options */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
138 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
139 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
140 ++ptr;
42980
9df911d654af Added conditional compilation for '()', which was an allowed sign code in a
Eric Smith <eric@trueblade.com>
parents: 42929
diff changeset
141 #if ALLOW_PARENS_FOR_SIGN
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
142 if (end-ptr >= 1 && 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
143 ++ptr;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
144 }
42980
9df911d654af Added conditional compilation for '()', which was an allowed sign code in a
Eric Smith <eric@trueblade.com>
parents: 42929
diff changeset
145 #endif
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
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
148 /* The special case for 0-padding (backwards compat) */
43097
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
149 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
150 format->fill_char = '0';
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
151 if (format->align == '\0') {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
152 format->align = '=';
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
153 }
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
154 ++ptr;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
155 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
156
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
157 /* XXX add error checking */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
158 specified_width = get_integer(&ptr, end, &format->width);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
159
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
160 /* if specified_width is 0, we didn't consume any characters for
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
161 the width. in that case, reset the width to -1, because
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
162 get_integer() will have set it to zero */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
163 if (specified_width == 0) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
164 format->width = -1;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
165 }
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 /* Parse field precision */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
168 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
169 ++ptr;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
170
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
171 /* XXX add error checking */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
172 specified_width = get_integer(&ptr, end, &format->precision);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
173
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
174 /* not having a precision after a dot is an error */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
175 if (specified_width == 0) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
176 PyErr_Format(PyExc_ValueError,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
177 "Format specifier missing precision");
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
178 return 0;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
179 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
180
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
181 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
182
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
183 /* Finally, parse the type field */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
184
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
185 if (end-ptr > 1) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
186 /* invalid conversion spec */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
187 PyErr_Format(PyExc_ValueError, "Invalid conversion specification");
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
188 return 0;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
189 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
190
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
191 if (end-ptr == 1) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
192 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
193 ++ptr;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
194 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
195
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
196 return 1;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
197 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
198
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
199 #if defined FORMAT_FLOAT || defined FORMAT_LONG
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
200 /************************************************************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
201 /*********** common routines for numeric formatting *********************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
202 /************************************************************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
203
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
204 /* describes the layout for an integer, see the comment in
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
205 _calc_integer_widths() for details */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
206 typedef struct {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
207 Py_ssize_t n_lpadding;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
208 Py_ssize_t n_spadding;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
209 Py_ssize_t n_rpadding;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
210 char lsign;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
211 Py_ssize_t n_lsign;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
212 char rsign;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
213 Py_ssize_t n_rsign;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
214 Py_ssize_t n_total; /* just a convenience, it's derivable from the
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
215 other fields */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
216 } NumberFieldWidths;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
217
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
218 /* 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
219 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
220 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
221 and more efficient enough to justify a little obfuscation? */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
222 static void
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
223 calc_number_widths(NumberFieldWidths *r, STRINGLIB_CHAR actual_sign,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
224 Py_ssize_t n_digits, const InternalFormatSpec *format)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
225 {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
226 r->n_lpadding = 0;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
227 r->n_spadding = 0;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
228 r->n_rpadding = 0;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
229 r->lsign = '\0';
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
230 r->n_lsign = 0;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
231 r->rsign = '\0';
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
232 r->n_rsign = 0;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
233
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
234 /* the output will look like:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
235 | |
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
236 | <lpadding> <lsign> <spadding> <digits> <rsign> <rpadding> |
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
237 | |
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
238
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
239 lsign and rsign are computed from format->sign and the actual
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
240 sign of the number
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
241
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
242 digits is already known
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 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
245 actual digits
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
246
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
247 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
248 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
249 */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
250
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
251 /* compute the various parts we're going to write */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
252 if (format->sign == '+') {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
253 /* always put a + or - */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
254 r->n_lsign = 1;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
255 r->lsign = (actual_sign == '-' ? '-' : '+');
42929
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
256 }
42980
9df911d654af Added conditional compilation for '()', which was an allowed sign code in a
Eric Smith <eric@trueblade.com>
parents: 42929
diff changeset
257 #if ALLOW_PARENS_FOR_SIGN
42929
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
258 else if (format->sign == '(') {
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
259 if (actual_sign == '-') {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
260 r->n_lsign = 1;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
261 r->lsign = '(';
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
262 r->n_rsign = 1;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
263 r->rsign = ')';
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
264 }
42929
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
265 }
42980
9df911d654af Added conditional compilation for '()', which was an allowed sign code in a
Eric Smith <eric@trueblade.com>
parents: 42929
diff changeset
266 #endif
42929
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
267 else if (format->sign == ' ') {
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
268 r->n_lsign = 1;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
269 r->lsign = (actual_sign == '-' ? '-' : ' ');
42929
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
270 }
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
271 else {
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
272 /* non specified, or the default (-) */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
273 if (actual_sign == '-') {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
274 r->n_lsign = 1;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
275 r->lsign = '-';
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
276 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
277 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
278
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
279 /* now the number of padding characters */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
280 if (format->width == -1) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
281 /* no padding at all, nothing to do */
42929
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
282 }
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
283 else {
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
284 /* see if any padding is needed */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
285 if (r->n_lsign + n_digits + r->n_rsign >= format->width) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
286 /* no padding needed, we're already bigger than the
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
287 requested width */
42929
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
288 }
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
289 else {
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
290 /* determine which of left, space, or right padding is
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
291 needed */
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
292 Py_ssize_t padding = format->width -
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
293 (r->n_lsign + n_digits + r->n_rsign);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
294 if (format->align == '<')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
295 r->n_rpadding = padding;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
296 else if (format->align == '>')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
297 r->n_lpadding = padding;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
298 else if (format->align == '^') {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
299 r->n_lpadding = padding / 2;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
300 r->n_rpadding = padding - r->n_lpadding;
42929
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
301 }
43097
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
302 else if (format->align == '=')
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
303 r->n_spadding = padding;
42929
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
304 else
43097
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
305 r->n_lpadding = padding;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
306 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
307 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
308 r->n_total = r->n_lpadding + r->n_lsign + r->n_spadding +
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
309 n_digits + r->n_rsign + r->n_rpadding;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
310 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
311
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
312 /* fill in the non-digit parts of a numbers's string representation,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
313 as determined in _calc_integer_widths(). returns the pointer to
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
314 where the digits go. */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
315 static STRINGLIB_CHAR *
48360
7e747de231b3 Merged revisions 64499 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48359
diff changeset
316 fill_non_digits(STRINGLIB_CHAR *p_buf, const NumberFieldWidths *spec,
7e747de231b3 Merged revisions 64499 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48359
diff changeset
317 Py_ssize_t n_digits, STRINGLIB_CHAR fill_char)
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
318 {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
319 STRINGLIB_CHAR* p_digits;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
320
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
321 if (spec->n_lpadding) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
322 STRINGLIB_FILL(p_buf, fill_char, spec->n_lpadding);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
323 p_buf += spec->n_lpadding;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
324 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
325 if (spec->n_lsign == 1) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
326 *p_buf++ = spec->lsign;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
327 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
328 if (spec->n_spadding) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
329 STRINGLIB_FILL(p_buf, fill_char, spec->n_spadding);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
330 p_buf += spec->n_spadding;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
331 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
332 p_digits = p_buf;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
333 p_buf += n_digits;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
334 if (spec->n_rsign == 1) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
335 *p_buf++ = spec->rsign;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
336 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
337 if (spec->n_rpadding) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
338 STRINGLIB_FILL(p_buf, fill_char, spec->n_rpadding);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
339 p_buf += spec->n_rpadding;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
340 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
341 return p_digits;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
342 }
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
343 #endif /* FORMAT_FLOAT || FORMAT_LONG */
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
344
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
345 /************************************************************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
346 /*********** string formatting ******************************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
347 /************************************************************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
348
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
349 static PyObject *
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
350 format_string_internal(PyObject *value, const InternalFormatSpec *format)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
351 {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
352 Py_ssize_t width; /* total field width */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
353 Py_ssize_t lpad;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
354 STRINGLIB_CHAR *dst;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
355 STRINGLIB_CHAR *src = STRINGLIB_STR(value);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
356 Py_ssize_t len = STRINGLIB_LEN(value);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
357 PyObject *result = NULL;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
358
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
359 /* sign is not allowed on strings */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
360 if (format->sign != '\0') {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
361 PyErr_SetString(PyExc_ValueError,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
362 "Sign not allowed in string format specifier");
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
363 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
364 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
365
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
366 /* '=' alignment not allowed on strings */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
367 if (format->align == '=') {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
368 PyErr_SetString(PyExc_ValueError,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
369 "'=' alignment not allowed "
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
370 "in string format specifier");
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
371 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
372 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
373
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
374 /* 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
375 characters */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
376 if (format->precision >= 0 && len >= format->precision) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
377 len = format->precision;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
378 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
379
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
380 if (format->width >= 0) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
381 width = format->width;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
382
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
383 /* but use at least len characters */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
384 if (len > width) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
385 width = len;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
386 }
42929
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
387 }
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
388 else {
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
389 /* not specified, use all of the chars and no more */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
390 width = len;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
391 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
392
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
393 /* allocate the resulting string */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
394 result = STRINGLIB_NEW(NULL, width);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
395 if (result == NULL)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
396 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
397
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
398 /* now write into that space */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
399 dst = STRINGLIB_STR(result);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
400
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
401 /* figure out how much leading space we need, based on the
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
402 aligning */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
403 if (format->align == '>')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
404 lpad = width - len;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
405 else if (format->align == '^')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
406 lpad = (width - len) / 2;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
407 else
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
408 lpad = 0;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
409
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
410 /* if right aligning, increment the destination allow space on the
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
411 left */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
412 memcpy(dst + lpad, src, len * sizeof(STRINGLIB_CHAR));
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
413
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
414 /* do any padding */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
415 if (width > len) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
416 STRINGLIB_CHAR fill_char = format->fill_char;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
417 if (fill_char == '\0') {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
418 /* use the default, if not specified */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
419 fill_char = ' ';
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
420 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
421
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
422 /* pad on left */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
423 if (lpad)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
424 STRINGLIB_FILL(dst, fill_char, lpad);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
425
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
426 /* pad on right */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
427 if (width - len - lpad)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
428 STRINGLIB_FILL(dst + len + lpad, fill_char, width - len - lpad);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
429 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
430
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
431 done:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
432 return result;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
433 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
434
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
435
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 /*********** long formatting ********************************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
438 /************************************************************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
439
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
440 #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
441 typedef PyObject*
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
442 (*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
443
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
444 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
445 format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
446 IntOrLongToString tostring)
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
447 {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
448 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
449 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
450 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
451 STRINGLIB_CHAR numeric_char;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
452 STRINGLIB_CHAR sign = '\0';
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
453 STRINGLIB_CHAR *p;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
454 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
455 string */
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
456 Py_ssize_t n_leading_chars;
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
457 Py_ssize_t n_grouping_chars = 0; /* Count of additional chars to
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
458 allocate, used for 'n'
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
459 formatting. */
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
460 NumberFieldWidths spec;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
461 long x;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
462
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
463 /* no precision allowed on integers */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
464 if (format->precision != -1) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
465 PyErr_SetString(PyExc_ValueError,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
466 "Precision not allowed in integer format specifier");
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
467 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
468 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
469
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
470
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
471 /* special case for character formatting */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
472 if (format->type == 'c') {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
473 /* error to specify a sign */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
474 if (format->sign != '\0') {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
475 PyErr_SetString(PyExc_ValueError,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
476 "Sign not allowed with integer"
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
477 " format specifier 'c'");
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
478 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
479 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
480
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
481 /* taken from unicodeobject.c formatchar() */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
482 /* 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
483 /* 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
484 x = PyLong_AsLong(value);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
485 if (x == -1 && PyErr_Occurred())
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
486 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
487 #ifdef Py_UNICODE_WIDE
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
488 if (x < 0 || x > 0x10ffff) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
489 PyErr_SetString(PyExc_OverflowError,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
490 "%c arg not in range(0x110000) "
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
491 "(wide Python build)");
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
492 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
493 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
494 #else
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
495 if (x < 0 || x > 0xffff) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
496 PyErr_SetString(PyExc_OverflowError,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
497 "%c arg not in range(0x10000) "
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
498 "(narrow Python build)");
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
499 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
500 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
501 #endif
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
502 numeric_char = (STRINGLIB_CHAR)x;
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
503 pnumeric_chars = &numeric_char;
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
504 n_digits = 1;
42929
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
505 }
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
506 else {
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
507 int base;
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
508 int leading_chars_to_skip; /* Number of characters added by
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
509 PyNumber_ToBase that we want to
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
510 skip over. */
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
511
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
512 /* 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
513 PyNumber_ToBase */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
514 switch (format->type) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
515 case 'b':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
516 base = 2;
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
517 leading_chars_to_skip = 2; /* 0b */
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
518 break;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
519 case 'o':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
520 base = 8;
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
521 leading_chars_to_skip = 2; /* 0o */
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
522 break;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
523 case 'x':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
524 case 'X':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
525 base = 16;
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
526 leading_chars_to_skip = 2; /* 0x */
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
527 break;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
528 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
529 case 'd':
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
530 case 'n':
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
531 base = 10;
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
532 leading_chars_to_skip = 0;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
533 break;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
534 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
535
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
536 /* Do the hard part, converting to a string in a given base */
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
537 tmp = tostring(value, base);
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
538 if (tmp == NULL)
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
539 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
540
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
541 pnumeric_chars = STRINGLIB_STR(tmp);
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
542 n_digits = STRINGLIB_LEN(tmp);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
543
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
544 /* Remember not to modify what pnumeric_chars points to. it
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
545 might be interned. Only modify it after we copy it into a
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
546 newly allocated output buffer. */
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
547
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
548 /* 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
549 and skip it */
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
550 sign = pnumeric_chars[0];
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
551 if (sign == '-') {
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
552 ++leading_chars_to_skip;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
553 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
554
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
555 /* Skip over the leading chars (0x, 0b, etc.) */
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
556 n_digits -= leading_chars_to_skip;
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
557 pnumeric_chars += leading_chars_to_skip;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
558 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
559
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
560 if (format->type == 'n')
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
561 /* Compute how many additional chars we need to allocate
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
562 to hold the thousands grouping. */
48358
098173e844d1 Merged revisions 64491 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 47393
diff changeset
563 STRINGLIB_GROUPING(NULL, n_digits, n_digits,
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
564 0, &n_grouping_chars, 0);
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
565
48360
7e747de231b3 Merged revisions 64499 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48359
diff changeset
566 /* Calculate the widths of the various leading and trailing parts */
7e747de231b3 Merged revisions 64499 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48359
diff changeset
567 calc_number_widths(&spec, sign, n_digits + n_grouping_chars, format);
7e747de231b3 Merged revisions 64499 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48359
diff changeset
568
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
569 /* Allocate a new string to hold the result */
48360
7e747de231b3 Merged revisions 64499 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48359
diff changeset
570 result = STRINGLIB_NEW(NULL, spec.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
571 if (!result)
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
572 goto done;
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
573 p = STRINGLIB_STR(result);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
574
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
575 /* Fill in the digit parts */
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
576 n_leading_chars = spec.n_lpadding + spec.n_lsign + spec.n_spadding;
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
577 memmove(p + n_leading_chars,
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
578 pnumeric_chars,
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
579 n_digits * sizeof(STRINGLIB_CHAR));
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
580
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
581 /* If type is 'X', convert to uppercase */
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
582 if (format->type == 'X') {
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
583 Py_ssize_t t;
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
584 for (t = 0; t < n_digits; ++t)
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
585 p[t + n_leading_chars] = STRINGLIB_TOUPPER(p[t + n_leading_chars]);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
586 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
587
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
588 /* Insert the grouping, if any, after the uppercasing of 'X', so we can
48359
b1bdcbc97a14 Merged revisions 64496 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48358
diff changeset
589 ensure that grouping chars won't be affected. */
48360
7e747de231b3 Merged revisions 64499 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48359
diff changeset
590 if (n_grouping_chars) {
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
591 /* We know this can't fail, since we've already
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
592 reserved enough space. */
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
593 STRINGLIB_CHAR *pstart = p + n_leading_chars;
48358
098173e844d1 Merged revisions 64491 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 47393
diff changeset
594 int r = STRINGLIB_GROUPING(pstart, n_digits, n_digits,
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
595 spec.n_total+n_grouping_chars-n_leading_chars,
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
596 NULL, 0);
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
597 assert(r);
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
598 }
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
599
48360
7e747de231b3 Merged revisions 64499 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48359
diff changeset
600 /* Fill in the non-digit parts (padding, sign, etc.) */
7e747de231b3 Merged revisions 64499 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48359
diff changeset
601 fill_non_digits(p, &spec, n_digits + n_grouping_chars,
7e747de231b3 Merged revisions 64499 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48359
diff changeset
602 format->fill_char == '\0' ? ' ' : format->fill_char);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
603
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
604 done:
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
605 Py_XDECREF(tmp);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
606 return result;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
607 }
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
608 #endif /* defined FORMAT_LONG || defined FORMAT_INT */
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 /************************************************************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
611 /*********** float formatting *******************************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
612 /************************************************************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
613
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
614 #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
615 #if STRINGLIB_IS_UNICODE
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
616 /* taken from unicodeobject.c */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
617 static Py_ssize_t
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
618 strtounicode(Py_UNICODE *buffer, const char *charbuffer)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
619 {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
620 register Py_ssize_t i;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
621 Py_ssize_t len = strlen(charbuffer);
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
622 for (i = len - 1; i >= 0; --i)
43097
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
623 buffer[i] = (Py_UNICODE) charbuffer[i];
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
624
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
625 return len;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
626 }
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
627 #endif
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
628
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
629 /* see FORMATBUFLEN in unicodeobject.c */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
630 #define FLOAT_FORMATBUFLEN 120
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
631
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
632 /* much of this is taken from unicodeobject.c */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
633 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
634 format_float_internal(PyObject *value,
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
635 const InternalFormatSpec *format)
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
636 {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
637 /* fmt = '%.' + `prec` + `type` + '%%'
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
638 worst case length = 2 + 10 (len of INT_MAX) + 1 + 2 = 15 (use 20)*/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
639 char fmt[20];
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
640
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
641 /* taken from unicodeobject.c */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
642 /* Worst case length calc to ensure no buffer overrun:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
643
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
644 'g' formats:
43097
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
645 fmt = %#.<prec>g
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
646 buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
647 for any double rep.)
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
648 len = 1 + prec + 1 + 2 + 5 = 9 + prec
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
649
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
650 'f' formats:
43097
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
651 buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50)
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
652 len = 1 + 50 + 1 + prec = 52 + prec
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
653
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
654 If prec=0 the effective precision is 1 (the leading digit is
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
655 always given), therefore increase the length by one.
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
656
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
657 */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
658 char charbuf[FLOAT_FORMATBUFLEN];
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
659 Py_ssize_t n_digits;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
660 double x;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
661 Py_ssize_t precision = format->precision;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
662 PyObject *result = NULL;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
663 STRINGLIB_CHAR sign;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
664 char* trailing = "";
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
665 STRINGLIB_CHAR *p;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
666 NumberFieldWidths spec;
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
667 STRINGLIB_CHAR type = format->type;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
668
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
669 #if STRINGLIB_IS_UNICODE
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
670 Py_UNICODE unicodebuf[FLOAT_FORMATBUFLEN];
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
671 #endif
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
672
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
673 /* first, do the conversion as 8-bit chars, using the platform's
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
674 snprintf. then, if needed, convert to unicode. */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
675
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
676 /* 'F' is the same as 'f', per the PEP */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
677 if (type == 'F')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
678 type = 'f';
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
679
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
680 x = PyFloat_AsDouble(value);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
681
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
682 if (x == -1.0 && PyErr_Occurred())
43097
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
683 goto done;
42817
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 (type == '%') {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
686 type = 'f';
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
687 x *= 100;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
688 trailing = "%";
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
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
691 if (precision < 0)
43097
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
692 precision = 6;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
693 if (type == 'f' && (fabs(x) / 1e25) >= 1e25)
43097
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
694 type = 'g';
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
695
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
696 /* cast "type", because if we're in unicode we need to pass a
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
697 8-bit char. this is safe, because we've restricted what "type"
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
698 can be */
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
699 PyOS_snprintf(fmt, sizeof(fmt), "%%.%" PY_FORMAT_SIZE_T "d%c", precision,
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
700 (char)type);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
701
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
702 /* do the actual formatting */
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
703 PyOS_ascii_formatd(charbuf, sizeof(charbuf), fmt, x);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
704
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
705 /* adding trailing to fmt with PyOS_snprintf doesn't work, not
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
706 sure why. we'll just concatentate it here, no harm done. we
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
707 know we can't have a buffer overflow from the fmt size
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
708 analysis */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
709 strcat(charbuf, trailing);
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 /* rather than duplicate the code for snprintf for both unicode
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
712 and 8 bit strings, we just use the 8 bit version and then
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
713 convert to unicode in a separate code path. that's probably
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
714 the lesser of 2 evils. */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
715 #if STRINGLIB_IS_UNICODE
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
716 n_digits = strtounicode(unicodebuf, charbuf);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
717 p = unicodebuf;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
718 #else
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
719 /* compute the length. I believe this is done because the return
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
720 value from snprintf above is unreliable */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
721 n_digits = strlen(charbuf);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
722 p = charbuf;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
723 #endif
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
724
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
725 /* is a sign character present in the output? if so, remember it
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
726 and skip it */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
727 sign = p[0];
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
728 if (sign == '-') {
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
729 ++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
730 --n_digits;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
731 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
732
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
733 calc_number_widths(&spec, sign, n_digits, format);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
734
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
735 /* allocate a string with enough space */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
736 result = STRINGLIB_NEW(NULL, spec.n_total);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
737 if (result == NULL)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
738 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
739
48360
7e747de231b3 Merged revisions 64499 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48359
diff changeset
740 /* Fill in the non-digit parts (padding, sign, etc.) */
7e747de231b3 Merged revisions 64499 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48359
diff changeset
741 fill_non_digits(STRINGLIB_STR(result), &spec, n_digits,
7e747de231b3 Merged revisions 64499 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48359
diff changeset
742 format->fill_char == '\0' ? ' ' : format->fill_char);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
743
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
744 /* fill in the digit parts */
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
745 memmove(STRINGLIB_STR(result) +
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
746 (spec.n_lpadding + spec.n_lsign + spec.n_spadding),
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
747 p,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
748 n_digits * sizeof(STRINGLIB_CHAR));
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
749
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
750 done:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
751 return result;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
752 }
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
753 #endif /* FORMAT_FLOAT */
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
754
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
755 /************************************************************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
756 /*********** built in formatters ****************************************/
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 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
759 FORMAT_STRING(PyObject *obj,
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
760 STRINGLIB_CHAR *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
761 Py_ssize_t format_spec_len)
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
762 {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
763 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
764 PyObject *result = NULL;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
765
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
766 /* 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
767 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
768 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
769 result = STRINGLIB_TOSTR(obj);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
770 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
771 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
772
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
773 /* 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
774 if (!parse_internal_render_format_spec(format_spec, format_spec_len,
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
775 &format, 's'))
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
776 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
777
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
778 /* type conversion? */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
779 switch (format.type) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
780 case 's':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
781 /* 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
782 result = format_string_internal(obj, &format);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
783 break;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
784 default:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
785 /* unknown */
46335
3d9ad27d930b Merged revisions 62199 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45852
diff changeset
786 #if STRINGLIB_IS_UNICODE
3d9ad27d930b Merged revisions 62199 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45852
diff changeset
787 /* If STRINGLIB_CHAR is Py_UNICODE, %c might be out-of-range,
3d9ad27d930b Merged revisions 62199 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45852
diff changeset
788 hence the two cases. If it is char, gcc complains that the
3d9ad27d930b Merged revisions 62199 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45852
diff changeset
789 condition below is always true, hence the ifdef. */
3d9ad27d930b Merged revisions 62199 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45852
diff changeset
790 if (format.type > 32 && format.type <128)
3d9ad27d930b Merged revisions 62199 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45852
diff changeset
791 #endif
3d9ad27d930b Merged revisions 62199 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45852
diff changeset
792 PyErr_Format(PyExc_ValueError, "Unknown conversion type %c",
3d9ad27d930b Merged revisions 62199 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45852
diff changeset
793 (char)format.type);
3d9ad27d930b Merged revisions 62199 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45852
diff changeset
794 #if STRINGLIB_IS_UNICODE
3d9ad27d930b Merged revisions 62199 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45852
diff changeset
795 else
3d9ad27d930b Merged revisions 62199 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45852
diff changeset
796 PyErr_Format(PyExc_ValueError, "Unknown conversion type '\\x%x'",
3d9ad27d930b Merged revisions 62199 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45852
diff changeset
797 (unsigned int)format.type);
3d9ad27d930b Merged revisions 62199 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45852
diff changeset
798 #endif
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
799 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
800 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
801
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
802 done:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
803 return result;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
804 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
805
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
806 #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
807 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
808 format_int_or_long(PyObject* obj,
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
809 STRINGLIB_CHAR *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
810 Py_ssize_t format_spec_len,
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
811 IntOrLongToString tostring)
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
812 {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
813 PyObject *result = NULL;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
814 PyObject *tmp = NULL;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
815 InternalFormatSpec format;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
816
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
817 /* 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
818 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
819 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
820 result = STRINGLIB_TOSTR(obj);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
821 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
822 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
823
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
824 /* 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
825 if (!parse_internal_render_format_spec(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
826 format_spec_len,
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
827 &format, 'd'))
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
828 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
829
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
830 /* type conversion? */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
831 switch (format.type) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
832 case 'b':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
833 case 'c':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
834 case 'd':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
835 case 'o':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
836 case 'x':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
837 case 'X':
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
838 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
839 /* no type conversion needed, already an int (or long). do
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
840 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
841 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
842 break;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
843
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
844 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
845 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
846 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
847 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
848 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
849 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
850 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
851 /* 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
852 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
853 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
854 goto done;
47393
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
855 result = format_float_internal(obj, &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
856 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
857
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
858 default:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
859 /* unknown */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
860 PyErr_Format(PyExc_ValueError, "Unknown conversion type %c",
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
861 format.type);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
862 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
863 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
864
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
865 done:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
866 Py_XDECREF(tmp);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
867 return result;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
868 }
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
869 #endif /* FORMAT_LONG || defined FORMAT_INT */
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
870
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
871 #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
872 /* 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
873 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
874 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
875 #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
876 #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
877 #else
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
878 static PyObject*
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
879 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
880 {
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
881 /* 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
882 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
883 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
884 /* 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
885 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
886 }
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
887 #endif
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
888
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
889 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
890 FORMAT_LONG(PyObject *obj,
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
891 STRINGLIB_CHAR *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
892 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
893 {
47393
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
894 return format_int_or_long(obj, format_spec, format_spec_len,
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
895 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
896 }
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
897 #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
898
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
899 #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
900 /* 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
901 static PyObject*
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
902 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
903 {
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
904 /* 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
905 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
906 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
907 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
908 }
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
909
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
910 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
911 FORMAT_INT(PyObject *obj,
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
912 STRINGLIB_CHAR *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
913 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
914 {
47393
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
915 return format_int_or_long(obj, format_spec, format_spec_len,
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
916 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
917 }
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
918 #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
919
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
920 #ifdef FORMAT_FLOAT
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
921 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
922 FORMAT_FLOAT(PyObject *obj,
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
923 STRINGLIB_CHAR *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
924 Py_ssize_t format_spec_len)
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
925 {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
926 PyObject *result = NULL;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
927 InternalFormatSpec format;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
928
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
929 /* 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
930 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
931 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
932 result = STRINGLIB_TOSTR(obj);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
933 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
934 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
935
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
936 /* 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
937 if (!parse_internal_render_format_spec(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
938 format_spec_len,
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
939 &format, '\0'))
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
940 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
941
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
942 /* type conversion? */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
943 switch (format.type) {
45852
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
944 case '\0':
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
945 /* 'Z' means like 'g', but with at least one decimal. See
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
946 PyOS_ascii_formatd */
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
947 format.type = 'Z';
30c4e1ca8421 Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45435
diff changeset
948 /* Deliberate fall through to the next case statement */
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
949 case 'e':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
950 case 'E':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
951 case 'f':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
952 case 'F':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
953 case 'g':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
954 case 'G':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
955 case 'n':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
956 case '%':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
957 /* 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
958 result = format_float_internal(obj, &format);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
959 break;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
960
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
961 default:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
962 /* unknown */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
963 PyErr_Format(PyExc_ValueError, "Unknown conversion type %c",
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
964 format.type);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
965 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
966 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
967
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
968 done:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
969 return result;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
970 }
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
971 #endif /* FORMAT_FLOAT */