annotate Objects/stringlib/formatter.h @ 64166:d0e9febfa00a 3.1

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