Mercurial > cpython-lru_cache-weakref
annotate Objects/stringlib/formatter.h @ 45852:30c4e1ca8421
Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61431 | vinay.sajip | 2008-03-16 22:35:58 +0100 (So, 16 Mär 2008) | 1 line
Clarified documentation on use of shutdown().
........
r61433 | mark.summerfield | 2008-03-17 09:28:15 +0100 (Mo, 17 Mär 2008) | 5 lines
Added a footnote to each pointing out that for XML output if an encoding
string is given it should conform to the appropriate XML standards---for
example, "UTF-8" is okay, but "UTF8" is not.
........
r61434 | eric.smith | 2008-03-17 12:01:01 +0100 (Mo, 17 Mär 2008) | 7 lines
Issue 2264: empty float presentation type needs to have at least one digit past the decimal point.
Added "Z" format_char to PyOS_ascii_formatd to support empty float presentation type.
Renamed buf_size in PyOS_ascii_formatd to more accurately reflect it's meaning.
Modified format.__float__ to use the new "Z" format as the default.
Added test cases.
........
r61435 | eric.smith | 2008-03-17 13:14:29 +0100 (Mo, 17 Mär 2008) | 2 lines
Reformated lines > 79 chars.
Deleted unused macro ISXDIGIT.
........
r61436 | jeffrey.yasskin | 2008-03-17 15:40:53 +0100 (Mo, 17 Mär 2008) | 13 lines
Allow Gnu gcc's to build python on OSX by removing -Wno-long-double,
-no-cpp-precomp, and -mno-fused-madd from configure.
* r22183 added -no-cpp-precomp, which
http://gcc.gnu.org/ml/gcc/2005-12/msg00368.html claims hasn't been
needed since gcc-3.1.
* r25607 added -Wno-long-double to avoid a warning in
Include/objimpl.h (issue 525481). The long double is still there,
but OSX 10.4's gcc no longer warns about it.
* r33666 fixed issue 775892 on OSX 10.3 by adding -mno-fused-madd,
which changed the sign of some float 0s. Tim Peters said it wasn't
a real issue anyway, and it no longer causes test failures.
Fixes issue #1779871.
........
r61439 | martin.v.loewis | 2008-03-17 17:31:57 +0100 (Mo, 17 Mär 2008) | 2 lines
Add Trent Nelson.
........
r61444 | travis.oliphant | 2008-03-17 18:36:12 +0100 (Mo, 17 Mär 2008) | 1 line
Add necessary headers to back-port new buffer protocol to Python 2.6
........
r61449 | gregory.p.smith | 2008-03-17 19:48:05 +0100 (Mo, 17 Mär 2008) | 8 lines
Force zlib.crc32 and zlib.adler32 to return a signed integer on all platforms
regardless of the native sizeof(long) used in the integer object.
This somewhat odd behavior of returning a signed is maintained in 2.x for
compatibility reasons of always returning an integer rather than a long object.
Fixes Issue1202 for Python 2.6
........
r61450 | neal.norwitz | 2008-03-17 20:02:45 +0100 (Mo, 17 Mär 2008) | 3 lines
Use a buffer large enough to ensure we don't overrun, even if the value
is outside the range we expect.
........
r61453 | steven.bethard | 2008-03-17 20:33:11 +0100 (Mo, 17 Mär 2008) | 1 line
Document unicode.isnumeric() and unicode.isdecimal() (issue2326)
........
r61458 | neal.norwitz | 2008-03-17 21:22:43 +0100 (Mo, 17 Mär 2008) | 5 lines
Issue 2321: reduce memory usage (increase the memory that is returned
to the system) by using pymalloc for the data of unicode objects.
Will backport.
........
r61465 | martin.v.loewis | 2008-03-17 22:55:30 +0100 (Mo, 17 Mär 2008) | 2 lines
Add David Wolever.
........
r61468 | gregory.p.smith | 2008-03-18 01:20:01 +0100 (Di, 18 Mär 2008) | 3 lines
Fix the IOError message text when opening a file with an invalid filename.
Error reported by Ilan Schnell.
........
r61471 | brett.cannon | 2008-03-18 02:00:07 +0100 (Di, 18 Mär 2008) | 2 lines
Convert test_strftime, test_getargs, and test_pep247 to use unittest.
........
r61472 | jeffrey.yasskin | 2008-03-18 02:09:59 +0100 (Di, 18 Mär 2008) | 2 lines
Fix build on platforms that don't have intptr_t. Patch by Joseph Armbruster.
........
r61473 | brett.cannon | 2008-03-18 02:50:25 +0100 (Di, 18 Mär 2008) | 2 lines
Convert test_dummy_threading and test_dbm to unittest.
........
r61474 | brett.cannon | 2008-03-18 02:58:56 +0100 (Di, 18 Mär 2008) | 2 lines
Move test_extcall to doctest.
........
r61480 | brett.cannon | 2008-03-18 04:46:22 +0100 (Di, 18 Mär 2008) | 2 lines
test_errno was a no-op test; now it actually tests things and uses unittest.
........
r61483 | brett.cannon | 2008-03-18 05:09:00 +0100 (Di, 18 Mär 2008) | 3 lines
Remove our implementation of memmove() and strerror(); both are in the C89
standard library.
........
r61484 | brett.cannon | 2008-03-18 05:16:06 +0100 (Di, 18 Mär 2008) | 2 lines
The output directory for tests that compare against stdout is now gone!
........
r61488 | jeffrey.yasskin | 2008-03-18 05:29:35 +0100 (Di, 18 Mär 2008) | 2 lines
Block the "socket.ssl() is deprecated" warning from test_socket_ssl.
........
r61495 | jeffrey.yasskin | 2008-03-18 05:56:06 +0100 (Di, 18 Mär 2008) | 4 lines
Speed test_thread up from 51.328s to 0.081s by reducing its sleep times. We
still sleep at all to make it likely that all threads are active at the same
time.
........
r61496 | jeffrey.yasskin | 2008-03-18 06:12:41 +0100 (Di, 18 Mär 2008) | 4 lines
Speed up test_dict by about 10x by only checking selected dict literal sizes,
instead of every integer from 0 to 400. Exhaustive testing wastes time without
providing enough more assurance that the code is correct.
........
r61498 | neal.norwitz | 2008-03-18 06:20:29 +0100 (Di, 18 Mär 2008) | 1 line
Try increasing the timeout to reduce the flakiness of this test.
........
r61503 | brett.cannon | 2008-03-18 06:43:04 +0100 (Di, 18 Mär 2008) | 2 lines
Improve the error message for a test that failed on the S-390 Debian buildbot.
........
r61504 | jeffrey.yasskin | 2008-03-18 06:45:40 +0100 (Di, 18 Mär 2008) | 3 lines
Add a -S/--slow flag to regrtest to have it print the 10 slowest tests with
their times.
........
r61507 | neal.norwitz | 2008-03-18 07:03:46 +0100 (Di, 18 Mär 2008) | 1 line
Add some info to the failure messages
........
r61509 | trent.nelson | 2008-03-18 08:02:12 +0100 (Di, 18 Mär 2008) | 1 line
Issue 2286: bump up the stack size of the 64-bit debug python_d.exe to 2100000. The default value of 200000 causes a stack overflow at 1965 iterations of r_object() in marshal.c, 35 iterations before the 2000 limit enforced by MAX_MARSHAL_STACK_DEPTH.
........
r61510 | trent.nelson | 2008-03-18 08:32:47 +0100 (Di, 18 Mär 2008) | 5 lines
The behaviour of winsound.Beep() seems to differ between different versions of Windows when there's either:
a) no sound card entirely
b) legacy beep driver has been disabled
c) the legacy beep driver has been uninstalled
Sometimes RuntimeErrors are raised, sometimes they're not. If _have_soundcard() returns False, don't expect winsound.Beep() to raise a RuntimeError, as this clearly isn't the case, as demonstrated by the various Win32 XP buildbots.
........
r61515 | martin.v.loewis | 2008-03-18 13:20:15 +0100 (Di, 18 Mär 2008) | 2 lines
norwitz-amd64 (gentoo) has EREMOTEIO.
........
r61516 | martin.v.loewis | 2008-03-18 13:45:37 +0100 (Di, 18 Mär 2008) | 2 lines
Add more Linux error codes.
........
r61517 | martin.v.loewis | 2008-03-18 14:05:03 +0100 (Di, 18 Mär 2008) | 2 lines
Add WSA errors.
........
r61518 | martin.v.loewis | 2008-03-18 14:16:05 +0100 (Di, 18 Mär 2008) | 2 lines
Note that the stderr output of the test is intentional.
........
| author | Christian Heimes <christian@cheimes.de> |
|---|---|
| date | Tue, 18 Mar 2008 15:15:01 +0000 |
| parents | 42b3b117391d |
| children | 3d9ad27d930b |
| rev | line source |
|---|---|
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
1 /* implements the string, long, and float formatters. that is, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
2 string.__format__, etc. */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
3 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
4 /* Before including this, you must include either: |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
5 stringlib/unicodedefs.h |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
6 stringlib/stringdefs.h |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
7 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
8 Also, you should define the names: |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
9 FORMAT_STRING |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
10 FORMAT_LONG |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
11 FORMAT_FLOAT |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
12 to be whatever you want the public names of these functions to |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
13 be. These are the only non-static functions defined here. |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
14 */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
15 |
|
42980
9df911d654af
Added conditional compilation for '()', which was an allowed sign code in a
Eric Smith <eric@trueblade.com>
parents:
42929
diff
changeset
|
16 #define ALLOW_PARENS_FOR_SIGN 0 |
|
9df911d654af
Added conditional compilation for '()', which was an allowed sign code in a
Eric Smith <eric@trueblade.com>
parents:
42929
diff
changeset
|
17 |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
18 /* |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
19 get_integer consumes 0 or more decimal digit characters from an |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
20 input string, updates *result with the corresponding positive |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
21 integer, and returns the number of digits consumed. |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
22 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
23 returns -1 on error. |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
24 */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
25 static int |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
26 get_integer(STRINGLIB_CHAR **ptr, STRINGLIB_CHAR *end, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
27 Py_ssize_t *result) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
28 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
29 Py_ssize_t accumulator, digitval, oldaccumulator; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
30 int numdigits; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
31 accumulator = numdigits = 0; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
32 for (;;(*ptr)++, numdigits++) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
33 if (*ptr >= end) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
34 break; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
35 digitval = STRINGLIB_TODECIMAL(**ptr); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
36 if (digitval < 0) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
37 break; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
38 /* |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
39 This trick was copied from old Unicode format code. It's cute, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
40 but would really suck on an old machine with a slow divide |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
41 implementation. Fortunately, in the normal case we do not |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
42 expect too many digits. |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
43 */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
44 oldaccumulator = accumulator; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
45 accumulator *= 10; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
46 if ((accumulator+10)/10 != oldaccumulator+1) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
47 PyErr_Format(PyExc_ValueError, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
48 "Too many decimal digits in format string"); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
49 return -1; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
50 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
51 accumulator += digitval; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
52 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
53 *result = accumulator; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
54 return numdigits; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
55 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
56 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
57 /************************************************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
58 /*********** standard format specifier parsing **************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
59 /************************************************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
60 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
61 /* returns true if this character is a specifier alignment token */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
62 Py_LOCAL_INLINE(int) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
63 is_alignment_token(STRINGLIB_CHAR c) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
64 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
65 switch (c) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
66 case '<': case '>': case '=': case '^': |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
67 return 1; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
68 default: |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
69 return 0; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
70 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
71 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
72 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
73 /* returns true if this character is a sign element */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
74 Py_LOCAL_INLINE(int) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
75 is_sign_element(STRINGLIB_CHAR c) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
76 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
77 switch (c) { |
|
42980
9df911d654af
Added conditional compilation for '()', which was an allowed sign code in a
Eric Smith <eric@trueblade.com>
parents:
42929
diff
changeset
|
78 case ' ': case '+': case '-': |
|
42981
0f82fc42fd68
Corrected missed #if in r57652.
Eric Smith <eric@trueblade.com>
parents:
42980
diff
changeset
|
79 #if ALLOW_PARENS_FOR_SIGN |
|
42980
9df911d654af
Added conditional compilation for '()', which was an allowed sign code in a
Eric Smith <eric@trueblade.com>
parents:
42929
diff
changeset
|
80 case '(': |
|
42981
0f82fc42fd68
Corrected missed #if in r57652.
Eric Smith <eric@trueblade.com>
parents:
42980
diff
changeset
|
81 #endif |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
82 return 1; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
83 default: |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
84 return 0; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
85 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
86 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
87 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
88 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
89 typedef struct { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
90 STRINGLIB_CHAR fill_char; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
91 STRINGLIB_CHAR align; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
92 STRINGLIB_CHAR sign; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
93 Py_ssize_t width; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
94 Py_ssize_t precision; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
95 STRINGLIB_CHAR type; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
96 } InternalFormatSpec; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
97 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
98 /* |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
99 ptr points to the start of the format_spec, end points just past its end. |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
100 fills in format with the parsed information. |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
101 returns 1 on success, 0 on failure. |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
102 if failure, sets the exception |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
103 */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
104 static int |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
105 parse_internal_render_format_spec(PyObject *format_spec, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
106 InternalFormatSpec *format, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
107 char default_type) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
108 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
109 STRINGLIB_CHAR *ptr = STRINGLIB_STR(format_spec); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
110 STRINGLIB_CHAR *end = ptr + STRINGLIB_LEN(format_spec); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
111 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
112 /* end-ptr is used throughout this code to specify the length of |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
113 the input string */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
114 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
115 Py_ssize_t specified_width; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
116 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
117 format->fill_char = '\0'; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
118 format->align = '\0'; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
119 format->sign = '\0'; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
120 format->width = -1; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
121 format->precision = -1; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
122 format->type = default_type; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
123 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
124 /* If the second char is an alignment token, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
125 then parse the fill char */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
126 if (end-ptr >= 2 && is_alignment_token(ptr[1])) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
127 format->align = ptr[1]; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
128 format->fill_char = ptr[0]; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
129 ptr += 2; |
|
42929
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
130 } |
|
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
131 else if (end-ptr >= 1 && is_alignment_token(ptr[0])) { |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
132 format->align = ptr[0]; |
|
45435
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45382
diff
changeset
|
133 ++ptr; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
134 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
135 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
136 /* Parse the various sign options */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
137 if (end-ptr >= 1 && is_sign_element(ptr[0])) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
138 format->sign = ptr[0]; |
|
45435
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45382
diff
changeset
|
139 ++ptr; |
|
42980
9df911d654af
Added conditional compilation for '()', which was an allowed sign code in a
Eric Smith <eric@trueblade.com>
parents:
42929
diff
changeset
|
140 #if ALLOW_PARENS_FOR_SIGN |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
141 if (end-ptr >= 1 && ptr[0] == ')') { |
|
45435
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45382
diff
changeset
|
142 ++ptr; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
143 } |
|
42980
9df911d654af
Added conditional compilation for '()', which was an allowed sign code in a
Eric Smith <eric@trueblade.com>
parents:
42929
diff
changeset
|
144 #endif |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
145 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
146 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
147 /* The special case for 0-padding (backwards compat) */ |
| 43097 | 148 if (format->fill_char == '\0' && end-ptr >= 1 && ptr[0] == '0') { |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
149 format->fill_char = '0'; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
150 if (format->align == '\0') { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
151 format->align = '='; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
152 } |
|
45435
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45382
diff
changeset
|
153 ++ptr; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
154 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
155 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
156 /* XXX add error checking */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
157 specified_width = get_integer(&ptr, end, &format->width); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
158 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
159 /* if specified_width is 0, we didn't consume any characters for |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
160 the width. in that case, reset the width to -1, because |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
161 get_integer() will have set it to zero */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
162 if (specified_width == 0) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
163 format->width = -1; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
164 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
165 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
166 /* Parse field precision */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
167 if (end-ptr && ptr[0] == '.') { |
|
45435
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45382
diff
changeset
|
168 ++ptr; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
169 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
170 /* XXX add error checking */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
171 specified_width = get_integer(&ptr, end, &format->precision); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
172 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
173 /* not having a precision after a dot is an error */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
174 if (specified_width == 0) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
175 PyErr_Format(PyExc_ValueError, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
176 "Format specifier missing precision"); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
177 return 0; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
178 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
179 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
180 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
181 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
182 /* Finally, parse the type field */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
183 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
184 if (end-ptr > 1) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
185 /* invalid conversion spec */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
186 PyErr_Format(PyExc_ValueError, "Invalid conversion specification"); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
187 return 0; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
188 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
189 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
190 if (end-ptr == 1) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
191 format->type = ptr[0]; |
|
45435
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45382
diff
changeset
|
192 ++ptr; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
193 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
194 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
195 return 1; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
196 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
197 |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
198 #if defined FORMAT_FLOAT || defined FORMAT_LONG |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
199 /************************************************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
200 /*********** common routines for numeric formatting *********************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
201 /************************************************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
202 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
203 /* describes the layout for an integer, see the comment in |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
204 _calc_integer_widths() for details */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
205 typedef struct { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
206 Py_ssize_t n_lpadding; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
207 Py_ssize_t n_spadding; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
208 Py_ssize_t n_rpadding; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
209 char lsign; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
210 Py_ssize_t n_lsign; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
211 char rsign; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
212 Py_ssize_t n_rsign; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
213 Py_ssize_t n_total; /* just a convenience, it's derivable from the |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
214 other fields */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
215 } NumberFieldWidths; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
216 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
217 /* not all fields of format are used. for example, precision is |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
218 unused. should this take discrete params in order to be more clear |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
219 about what it does? or is passing a single format parameter easier |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
220 and more efficient enough to justify a little obfuscation? */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
221 static void |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
222 calc_number_widths(NumberFieldWidths *r, STRINGLIB_CHAR actual_sign, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
223 Py_ssize_t n_digits, const InternalFormatSpec *format) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
224 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
225 r->n_lpadding = 0; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
226 r->n_spadding = 0; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
227 r->n_rpadding = 0; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
228 r->lsign = '\0'; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
229 r->n_lsign = 0; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
230 r->rsign = '\0'; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
231 r->n_rsign = 0; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
232 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
233 /* the output will look like: |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
234 | | |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
235 | <lpadding> <lsign> <spadding> <digits> <rsign> <rpadding> | |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
236 | | |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
237 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
238 lsign and rsign are computed from format->sign and the actual |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
239 sign of the number |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
240 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
241 digits is already known |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
242 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
243 the total width is either given, or computed from the |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
244 actual digits |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
245 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
246 only one of lpadding, spadding, and rpadding can be non-zero, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
247 and it's calculated from the width and other fields |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
248 */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
249 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
250 /* compute the various parts we're going to write */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
251 if (format->sign == '+') { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
252 /* always put a + or - */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
253 r->n_lsign = 1; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
254 r->lsign = (actual_sign == '-' ? '-' : '+'); |
|
42929
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
255 } |
|
42980
9df911d654af
Added conditional compilation for '()', which was an allowed sign code in a
Eric Smith <eric@trueblade.com>
parents:
42929
diff
changeset
|
256 #if ALLOW_PARENS_FOR_SIGN |
|
42929
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
257 else if (format->sign == '(') { |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
258 if (actual_sign == '-') { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
259 r->n_lsign = 1; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
260 r->lsign = '('; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
261 r->n_rsign = 1; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
262 r->rsign = ')'; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
263 } |
|
42929
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
264 } |
|
42980
9df911d654af
Added conditional compilation for '()', which was an allowed sign code in a
Eric Smith <eric@trueblade.com>
parents:
42929
diff
changeset
|
265 #endif |
|
42929
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
266 else if (format->sign == ' ') { |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
267 r->n_lsign = 1; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
268 r->lsign = (actual_sign == '-' ? '-' : ' '); |
|
42929
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
269 } |
|
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
270 else { |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
271 /* non specified, or the default (-) */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
272 if (actual_sign == '-') { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
273 r->n_lsign = 1; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
274 r->lsign = '-'; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
275 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
276 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
277 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
278 /* now the number of padding characters */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
279 if (format->width == -1) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
280 /* no padding at all, nothing to do */ |
|
42929
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
281 } |
|
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
282 else { |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
283 /* see if any padding is needed */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
284 if (r->n_lsign + n_digits + r->n_rsign >= format->width) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
285 /* no padding needed, we're already bigger than the |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
286 requested width */ |
|
42929
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
287 } |
|
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
288 else { |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
289 /* determine which of left, space, or right padding is |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
290 needed */ |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
291 Py_ssize_t padding = format->width - |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
292 (r->n_lsign + n_digits + r->n_rsign); |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
293 if (format->align == '<') |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
294 r->n_rpadding = padding; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
295 else if (format->align == '>') |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
296 r->n_lpadding = padding; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
297 else if (format->align == '^') { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
298 r->n_lpadding = padding / 2; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
299 r->n_rpadding = padding - r->n_lpadding; |
|
42929
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
300 } |
| 43097 | 301 else if (format->align == '=') |
| 302 r->n_spadding = padding; | |
|
42929
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
303 else |
| 43097 | 304 r->n_lpadding = padding; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
305 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
306 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
307 r->n_total = r->n_lpadding + r->n_lsign + r->n_spadding + |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
308 n_digits + r->n_rsign + r->n_rpadding; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
309 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
310 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
311 /* fill in the non-digit parts of a numbers's string representation, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
312 as determined in _calc_integer_widths(). returns the pointer to |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
313 where the digits go. */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
314 static STRINGLIB_CHAR * |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
315 fill_number(STRINGLIB_CHAR *p_buf, const NumberFieldWidths *spec, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
316 Py_ssize_t n_digits, STRINGLIB_CHAR fill_char) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
317 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
318 STRINGLIB_CHAR* p_digits; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
319 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
320 if (spec->n_lpadding) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
321 STRINGLIB_FILL(p_buf, fill_char, spec->n_lpadding); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
322 p_buf += spec->n_lpadding; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
323 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
324 if (spec->n_lsign == 1) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
325 *p_buf++ = spec->lsign; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
326 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
327 if (spec->n_spadding) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
328 STRINGLIB_FILL(p_buf, fill_char, spec->n_spadding); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
329 p_buf += spec->n_spadding; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
330 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
331 p_digits = p_buf; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
332 p_buf += n_digits; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
333 if (spec->n_rsign == 1) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
334 *p_buf++ = spec->rsign; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
335 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
336 if (spec->n_rpadding) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
337 STRINGLIB_FILL(p_buf, fill_char, spec->n_rpadding); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
338 p_buf += spec->n_rpadding; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
339 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
340 return p_digits; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
341 } |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
342 #endif /* FORMAT_FLOAT || FORMAT_LONG */ |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
343 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
344 /************************************************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
345 /*********** string formatting ******************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
346 /************************************************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
347 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
348 static PyObject * |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
349 format_string_internal(PyObject *value, const InternalFormatSpec *format) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
350 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
351 Py_ssize_t width; /* total field width */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
352 Py_ssize_t lpad; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
353 STRINGLIB_CHAR *dst; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
354 STRINGLIB_CHAR *src = STRINGLIB_STR(value); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
355 Py_ssize_t len = STRINGLIB_LEN(value); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
356 PyObject *result = NULL; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
357 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
358 /* sign is not allowed on strings */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
359 if (format->sign != '\0') { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
360 PyErr_SetString(PyExc_ValueError, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
361 "Sign not allowed in string format specifier"); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
362 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
363 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
364 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
365 /* '=' alignment not allowed on strings */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
366 if (format->align == '=') { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
367 PyErr_SetString(PyExc_ValueError, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
368 "'=' alignment not allowed " |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
369 "in string format specifier"); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
370 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
371 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
372 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
373 /* if precision is specified, output no more that format.precision |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
374 characters */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
375 if (format->precision >= 0 && len >= format->precision) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
376 len = format->precision; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
377 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
378 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
379 if (format->width >= 0) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
380 width = format->width; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
381 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
382 /* but use at least len characters */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
383 if (len > width) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
384 width = len; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
385 } |
|
42929
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
386 } |
|
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
387 else { |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
388 /* not specified, use all of the chars and no more */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
389 width = len; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
390 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
391 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
392 /* allocate the resulting string */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
393 result = STRINGLIB_NEW(NULL, width); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
394 if (result == NULL) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
395 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
396 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
397 /* now write into that space */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
398 dst = STRINGLIB_STR(result); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
399 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
400 /* figure out how much leading space we need, based on the |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
401 aligning */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
402 if (format->align == '>') |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
403 lpad = width - len; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
404 else if (format->align == '^') |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
405 lpad = (width - len) / 2; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
406 else |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
407 lpad = 0; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
408 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
409 /* if right aligning, increment the destination allow space on the |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
410 left */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
411 memcpy(dst + lpad, src, len * sizeof(STRINGLIB_CHAR)); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
412 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
413 /* do any padding */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
414 if (width > len) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
415 STRINGLIB_CHAR fill_char = format->fill_char; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
416 if (fill_char == '\0') { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
417 /* use the default, if not specified */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
418 fill_char = ' '; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
419 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
420 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
421 /* pad on left */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
422 if (lpad) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
423 STRINGLIB_FILL(dst, fill_char, lpad); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
424 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
425 /* pad on right */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
426 if (width - len - lpad) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
427 STRINGLIB_FILL(dst + len + lpad, fill_char, width - len - lpad); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
428 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
429 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
430 done: |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
431 return result; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
432 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
433 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
434 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
435 /************************************************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
436 /*********** long formatting ********************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
437 /************************************************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
438 |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
439 #if defined FORMAT_LONG || defined FORMAT_INT |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
440 typedef PyObject* |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
441 (*IntOrLongToString)(PyObject *value, int base); |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
442 |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
443 static PyObject * |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
444 format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format, |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
445 IntOrLongToString tostring) |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
446 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
447 PyObject *result = NULL; |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
448 PyObject *tmp = NULL; |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
449 STRINGLIB_CHAR *pnumeric_chars; |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
450 STRINGLIB_CHAR numeric_char; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
451 STRINGLIB_CHAR sign = '\0'; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
452 STRINGLIB_CHAR *p; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
453 Py_ssize_t n_digits; /* count of digits need from the computed |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
454 string */ |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
455 Py_ssize_t n_leading_chars; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
456 NumberFieldWidths spec; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
457 long x; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
458 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
459 /* no precision allowed on integers */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
460 if (format->precision != -1) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
461 PyErr_SetString(PyExc_ValueError, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
462 "Precision not allowed in integer format specifier"); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
463 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
464 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
465 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
466 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
467 /* special case for character formatting */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
468 if (format->type == 'c') { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
469 /* error to specify a sign */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
470 if (format->sign != '\0') { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
471 PyErr_SetString(PyExc_ValueError, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
472 "Sign not allowed with integer" |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
473 " format specifier 'c'"); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
474 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
475 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
476 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
477 /* taken from unicodeobject.c formatchar() */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
478 /* Integer input truncated to a character */ |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
479 /* XXX: won't work for int */ |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
43172
diff
changeset
|
480 x = PyLong_AsLong(value); |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
481 if (x == -1 && PyErr_Occurred()) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
482 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
483 #ifdef Py_UNICODE_WIDE |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
484 if (x < 0 || x > 0x10ffff) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
485 PyErr_SetString(PyExc_OverflowError, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
486 "%c arg not in range(0x110000) " |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
487 "(wide Python build)"); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
488 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
489 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
490 #else |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
491 if (x < 0 || x > 0xffff) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
492 PyErr_SetString(PyExc_OverflowError, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
493 "%c arg not in range(0x10000) " |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
494 "(narrow Python build)"); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
495 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
496 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
497 #endif |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
498 numeric_char = (STRINGLIB_CHAR)x; |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
499 pnumeric_chars = &numeric_char; |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
500 n_digits = 1; |
|
42929
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
501 } |
|
11ec4097fafe
Code layout changes for PEP 7 compliance.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
502 else { |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
503 int base; |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
504 int leading_chars_to_skip; /* Number of characters added by |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
505 PyNumber_ToBase that we want to |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
506 skip over. */ |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
507 |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
508 /* Compute the base and how many characters will be added by |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
509 PyNumber_ToBase */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
510 switch (format->type) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
511 case 'b': |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
512 base = 2; |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
513 leading_chars_to_skip = 2; /* 0b */ |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
514 break; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
515 case 'o': |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
516 base = 8; |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
517 leading_chars_to_skip = 2; /* 0o */ |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
518 break; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
519 case 'x': |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
520 case 'X': |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
521 base = 16; |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
522 leading_chars_to_skip = 2; /* 0x */ |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
523 break; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
524 default: /* shouldn't be needed, but stops a compiler warning */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
525 case 'd': |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
526 base = 10; |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
527 leading_chars_to_skip = 0; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
528 break; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
529 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
530 |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
531 /* Do the hard part, converting to a string in a given base */ |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
532 tmp = tostring(value, base); |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
533 if (tmp == NULL) |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
534 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
535 |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
536 pnumeric_chars = STRINGLIB_STR(tmp); |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
537 n_digits = STRINGLIB_LEN(tmp); |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
538 |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
539 /* Remember not to modify what pnumeric_chars points to. it |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
540 might be interned. Only modify it after we copy it into a |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
541 newly allocated output buffer. */ |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
542 |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
543 /* Is a sign character present in the output? If so, remember it |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
544 and skip it */ |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
545 sign = pnumeric_chars[0]; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
546 if (sign == '-') { |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
547 ++leading_chars_to_skip; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
548 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
549 |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
550 /* Skip over the leading chars (0x, 0b, etc.) */ |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
551 n_digits -= leading_chars_to_skip; |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
552 pnumeric_chars += leading_chars_to_skip; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
553 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
554 |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
555 /* Calculate the widths of the various leading and trailing parts */ |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
556 calc_number_widths(&spec, sign, n_digits, format); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
557 |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
558 /* Allocate a new string to hold the result */ |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
559 result = STRINGLIB_NEW(NULL, spec.n_total); |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
560 if (!result) |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
561 goto done; |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
562 p = STRINGLIB_STR(result); |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
563 |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
564 /* Fill in the digit parts */ |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
565 n_leading_chars = spec.n_lpadding + spec.n_lsign + spec.n_spadding; |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
566 memmove(p + n_leading_chars, |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
567 pnumeric_chars, |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
568 n_digits * sizeof(STRINGLIB_CHAR)); |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
569 |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
570 /* if X, convert to uppercase */ |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
571 if (format->type == 'X') { |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
572 Py_ssize_t t; |
|
45435
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45382
diff
changeset
|
573 for (t = 0; t < n_digits; ++t) |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
574 p[t + n_leading_chars] = STRINGLIB_TOUPPER(p[t + n_leading_chars]); |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
575 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
576 |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
577 /* Fill in the non-digit parts */ |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
578 fill_number(p, &spec, n_digits, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
579 format->fill_char == '\0' ? ' ' : format->fill_char); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
580 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
581 done: |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
582 Py_XDECREF(tmp); |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
583 return result; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
584 } |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
585 #endif /* defined FORMAT_LONG || defined FORMAT_INT */ |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
586 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
587 /************************************************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
588 /*********** float formatting *******************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
589 /************************************************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
590 |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
591 #ifdef FORMAT_FLOAT |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
592 #if STRINGLIB_IS_UNICODE |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
593 /* taken from unicodeobject.c */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
594 static Py_ssize_t |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
595 strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
596 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
597 register Py_ssize_t i; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
598 Py_ssize_t len = strlen(charbuffer); |
|
45435
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45382
diff
changeset
|
599 for (i = len - 1; i >= 0; --i) |
| 43097 | 600 buffer[i] = (Py_UNICODE) charbuffer[i]; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
601 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
602 return len; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
603 } |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
604 #endif |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
605 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
606 /* see FORMATBUFLEN in unicodeobject.c */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
607 #define FLOAT_FORMATBUFLEN 120 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
608 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
609 /* much of this is taken from unicodeobject.c */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
610 static PyObject * |
|
45435
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45382
diff
changeset
|
611 format_float_internal(PyObject *value, |
|
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45382
diff
changeset
|
612 const InternalFormatSpec *format) |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
613 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
614 /* fmt = '%.' + `prec` + `type` + '%%' |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
615 worst case length = 2 + 10 (len of INT_MAX) + 1 + 2 = 15 (use 20)*/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
616 char fmt[20]; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
617 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
618 /* taken from unicodeobject.c */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
619 /* Worst case length calc to ensure no buffer overrun: |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
620 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
621 'g' formats: |
| 43097 | 622 fmt = %#.<prec>g |
| 623 buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp | |
| 624 for any double rep.) | |
| 625 len = 1 + prec + 1 + 2 + 5 = 9 + prec | |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
626 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
627 'f' formats: |
| 43097 | 628 buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 629 len = 1 + 50 + 1 + prec = 52 + prec | |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
630 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
631 If prec=0 the effective precision is 1 (the leading digit is |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
632 always given), therefore increase the length by one. |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
633 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
634 */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
635 char charbuf[FLOAT_FORMATBUFLEN]; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
636 Py_ssize_t n_digits; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
637 double x; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
638 Py_ssize_t precision = format->precision; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
639 PyObject *result = NULL; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
640 STRINGLIB_CHAR sign; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
641 char* trailing = ""; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
642 STRINGLIB_CHAR *p; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
643 NumberFieldWidths spec; |
|
45435
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45382
diff
changeset
|
644 STRINGLIB_CHAR type = format->type; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
645 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
646 #if STRINGLIB_IS_UNICODE |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
647 Py_UNICODE unicodebuf[FLOAT_FORMATBUFLEN]; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
648 #endif |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
649 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
650 /* first, do the conversion as 8-bit chars, using the platform's |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
651 snprintf. then, if needed, convert to unicode. */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
652 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
653 /* 'F' is the same as 'f', per the PEP */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
654 if (type == 'F') |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
655 type = 'f'; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
656 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
657 x = PyFloat_AsDouble(value); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
658 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
659 if (x == -1.0 && PyErr_Occurred()) |
| 43097 | 660 goto done; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
661 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
662 if (type == '%') { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
663 type = 'f'; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
664 x *= 100; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
665 trailing = "%"; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
666 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
667 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
668 if (precision < 0) |
| 43097 | 669 precision = 6; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
670 if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 43097 | 671 type = 'g'; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
672 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
673 /* cast "type", because if we're in unicode we need to pass a |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
674 8-bit char. this is safe, because we've restricted what "type" |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
675 can be */ |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
676 PyOS_snprintf(fmt, sizeof(fmt), "%%.%" PY_FORMAT_SIZE_T "d%c", precision, |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
677 (char)type); |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
678 |
|
45435
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45382
diff
changeset
|
679 /* do the actual formatting */ |
|
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45382
diff
changeset
|
680 PyOS_ascii_formatd(charbuf, sizeof(charbuf), fmt, x); |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
681 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
682 /* adding trailing to fmt with PyOS_snprintf doesn't work, not |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
683 sure why. we'll just concatentate it here, no harm done. we |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
684 know we can't have a buffer overflow from the fmt size |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
685 analysis */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
686 strcat(charbuf, trailing); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
687 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
688 /* rather than duplicate the code for snprintf for both unicode |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
689 and 8 bit strings, we just use the 8 bit version and then |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
690 convert to unicode in a separate code path. that's probably |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
691 the lesser of 2 evils. */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
692 #if STRINGLIB_IS_UNICODE |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
693 n_digits = strtounicode(unicodebuf, charbuf); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
694 p = unicodebuf; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
695 #else |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
696 /* compute the length. I believe this is done because the return |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
697 value from snprintf above is unreliable */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
698 n_digits = strlen(charbuf); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
699 p = charbuf; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
700 #endif |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
701 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
702 /* is a sign character present in the output? if so, remember it |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
703 and skip it */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
704 sign = p[0]; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
705 if (sign == '-') { |
|
45435
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45382
diff
changeset
|
706 ++p; |
|
42b3b117391d
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45382
diff
changeset
|
707 --n_digits; |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
708 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
709 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
710 calc_number_widths(&spec, sign, n_digits, format); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
711 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
712 /* allocate a string with enough space */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
713 result = STRINGLIB_NEW(NULL, spec.n_total); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
714 if (result == NULL) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
715 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
716 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
717 /* fill in the non-digit parts */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
718 fill_number(STRINGLIB_STR(result), &spec, n_digits, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
719 format->fill_char == '\0' ? ' ' : format->fill_char); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
720 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
721 /* fill in the digit parts */ |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
722 memmove(STRINGLIB_STR(result) + |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
723 (spec.n_lpadding + spec.n_lsign + spec.n_spadding), |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
724 p, |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
725 n_digits * sizeof(STRINGLIB_CHAR)); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
726 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
727 done: |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
728 return result; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
729 } |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
730 #endif /* FORMAT_FLOAT */ |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
731 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
732 /************************************************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
733 /*********** built in formatters ****************************************/ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
734 /************************************************************************/ |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
735 #ifdef FORMAT_STRING |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
736 PyObject * |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
737 FORMAT_STRING(PyObject* value, PyObject* args) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
738 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
739 PyObject *format_spec; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
740 PyObject *result = NULL; |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
741 #if PY_VERSION_HEX < 0x03000000 |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
742 PyObject *tmp = NULL; |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
743 #endif |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
744 InternalFormatSpec format; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
745 |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
746 /* If 2.x, we accept either str or unicode, and try to convert it |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
747 to the right type. In 3.x, we insist on only unicode */ |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
748 #if PY_VERSION_HEX >= 0x03000000 |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
749 if (!PyArg_ParseTuple(args, STRINGLIB_PARSE_CODE ":__format__", |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
750 &format_spec)) |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
751 goto done; |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
752 #else |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
753 /* If 2.x, convert format_spec to the same type as value */ |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
754 /* This is to allow things like u''.format('') */ |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
755 if (!PyArg_ParseTuple(args, "O:__format__", &format_spec)) |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
756 goto done; |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
757 if (!(PyString_Check(format_spec) || PyUnicode_Check(format_spec))) { |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
758 PyErr_Format(PyExc_TypeError, "__format__ arg must be str " |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
759 "or unicode, not %s", Py_TYPE(format_spec)->tp_name); |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
760 goto done; |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
761 } |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
762 tmp = STRINGLIB_TOSTR(format_spec); |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
763 if (tmp == NULL) |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
764 goto done; |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
765 format_spec = tmp; |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
766 #endif |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
767 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
768 /* check for the special case of zero length format spec, make |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
769 it equivalent to str(value) */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
770 if (STRINGLIB_LEN(format_spec) == 0) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
771 result = STRINGLIB_TOSTR(value); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
772 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
773 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
774 |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
775 |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
776 /* parse the format_spec */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
777 if (!parse_internal_render_format_spec(format_spec, &format, 's')) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
778 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
779 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
780 /* type conversion? */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
781 switch (format.type) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
782 case 's': |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
783 /* no type conversion needed, already a string. do the formatting */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
784 result = format_string_internal(value, &format); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
785 break; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
786 default: |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
787 /* unknown */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
788 PyErr_Format(PyExc_ValueError, "Unknown conversion type %c", |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
789 format.type); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
790 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
791 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
792 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
793 done: |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
794 #if PY_VERSION_HEX < 0x03000000 |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
795 Py_XDECREF(tmp); |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
796 #endif |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
797 return result; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
798 } |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
799 #endif /* FORMAT_STRING */ |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
800 |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
801 #if defined FORMAT_LONG || defined FORMAT_INT |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
802 static PyObject* |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
803 format_int_or_long(PyObject* value, PyObject* args, IntOrLongToString tostring) |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
804 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
805 PyObject *format_spec; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
806 PyObject *result = NULL; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
807 PyObject *tmp = NULL; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
808 InternalFormatSpec format; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
809 |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
810 if (!PyArg_ParseTuple(args, STRINGLIB_PARSE_CODE ":__format__", |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
811 &format_spec)) |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
812 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
813 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
814 /* check for the special case of zero length format spec, make |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
815 it equivalent to str(value) */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
816 if (STRINGLIB_LEN(format_spec) == 0) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
817 result = STRINGLIB_TOSTR(value); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
818 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
819 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
820 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
821 /* parse the format_spec */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
822 if (!parse_internal_render_format_spec(format_spec, &format, 'd')) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
823 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
824 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
825 /* type conversion? */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
826 switch (format.type) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
827 case 'b': |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
828 case 'c': |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
829 case 'd': |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
830 case 'o': |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
831 case 'x': |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
832 case 'X': |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
833 /* no type conversion needed, already an int (or long). do |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
834 the formatting */ |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
835 result = format_int_or_long_internal(value, &format, tostring); |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
836 break; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
837 |
|
45032
a28d0b14b77d
Partially revert r60376: restore ability for ints to be automatically converted to floats, if a float type specifier is given to an int. PEP 3101 should be clarified on this point.Also, remove unused local variables left over from r60376.
Eric Smith <eric@trueblade.com>
parents:
45023
diff
changeset
|
838 case 'e': |
|
a28d0b14b77d
Partially revert r60376: restore ability for ints to be automatically converted to floats, if a float type specifier is given to an int. PEP 3101 should be clarified on this point.Also, remove unused local variables left over from r60376.
Eric Smith <eric@trueblade.com>
parents:
45023
diff
changeset
|
839 case 'E': |
|
a28d0b14b77d
Partially revert r60376: restore ability for ints to be automatically converted to floats, if a float type specifier is given to an int. PEP 3101 should be clarified on this point.Also, remove unused local variables left over from r60376.
Eric Smith <eric@trueblade.com>
parents:
45023
diff
changeset
|
840 case 'f': |
|
a28d0b14b77d
Partially revert r60376: restore ability for ints to be automatically converted to floats, if a float type specifier is given to an int. PEP 3101 should be clarified on this point.Also, remove unused local variables left over from r60376.
Eric Smith <eric@trueblade.com>
parents:
45023
diff
changeset
|
841 case 'F': |
|
a28d0b14b77d
Partially revert r60376: restore ability for ints to be automatically converted to floats, if a float type specifier is given to an int. PEP 3101 should be clarified on this point.Also, remove unused local variables left over from r60376.
Eric Smith <eric@trueblade.com>
parents:
45023
diff
changeset
|
842 case 'g': |
|
a28d0b14b77d
Partially revert r60376: restore ability for ints to be automatically converted to floats, if a float type specifier is given to an int. PEP 3101 should be clarified on this point.Also, remove unused local variables left over from r60376.
Eric Smith <eric@trueblade.com>
parents:
45023
diff
changeset
|
843 case 'G': |
|
a28d0b14b77d
Partially revert r60376: restore ability for ints to be automatically converted to floats, if a float type specifier is given to an int. PEP 3101 should be clarified on this point.Also, remove unused local variables left over from r60376.
Eric Smith <eric@trueblade.com>
parents:
45023
diff
changeset
|
844 case 'n': |
|
a28d0b14b77d
Partially revert r60376: restore ability for ints to be automatically converted to floats, if a float type specifier is given to an int. PEP 3101 should be clarified on this point.Also, remove unused local variables left over from r60376.
Eric Smith <eric@trueblade.com>
parents:
45023
diff
changeset
|
845 case '%': |
|
a28d0b14b77d
Partially revert r60376: restore ability for ints to be automatically converted to floats, if a float type specifier is given to an int. PEP 3101 should be clarified on this point.Also, remove unused local variables left over from r60376.
Eric Smith <eric@trueblade.com>
parents:
45023
diff
changeset
|
846 /* convert to float */ |
|
a28d0b14b77d
Partially revert r60376: restore ability for ints to be automatically converted to floats, if a float type specifier is given to an int. PEP 3101 should be clarified on this point.Also, remove unused local variables left over from r60376.
Eric Smith <eric@trueblade.com>
parents:
45023
diff
changeset
|
847 tmp = PyNumber_Float(value); |
|
a28d0b14b77d
Partially revert r60376: restore ability for ints to be automatically converted to floats, if a float type specifier is given to an int. PEP 3101 should be clarified on this point.Also, remove unused local variables left over from r60376.
Eric Smith <eric@trueblade.com>
parents:
45023
diff
changeset
|
848 if (tmp == NULL) |
|
a28d0b14b77d
Partially revert r60376: restore ability for ints to be automatically converted to floats, if a float type specifier is given to an int. PEP 3101 should be clarified on this point.Also, remove unused local variables left over from r60376.
Eric Smith <eric@trueblade.com>
parents:
45023
diff
changeset
|
849 goto done; |
|
a28d0b14b77d
Partially revert r60376: restore ability for ints to be automatically converted to floats, if a float type specifier is given to an int. PEP 3101 should be clarified on this point.Also, remove unused local variables left over from r60376.
Eric Smith <eric@trueblade.com>
parents:
45023
diff
changeset
|
850 result = format_float_internal(value, &format); |
|
a28d0b14b77d
Partially revert r60376: restore ability for ints to be automatically converted to floats, if a float type specifier is given to an int. PEP 3101 should be clarified on this point.Also, remove unused local variables left over from r60376.
Eric Smith <eric@trueblade.com>
parents:
45023
diff
changeset
|
851 break; |
|
a28d0b14b77d
Partially revert r60376: restore ability for ints to be automatically converted to floats, if a float type specifier is given to an int. PEP 3101 should be clarified on this point.Also, remove unused local variables left over from r60376.
Eric Smith <eric@trueblade.com>
parents:
45023
diff
changeset
|
852 |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
853 default: |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
854 /* unknown */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
855 PyErr_Format(PyExc_ValueError, "Unknown conversion type %c", |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
856 format.type); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
857 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
858 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
859 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
860 done: |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
861 Py_XDECREF(tmp); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
862 return result; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
863 } |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
864 #endif /* FORMAT_LONG || defined FORMAT_INT */ |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
865 |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
866 #ifdef FORMAT_LONG |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
867 /* Need to define long_format as a function that will convert a long |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
868 to a string. In 3.0, _PyLong_Format has the correct signature. In |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
869 2.x, we need to fudge a few parameters */ |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
870 #if PY_VERSION_HEX >= 0x03000000 |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
871 #define long_format _PyLong_Format |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
872 #else |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
873 static PyObject* |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
874 long_format(PyObject* value, int base) |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
875 { |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
876 /* Convert to base, don't add trailing 'L', and use the new octal |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
877 format. We already know this is a long object */ |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
878 assert(PyLong_Check(value)); |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
879 /* convert to base, don't add 'L', and use the new octal format */ |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
880 return _PyLong_Format(value, base, 0, 1); |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
881 } |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
882 #endif |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
883 |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
884 PyObject * |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
885 FORMAT_LONG(PyObject* value, PyObject* args) |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
886 { |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
887 return format_int_or_long(value, args, long_format); |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
888 } |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
889 #endif /* FORMAT_LONG */ |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
890 |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
891 #ifdef FORMAT_INT |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
892 /* this is only used for 2.x, not 3.0 */ |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
893 static PyObject* |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
894 int_format(PyObject* value, int base) |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
895 { |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
896 /* Convert to base, and use the new octal format. We already |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
897 know this is an int object */ |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
898 assert(PyInt_Check(value)); |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
899 return _PyInt_Format((PyIntObject*)value, base, 1); |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
900 } |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
901 |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
902 PyObject * |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
903 FORMAT_INT(PyObject* value, PyObject* args) |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
904 { |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
905 return format_int_or_long(value, args, int_format); |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
906 } |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
907 #endif /* FORMAT_INT */ |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
908 |
|
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
909 #ifdef FORMAT_FLOAT |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
910 PyObject * |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
911 FORMAT_FLOAT(PyObject *value, PyObject *args) |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
912 { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
913 PyObject *format_spec; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
914 PyObject *result = NULL; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
915 InternalFormatSpec format; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
916 |
|
43172
d842457e4b0e
Changed to use 'U' argument to PyArg_ParseTuple, instead of manually checking for unicode objects.
Eric Smith <eric@trueblade.com>
parents:
43097
diff
changeset
|
917 if (!PyArg_ParseTuple(args, STRINGLIB_PARSE_CODE ":__format__", &format_spec)) |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
918 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
919 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
920 /* check for the special case of zero length format spec, make |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
921 it equivalent to str(value) */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
922 if (STRINGLIB_LEN(format_spec) == 0) { |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
923 result = STRINGLIB_TOSTR(value); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
924 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
925 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
926 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
927 /* parse the format_spec */ |
|
45852
30c4e1ca8421
Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45435
diff
changeset
|
928 if (!parse_internal_render_format_spec(format_spec, &format, '\0')) |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
929 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
930 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
931 /* type conversion? */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
932 switch (format.type) { |
|
45852
30c4e1ca8421
Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45435
diff
changeset
|
933 case '\0': |
|
30c4e1ca8421
Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45435
diff
changeset
|
934 /* 'Z' means like 'g', but with at least one decimal. See |
|
30c4e1ca8421
Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45435
diff
changeset
|
935 PyOS_ascii_formatd */ |
|
30c4e1ca8421
Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45435
diff
changeset
|
936 format.type = 'Z'; |
|
30c4e1ca8421
Merged revisions 61431,61433-61436,61439,61444,61449-61450,61453,61458,61465,61468,61471-61474,61480,61483-61484,61488,61495-61496,61498,61503-61504,61507,61509-61510,61515-61518 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45435
diff
changeset
|
937 /* Deliberate fall through to the next case statement */ |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
938 case 'e': |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
939 case 'E': |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
940 case 'f': |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
941 case 'F': |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
942 case 'g': |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
943 case 'G': |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
944 case 'n': |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
945 case '%': |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
946 /* no conversion, already a float. do the formatting */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
947 result = format_float_internal(value, &format); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
948 break; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
949 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
950 default: |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
951 /* unknown */ |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
952 PyErr_Format(PyExc_ValueError, "Unknown conversion type %c", |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
953 format.type); |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
954 goto done; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
955 } |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
956 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
957 done: |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
958 return result; |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
diff
changeset
|
959 } |
|
45382
7af955843929
Fixes for shared 2.6 code that implements PEP 3101, advanced string
Eric Smith <eric@trueblade.com>
parents:
45032
diff
changeset
|
960 #endif /* FORMAT_FLOAT */ |
