annotate Objects/stringlib/formatter.h @ 80661:94de77bd0b4b 2.7

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