annotate Objects/stringlib/formatter.h @ 51810:c8d06dfd4c39 3.0

Block all blocking commits on the py3k branch.
author Georg Brandl <georg@python.org>
date Mon, 23 Feb 2009 10:42:25 +0000
parents 4d3baac2e1a2
children f49a97a979eb
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
51758
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
18 /* Raises an exception about an unknown presentation type for this
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
19 * type. */
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
20
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
21 static void
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
22 unknown_presentation_type(STRINGLIB_CHAR presentation_type,
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
23 const char* type_name)
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
24 {
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
25 #if STRINGLIB_IS_UNICODE
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
26 /* If STRINGLIB_CHAR is Py_UNICODE, %c might be out-of-range,
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
27 hence the two cases. If it is char, gcc complains that the
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
28 condition below is always true, hence the ifdef. */
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
29 if (presentation_type > 32 && presentation_type < 128)
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
30 #endif
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
31 PyErr_Format(PyExc_ValueError,
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
32 "Unknown format code '%c' "
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
33 "for object of type '%.200s'",
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
34 presentation_type,
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
35 type_name);
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
36 #if STRINGLIB_IS_UNICODE
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
37 else
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
38 PyErr_Format(PyExc_ValueError,
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
39 "Unknown format code '\\x%x' "
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
40 "for object of type '%.200s'",
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
41 (unsigned int)presentation_type,
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
42 type_name);
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
43 #endif
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
44 }
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
45
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
46 /*
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
47 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
48 input string, updates *result with the corresponding positive
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
49 integer, and returns the number of digits consumed.
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 returns -1 on error.
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 static int
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
54 get_integer(STRINGLIB_CHAR **ptr, STRINGLIB_CHAR *end,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
55 Py_ssize_t *result)
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 Py_ssize_t accumulator, digitval, oldaccumulator;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
58 int numdigits;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
59 accumulator = numdigits = 0;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
60 for (;;(*ptr)++, numdigits++) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
61 if (*ptr >= end)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
62 break;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
63 digitval = STRINGLIB_TODECIMAL(**ptr);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
64 if (digitval < 0)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
65 break;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
66 /*
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
67 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
68 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
69 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
70 expect too many digits.
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 oldaccumulator = accumulator;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
73 accumulator *= 10;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
74 if ((accumulator+10)/10 != oldaccumulator+1) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
75 PyErr_Format(PyExc_ValueError,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
76 "Too many decimal digits in format string");
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
77 return -1;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
78 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
79 accumulator += digitval;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
80 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
81 *result = accumulator;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
82 return numdigits;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
83 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
84
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
85 /************************************************************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
86 /*********** standard format specifier parsing **************************/
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 /* 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
90 Py_LOCAL_INLINE(int)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
91 is_alignment_token(STRINGLIB_CHAR c)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
92 {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
93 switch (c) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
94 case '<': case '>': case '=': case '^':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
95 return 1;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
96 default:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
97 return 0;
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 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
100
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
101 /* 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
102 Py_LOCAL_INLINE(int)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
103 is_sign_element(STRINGLIB_CHAR c)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
104 {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
105 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
106 case ' ': case '+': case '-':
42981
0f82fc42fd68 Corrected missed #if in r57652.
Eric Smith <eric@trueblade.com>
parents: 42980
diff changeset
107 #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
108 case '(':
42981
0f82fc42fd68 Corrected missed #if in r57652.
Eric Smith <eric@trueblade.com>
parents: 42980
diff changeset
109 #endif
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
110 return 1;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
111 default:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
112 return 0;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
113 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
114 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
115
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
116
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
117 typedef struct {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
118 STRINGLIB_CHAR fill_char;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
119 STRINGLIB_CHAR align;
48472
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
120 int alternate;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
121 STRINGLIB_CHAR sign;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
122 Py_ssize_t width;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
123 Py_ssize_t precision;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
124 STRINGLIB_CHAR type;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
125 } InternalFormatSpec;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
126
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
127 /*
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
128 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
129 fills in format with the parsed information.
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
130 returns 1 on success, 0 on failure.
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
131 if failure, sets the exception
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
132 */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
133 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
134 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
135 Py_ssize_t format_spec_len,
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
136 InternalFormatSpec *format,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
137 char default_type)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
138 {
47393
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
139 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
140 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
141
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
142 /* 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
143 the input string */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
144
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
145 Py_ssize_t specified_width;
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 format->fill_char = '\0';
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
148 format->align = '\0';
48472
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
149 format->alternate = 0;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
150 format->sign = '\0';
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
151 format->width = -1;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
152 format->precision = -1;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
153 format->type = default_type;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
154
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
155 /* If the second char is an alignment token,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
156 then parse the fill char */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
157 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
158 format->align = ptr[1];
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
159 format->fill_char = ptr[0];
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
160 ptr += 2;
42929
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
161 }
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
162 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
163 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
164 ++ptr;
42817
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 the various sign options */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
168 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
169 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
170 ++ptr;
42980
9df911d654af Added conditional compilation for '()', which was an allowed sign code in a
Eric Smith <eric@trueblade.com>
parents: 42929
diff changeset
171 #if ALLOW_PARENS_FOR_SIGN
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
172 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
173 ++ptr;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
174 }
42980
9df911d654af Added conditional compilation for '()', which was an allowed sign code in a
Eric Smith <eric@trueblade.com>
parents: 42929
diff changeset
175 #endif
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
176 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
177
48482
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
178 /* If the next character is #, we're in alternate mode. This only
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
179 applies to integers. */
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
180 if (end-ptr >= 1 && ptr[0] == '#') {
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
181 format->alternate = 1;
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
182 ++ptr;
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
183 }
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
184
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
185 /* The special case for 0-padding (backwards compat) */
43097
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
186 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
187 format->fill_char = '0';
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
188 if (format->align == '\0') {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
189 format->align = '=';
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
190 }
45435
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45382
diff changeset
191 ++ptr;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
192 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
193
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
194 /* XXX add error checking */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
195 specified_width = get_integer(&ptr, end, &format->width);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
196
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
197 /* 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
198 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
199 get_integer() will have set it to zero */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
200 if (specified_width == 0) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
201 format->width = -1;
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 /* Parse field precision */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
205 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
206 ++ptr;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
207
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
208 /* XXX add error checking */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
209 specified_width = get_integer(&ptr, end, &format->precision);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
210
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
211 /* 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
212 if (specified_width == 0) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
213 PyErr_Format(PyExc_ValueError,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
214 "Format specifier missing precision");
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
215 return 0;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
216 }
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 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
219
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
220 /* Finally, parse the type field */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
221
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
222 if (end-ptr > 1) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
223 /* invalid conversion spec */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
224 PyErr_Format(PyExc_ValueError, "Invalid conversion specification");
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
225 return 0;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
226 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
227
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
228 if (end-ptr == 1) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
229 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
230 ++ptr;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
231 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
232
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
233 return 1;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
234 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
235
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
236 #if defined FORMAT_FLOAT || defined FORMAT_LONG
42817
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 /*********** common routines for numeric formatting *********************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
239 /************************************************************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
240
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
241 /* describes the layout for an integer, see the comment in
48482
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
242 calc_number_widths() for details */
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
243 typedef struct {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
244 Py_ssize_t n_lpadding;
48482
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
245 Py_ssize_t n_prefix;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
246 Py_ssize_t n_spadding;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
247 Py_ssize_t n_rpadding;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
248 char lsign;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
249 Py_ssize_t n_lsign;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
250 char rsign;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
251 Py_ssize_t n_rsign;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
252 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
253 other fields */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
254 } NumberFieldWidths;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
255
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
256 /* 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
257 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
258 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
259 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
260 static void
48504
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
261 calc_number_widths(NumberFieldWidths *spec, STRINGLIB_CHAR actual_sign,
48472
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
262 Py_ssize_t n_prefix, Py_ssize_t n_digits,
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
263 const InternalFormatSpec *format)
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
264 {
48504
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
265 spec->n_lpadding = 0;
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
266 spec->n_prefix = 0;
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
267 spec->n_spadding = 0;
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
268 spec->n_rpadding = 0;
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
269 spec->lsign = '\0';
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
270 spec->n_lsign = 0;
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
271 spec->rsign = '\0';
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
272 spec->n_rsign = 0;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
273
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
274 /* the output will look like:
48472
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
275 | |
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
276 | <lpadding> <lsign> <prefix> <spadding> <digits> <rsign> <rpadding> |
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
277 | |
42817
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 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
280 sign of the number
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
281
48472
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
282 prefix is given (it's for the '0x' prefix)
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
283
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
284 digits is already known
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
285
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
286 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
287 actual digits
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
288
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
289 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
290 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
291 */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
292
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
293 /* 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
294 if (format->sign == '+') {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
295 /* always put a + or - */
48504
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
296 spec->n_lsign = 1;
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
297 spec->lsign = (actual_sign == '-' ? '-' : '+');
42929
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
298 }
42980
9df911d654af Added conditional compilation for '()', which was an allowed sign code in a
Eric Smith <eric@trueblade.com>
parents: 42929
diff changeset
299 #if ALLOW_PARENS_FOR_SIGN
42929
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
300 else if (format->sign == '(') {
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
301 if (actual_sign == '-') {
48504
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
302 spec->n_lsign = 1;
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
303 spec->lsign = '(';
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
304 spec->n_rsign = 1;
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
305 spec->rsign = ')';
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
306 }
42929
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
307 }
42980
9df911d654af Added conditional compilation for '()', which was an allowed sign code in a
Eric Smith <eric@trueblade.com>
parents: 42929
diff changeset
308 #endif
42929
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
309 else if (format->sign == ' ') {
48504
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
310 spec->n_lsign = 1;
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
311 spec->lsign = (actual_sign == '-' ? '-' : ' ');
42929
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
312 }
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
313 else {
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
314 /* non specified, or the default (-) */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
315 if (actual_sign == '-') {
48504
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
316 spec->n_lsign = 1;
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
317 spec->lsign = '-';
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 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
320
48504
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
321 spec->n_prefix = n_prefix;
48482
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
322
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
323 /* now the number of padding characters */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
324 if (format->width == -1) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
325 /* 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
326 }
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
327 else {
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
328 /* see if any padding is needed */
48504
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
329 if (spec->n_lsign + n_digits + spec->n_rsign +
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
330 spec->n_prefix >= format->width) {
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
331 /* 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
332 requested width */
42929
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
333 }
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
334 else {
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
335 /* 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
336 needed */
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
337 Py_ssize_t padding = format->width -
48504
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
338 (spec->n_lsign + spec->n_prefix +
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
339 n_digits + spec->n_rsign);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
340 if (format->align == '<')
48504
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
341 spec->n_rpadding = padding;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
342 else if (format->align == '>')
48504
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
343 spec->n_lpadding = padding;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
344 else if (format->align == '^') {
48504
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
345 spec->n_lpadding = padding / 2;
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
346 spec->n_rpadding = padding - spec->n_lpadding;
42929
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
347 }
43097
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
348 else if (format->align == '=')
48504
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
349 spec->n_spadding = padding;
42929
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
350 else
48504
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
351 spec->n_lpadding = padding;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
352 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
353 }
48504
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
354 spec->n_total = spec->n_lpadding + spec->n_lsign + spec->n_prefix +
59ac5aef3b2d Merged revisions 65023 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48482
diff changeset
355 spec->n_spadding + n_digits + spec->n_rsign + spec->n_rpadding;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
356 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
357
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
358 /* fill in the non-digit parts of a numbers's string representation,
48482
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
359 as determined in calc_number_widths(). returns the pointer to
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
360 where the digits go. */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
361 static STRINGLIB_CHAR *
48360
7e747de231b3 Merged revisions 64499 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48359
diff changeset
362 fill_non_digits(STRINGLIB_CHAR *p_buf, const NumberFieldWidths *spec,
48482
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
363 STRINGLIB_CHAR *prefix, Py_ssize_t n_digits,
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
364 STRINGLIB_CHAR fill_char)
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
365 {
48482
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
366 STRINGLIB_CHAR *p_digits;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
367
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
368 if (spec->n_lpadding) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
369 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
370 p_buf += spec->n_lpadding;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
371 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
372 if (spec->n_lsign == 1) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
373 *p_buf++ = spec->lsign;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
374 }
48482
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
375 if (spec->n_prefix) {
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
376 memmove(p_buf,
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
377 prefix,
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
378 spec->n_prefix * sizeof(STRINGLIB_CHAR));
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
379 p_buf += spec->n_prefix;
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
380 }
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
381 if (spec->n_spadding) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
382 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
383 p_buf += spec->n_spadding;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
384 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
385 p_digits = p_buf;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
386 p_buf += n_digits;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
387 if (spec->n_rsign == 1) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
388 *p_buf++ = spec->rsign;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
389 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
390 if (spec->n_rpadding) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
391 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
392 p_buf += spec->n_rpadding;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
393 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
394 return p_digits;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
395 }
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
396 #endif /* FORMAT_FLOAT || FORMAT_LONG */
42817
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 /************************************************************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
399 /*********** string formatting ******************************************/
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
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
402 static PyObject *
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
403 format_string_internal(PyObject *value, const InternalFormatSpec *format)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
404 {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
405 Py_ssize_t width; /* total field width */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
406 Py_ssize_t lpad;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
407 STRINGLIB_CHAR *dst;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
408 STRINGLIB_CHAR *src = STRINGLIB_STR(value);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
409 Py_ssize_t len = STRINGLIB_LEN(value);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
410 PyObject *result = NULL;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
411
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
412 /* sign is not allowed on strings */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
413 if (format->sign != '\0') {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
414 PyErr_SetString(PyExc_ValueError,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
415 "Sign not allowed in string format specifier");
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
416 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
417 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
418
48472
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
419 /* alternate is not allowed on strings */
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
420 if (format->alternate) {
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
421 PyErr_SetString(PyExc_ValueError,
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
422 "Alternate form (#) not allowed in string format "
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
423 "specifier");
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
424 goto done;
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
425 }
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
426
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
427 /* '=' alignment not allowed on strings */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
428 if (format->align == '=') {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
429 PyErr_SetString(PyExc_ValueError,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
430 "'=' alignment not allowed "
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
431 "in string format specifier");
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
432 goto done;
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 /* 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
436 characters */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
437 if (format->precision >= 0 && len >= format->precision) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
438 len = format->precision;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
439 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
440
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
441 if (format->width >= 0) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
442 width = format->width;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
443
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
444 /* but use at least len characters */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
445 if (len > width) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
446 width = len;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
447 }
42929
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
448 }
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
449 else {
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
450 /* 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
451 width = len;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
452 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
453
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
454 /* allocate the resulting string */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
455 result = STRINGLIB_NEW(NULL, width);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
456 if (result == NULL)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
457 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
458
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
459 /* now write into that space */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
460 dst = STRINGLIB_STR(result);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
461
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
462 /* 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
463 aligning */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
464 if (format->align == '>')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
465 lpad = width - len;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
466 else if (format->align == '^')
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
467 lpad = (width - len) / 2;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
468 else
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
469 lpad = 0;
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 /* 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
472 left */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
473 memcpy(dst + lpad, src, len * sizeof(STRINGLIB_CHAR));
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
474
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
475 /* do any padding */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
476 if (width > len) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
477 STRINGLIB_CHAR fill_char = format->fill_char;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
478 if (fill_char == '\0') {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
479 /* use the default, if not specified */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
480 fill_char = ' ';
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
481 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
482
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
483 /* pad on left */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
484 if (lpad)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
485 STRINGLIB_FILL(dst, fill_char, lpad);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
486
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
487 /* pad on right */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
488 if (width - len - lpad)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
489 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
490 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
491
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
492 done:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
493 return result;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
494 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
495
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
496
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
497 /************************************************************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
498 /*********** long formatting ********************************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
499 /************************************************************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
500
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
501 #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
502 typedef PyObject*
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
503 (*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
504
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
505 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
506 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
507 IntOrLongToString tostring)
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
508 {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
509 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
510 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
511 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
512 STRINGLIB_CHAR numeric_char;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
513 STRINGLIB_CHAR sign = '\0';
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
514 STRINGLIB_CHAR *p;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
515 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
516 string */
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
517 Py_ssize_t n_leading_chars;
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
518 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
519 allocate, used for 'n'
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
520 formatting. */
48482
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
521 Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
522 STRINGLIB_CHAR *prefix = NULL;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
523 NumberFieldWidths spec;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
524 long x;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
525
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
526 /* no precision allowed on integers */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
527 if (format->precision != -1) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
528 PyErr_SetString(PyExc_ValueError,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
529 "Precision not allowed in integer format specifier");
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
530 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
531 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
532
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
533
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
534 /* special case for character formatting */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
535 if (format->type == 'c') {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
536 /* error to specify a sign */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
537 if (format->sign != '\0') {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
538 PyErr_SetString(PyExc_ValueError,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
539 "Sign not allowed with integer"
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
540 " format specifier 'c'");
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
541 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
542 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
543
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
544 /* taken from unicodeobject.c formatchar() */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
545 /* 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
546 /* 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
547 x = PyLong_AsLong(value);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
548 if (x == -1 && PyErr_Occurred())
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
549 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
550 #ifdef Py_UNICODE_WIDE
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
551 if (x < 0 || x > 0x10ffff) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
552 PyErr_SetString(PyExc_OverflowError,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
553 "%c arg not in range(0x110000) "
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
554 "(wide Python build)");
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
555 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
556 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
557 #else
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
558 if (x < 0 || x > 0xffff) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
559 PyErr_SetString(PyExc_OverflowError,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
560 "%c arg not in range(0x10000) "
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
561 "(narrow Python build)");
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
562 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
563 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
564 #endif
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
565 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
566 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
567 n_digits = 1;
42929
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
568 }
11ec4097fafe Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents: 42817
diff changeset
569 else {
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
570 int base;
48472
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
571 int leading_chars_to_skip = 0; /* Number of characters added by
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
572 PyNumber_ToBase that we want to
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
573 skip over. */
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
574
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
575 /* 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
576 PyNumber_ToBase */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
577 switch (format->type) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
578 case 'b':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
579 base = 2;
48482
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
580 leading_chars_to_skip = 2; /* 0b */
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
581 break;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
582 case 'o':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
583 base = 8;
48482
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
584 leading_chars_to_skip = 2; /* 0o */
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
585 break;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
586 case 'x':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
587 case 'X':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
588 base = 16;
48482
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
589 leading_chars_to_skip = 2; /* 0x */
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
590 break;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
591 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
592 case 'd':
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
593 case 'n':
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
594 base = 10;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
595 break;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
596 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
597
48482
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
598 /* The number of prefix chars is the same as the leading
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
599 chars to skip */
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
600 if (format->alternate)
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
601 n_prefix = leading_chars_to_skip;
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
602
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
603 /* 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
604 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
605 if (tmp == NULL)
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
606 goto done;
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 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
609 n_digits = STRINGLIB_LEN(tmp);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
610
48482
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
611 prefix = pnumeric_chars;
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
612
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
613 /* 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
614 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
615 newly allocated output buffer. */
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
616
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
617 /* 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
618 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
619 sign = pnumeric_chars[0];
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
620 if (sign == '-') {
48482
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
621 ++prefix;
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
622 ++leading_chars_to_skip;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
623 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
624
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
625 /* 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
626 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
627 pnumeric_chars += leading_chars_to_skip;
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
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
630 if (format->type == 'n')
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
631 /* 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
632 to hold the thousands grouping. */
48358
098173e844d1 Merged revisions 64491 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 47393
diff changeset
633 STRINGLIB_GROUPING(NULL, n_digits, n_digits,
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
634 0, &n_grouping_chars, 0);
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
635
48360
7e747de231b3 Merged revisions 64499 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48359
diff changeset
636 /* Calculate the widths of the various leading and trailing parts */
48482
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
637 calc_number_widths(&spec, sign, n_prefix, n_digits + n_grouping_chars,
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
638 format);
48360
7e747de231b3 Merged revisions 64499 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48359
diff changeset
639
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
640 /* 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
641 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
642 if (!result)
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
643 goto done;
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
644 p = STRINGLIB_STR(result);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
645
48482
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
646 /* XXX There is too much magic here regarding the internals of
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
647 spec and the location of the prefix and digits. It would be
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
648 better if calc_number_widths returned a number of logical
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
649 offsets into the buffer, and those were used. Maybe in a
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
650 future code cleanup. */
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
651
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
652 /* Fill in the digit parts */
48482
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
653 n_leading_chars = spec.n_lpadding + spec.n_lsign +
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
654 spec.n_prefix + spec.n_spadding;
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
655 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
656 pnumeric_chars,
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
657 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
658
48482
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
659 /* If type is 'X', convert the filled in digits 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
660 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
661 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
662 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
663 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
664 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
665
48482
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
666 /* Insert the grouping, if any, after the uppercasing of the digits, so
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
667 we can 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
668 if (n_grouping_chars) {
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
669 /* 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
670 reserved enough space. */
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
671 STRINGLIB_CHAR *pstart = p + n_leading_chars;
48848
5c75a33ef5b2 Merged revisions 66006 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents: 48523
diff changeset
672 #ifndef NDEBUG
5c75a33ef5b2 Merged revisions 66006 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents: 48523
diff changeset
673 int r =
5c75a33ef5b2 Merged revisions 66006 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents: 48523
diff changeset
674 #endif
5c75a33ef5b2 Merged revisions 66006 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents: 48523
diff changeset
675 STRINGLIB_GROUPING(pstart, n_digits, n_digits,
48482
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
676 spec.n_total+n_grouping_chars-n_leading_chars,
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
677 NULL, 0);
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
678 assert(r);
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
679 }
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
680
48360
7e747de231b3 Merged revisions 64499 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48359
diff changeset
681 /* Fill in the non-digit parts (padding, sign, etc.) */
48482
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
682 fill_non_digits(p, &spec, prefix, n_digits + n_grouping_chars,
48360
7e747de231b3 Merged revisions 64499 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48359
diff changeset
683 format->fill_char == '\0' ? ' ' : format->fill_char);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
684
48482
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
685 /* If type is 'X', uppercase the prefix. This has to be done after the
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
686 prefix is filled in by fill_non_digits */
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
687 if (format->type == 'X') {
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
688 Py_ssize_t t;
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
689 for (t = 0; t < n_prefix; ++t)
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
690 p[t + spec.n_lpadding + spec.n_lsign] =
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
691 STRINGLIB_TOUPPER(p[t + spec.n_lpadding + spec.n_lsign]);
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
692 }
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
693
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
694
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
695 done:
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
696 Py_XDECREF(tmp);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
697 return result;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
698 }
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
699 #endif /* defined FORMAT_LONG || defined FORMAT_INT */
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
700
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
701 /************************************************************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
702 /*********** float formatting *******************************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
703 /************************************************************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
704
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
705 #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
706 #if STRINGLIB_IS_UNICODE
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
707 /* taken from unicodeobject.c */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
708 static Py_ssize_t
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
709 strtounicode(Py_UNICODE *buffer, const char *charbuffer)
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 register Py_ssize_t i;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
712 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
713 for (i = len - 1; i >= 0; --i)
43097
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
714 buffer[i] = (Py_UNICODE) charbuffer[i];
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
715
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
716 return len;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
717 }
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
718 #endif
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
719
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
720 /* see FORMATBUFLEN in unicodeobject.c */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
721 #define FLOAT_FORMATBUFLEN 120
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
722
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
723 /* much of this is taken from unicodeobject.c */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
724 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
725 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
726 const InternalFormatSpec *format)
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
727 {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
728 /* fmt = '%.' + `prec` + `type` + '%%'
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
729 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
730 char fmt[20];
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 /* taken from unicodeobject.c */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
733 /* 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
734
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
735 'g' formats:
43097
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
736 fmt = %#.<prec>g
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
737 buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
738 for any double rep.)
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
739 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
740
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
741 'f' formats:
43097
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
742 buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50)
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
743 len = 1 + 50 + 1 + prec = 52 + prec
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
744
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
745 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
746 always given), therefore increase the length by one.
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
747
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
748 */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
749 char charbuf[FLOAT_FORMATBUFLEN];
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
750 Py_ssize_t n_digits;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
751 double x;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
752 Py_ssize_t precision = format->precision;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
753 PyObject *result = NULL;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
754 STRINGLIB_CHAR sign;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
755 char* trailing = "";
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
756 STRINGLIB_CHAR *p;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
757 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
758 STRINGLIB_CHAR type = format->type;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
759
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
760 #if STRINGLIB_IS_UNICODE
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
761 Py_UNICODE unicodebuf[FLOAT_FORMATBUFLEN];
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
762 #endif
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
763
48472
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
764 /* alternate is not allowed on floats. */
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
765 if (format->alternate) {
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
766 PyErr_SetString(PyExc_ValueError,
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
767 "Alternate form (#) not allowed in float format "
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
768 "specifier");
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
769 goto done;
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
770 }
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
771
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
772 /* 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
773 snprintf. then, if needed, convert to unicode. */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
774
48523
3d526525b3e8 Backed out r65073, pending fixing it in Windows.
Eric Smith <eric@trueblade.com>
parents: 48522
diff changeset
775 /* 'F' is the same as 'f', per the PEP */
3d526525b3e8 Backed out r65073, pending fixing it in Windows.
Eric Smith <eric@trueblade.com>
parents: 48522
diff changeset
776 if (type == 'F')
3d526525b3e8 Backed out r65073, pending fixing it in Windows.
Eric Smith <eric@trueblade.com>
parents: 48522
diff changeset
777 type = 'f';
3d526525b3e8 Backed out r65073, pending fixing it in Windows.
Eric Smith <eric@trueblade.com>
parents: 48522
diff changeset
778
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
779 x = PyFloat_AsDouble(value);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
780
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
781 if (x == -1.0 && PyErr_Occurred())
43097
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
782 goto done;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
783
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
784 if (type == '%') {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
785 type = 'f';
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
786 x *= 100;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
787 trailing = "%";
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
788 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
789
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
790 if (precision < 0)
43097
a9acc06da57a Added format tests.
Eric Smith <eric@trueblade.com>
parents: 42998
diff changeset
791 precision = 6;
48523
3d526525b3e8 Backed out r65073, pending fixing it in Windows.
Eric Smith <eric@trueblade.com>
parents: 48522
diff changeset
792 if (type == 'f' && (fabs(x) / 1e25) >= 1e25)
3d526525b3e8 Backed out r65073, pending fixing it in Windows.
Eric Smith <eric@trueblade.com>
parents: 48522
diff changeset
793 type = 'g';
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
794
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
795 /* 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
796 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
797 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
798 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
799 (char)type);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
800
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
801 /* 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
802 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
803
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
804 /* 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
805 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
806 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
807 analysis */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
808 strcat(charbuf, trailing);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
809
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
810 /* 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
811 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
812 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
813 the lesser of 2 evils. */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
814 #if STRINGLIB_IS_UNICODE
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
815 n_digits = strtounicode(unicodebuf, charbuf);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
816 p = unicodebuf;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
817 #else
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
818 /* 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
819 value from snprintf above is unreliable */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
820 n_digits = strlen(charbuf);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
821 p = charbuf;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
822 #endif
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 /* 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
825 and skip it */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
826 sign = p[0];
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
827 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
828 ++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
829 --n_digits;
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
830 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
831
48472
d4e473291a17 Forward port of r64958.
Eric Smith <eric@trueblade.com>
parents: 48360
diff changeset
832 calc_number_widths(&spec, sign, 0, n_digits, format);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
833
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
834 /* allocate a string with enough space */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
835 result = STRINGLIB_NEW(NULL, spec.n_total);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
836 if (result == NULL)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
837 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
838
48360
7e747de231b3 Merged revisions 64499 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48359
diff changeset
839 /* Fill in the non-digit parts (padding, sign, etc.) */
48482
c662e8cffddf Merged revisions 64984 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48472
diff changeset
840 fill_non_digits(STRINGLIB_STR(result), &spec, NULL, n_digits,
48360
7e747de231b3 Merged revisions 64499 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48359
diff changeset
841 format->fill_char == '\0' ? ' ' : format->fill_char);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
842
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
843 /* 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
844 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
845 (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
846 p,
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
847 n_digits * sizeof(STRINGLIB_CHAR));
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
848
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
849 done:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
850 return result;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
851 }
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
852 #endif /* FORMAT_FLOAT */
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
853
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
854 /************************************************************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
855 /*********** built in formatters ****************************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
856 /************************************************************************/
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
857 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
858 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
859 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
860 Py_ssize_t format_spec_len)
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
861 {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
862 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
863 PyObject *result = NULL;
42817
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 /* 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
866 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
867 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
868 result = STRINGLIB_TOSTR(obj);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
869 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
870 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
871
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
872 /* 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
873 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
874 &format, 's'))
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
875 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
876
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
877 /* type conversion? */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
878 switch (format.type) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
879 case 's':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
880 /* 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
881 result = format_string_internal(obj, &format);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
882 break;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
883 default:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
884 /* unknown */
51758
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
885 unknown_presentation_type(format.type, obj->ob_type->tp_name);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
886 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
887 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
888
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
889 done:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
890 return result;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
891 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
892
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
893 #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
894 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
895 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
896 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
897 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
898 IntOrLongToString tostring)
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
899 {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
900 PyObject *result = NULL;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
901 PyObject *tmp = NULL;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
902 InternalFormatSpec format;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
903
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
904 /* 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
905 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
906 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
907 result = STRINGLIB_TOSTR(obj);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
908 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
909 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
910
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
911 /* 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
912 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
913 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
914 &format, 'd'))
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
915 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
916
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
917 /* type conversion? */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
918 switch (format.type) {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
919 case 'b':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
920 case 'c':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
921 case 'd':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
922 case 'o':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
923 case 'x':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
924 case 'X':
46909
ef4ec968fdb6 Merged revisions 63078 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 46335
diff changeset
925 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
926 /* 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
927 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
928 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
929 break;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
930
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
931 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
932 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
933 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
934 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
935 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
936 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
937 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
938 /* 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
939 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
940 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
941 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
942 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
943 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
944
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
945 default:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
946 /* unknown */
51758
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
947 unknown_presentation_type(format.type, obj->ob_type->tp_name);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
948 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
949 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
950
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
951 done:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
952 Py_XDECREF(tmp);
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
953 return result;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
954 }
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
955 #endif /* FORMAT_LONG || defined FORMAT_INT */
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
956
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
957 #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
958 /* 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
959 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
960 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
961 #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
962 #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
963 #else
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
964 static PyObject*
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
965 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
966 {
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
967 /* 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
968 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
969 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
970 /* 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
971 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
972 }
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
973 #endif
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
974
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
975 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
976 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
977 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
978 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
979 {
47393
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
980 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
981 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
982 }
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
983 #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
984
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
985 #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
986 /* 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
987 static PyObject*
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
988 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
989 {
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
990 /* 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
991 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
992 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
993 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
994 }
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
995
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
996 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
997 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
998 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
999 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
1000 {
47393
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
1001 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
1002 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
1003 }
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
1004 #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
1005
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
1006 #ifdef FORMAT_FLOAT
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1007 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
1008 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
1009 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
1010 Py_ssize_t format_spec_len)
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1011 {
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1012 PyObject *result = NULL;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1013 InternalFormatSpec format;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1014
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1015 /* 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
1016 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
1017 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
1018 result = STRINGLIB_TOSTR(obj);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1019 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1020 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1021
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1022 /* 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
1023 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
1024 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
1025 &format, '\0'))
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1026 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1027
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1028 /* type conversion? */
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1029 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
1030 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
1031 /* '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
1032 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
1033 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
1034 /* 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
1035 case 'e':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1036 case 'E':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1037 case 'f':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1038 case 'F':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1039 case 'g':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1040 case 'G':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1041 case 'n':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1042 case '%':
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1043 /* 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
1044 result = format_float_internal(obj, &format);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1045 break;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1046
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1047 default:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1048 /* unknown */
51758
4d3baac2e1a2 Merged revisions 69808 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 48848
diff changeset
1049 unknown_presentation_type(format.type, obj->ob_type->tp_name);
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1050 goto done;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1051 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1052
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1053 done:
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1054 return result;
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1055 }
45382
7af955843929 Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents: 45032
diff changeset
1056 #endif /* FORMAT_FLOAT */