annotate Objects/unicodeobject.c @ 55046:edcfe3b8f736 legacy-trunk

multiprocessing doesn't compile in Solaris because a typo
author Jesus Cea <jcea@jcea.es>
date Thu, 02 Jul 2009 14:30:18 +0000
parents 8888a6fe9dd7
children 38d6e4e51af0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
1 /*
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 Unicode implementation based on original code by Fredrik Lundh,
12646
17d1a3f1f085 Minimal change so I can add the rest of MAL's checkin message:
Fred Drake <fdrake@acm.org>
parents: 12645
diff changeset
4 modified by Marc-Andre Lemburg <mal@lemburg.com> according to the
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 Unicode Integration Proposal (see file Misc/unicode.txt).
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
38335
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
7 Major speed upgrades to the method implementations at the Reykjavik
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
8 NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke.
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
9
13828
254f8b7c4a83 Changing the CNRI copyright notice according to CNRI's instructions.
Guido van Rossum <guido@python.org>
parents: 13761
diff changeset
10 Copyright (c) Corporation for National Research Initiatives.
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
16029
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
12 --------------------------------------------------------------------
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
13 The original string type implementation is:
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
14
51218
aa07a123194a fix indentation in comment
Benjamin Peterson <benjamin@python.org>
parents: 51213
diff changeset
15 Copyright (c) 1999 by Secret Labs AB
aa07a123194a fix indentation in comment
Benjamin Peterson <benjamin@python.org>
parents: 51213
diff changeset
16 Copyright (c) 1999 by Fredrik Lundh
16029
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
17
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
18 By obtaining, using, and/or copying this software and/or its
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
19 associated documentation, you agree that you have read, understood,
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
20 and will comply with the following terms and conditions:
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
21
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
22 Permission to use, copy, modify, and distribute this software and its
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
23 associated documentation for any purpose and without fee is hereby
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
24 granted, provided that the above copyright notice appears in all
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
25 copies, and that both that copyright notice and this permission notice
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
26 appear in supporting documentation, and that the name of Secret Labs
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
27 AB or the author not be used in advertising or publicity pertaining to
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
28 distribution of the software without specific, written prior
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
29 permission.
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
30
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
31 SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
32 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
33 FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
34 ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
35 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
36 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
37 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
38 --------------------------------------------------------------------
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
39
55d89a8fe963 refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents: 15952
diff changeset
40 */
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
37702
9b22d8b7bcaa Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents: 37668
diff changeset
42 #define PY_SSIZE_T_CLEAN
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 #include "Python.h"
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 #include "unicodeobject.h"
13209
b11e0a898de0 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 13196
diff changeset
46 #include "ucnhash.h"
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 23837
diff changeset
48 #ifdef MS_WINDOWS
12067
bcab189ddf86 MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
49 #include <windows.h>
bcab189ddf86 MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
50 #endif
12329
3cc77848a62a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12320
diff changeset
51
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 /* Limit for the Unicode object free list */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 45053
diff changeset
54 #define PyUnicode_MAXFREELIST 1024
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 /* Limit for the Unicode object free list stay alive optimization.
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 The implementation will keep allocated Unicode memory intact for
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 all objects on the free list having a size less than this
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
60 limit. This reduces malloc() overhead for small Unicode objects.
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 45053
diff changeset
62 At worst this will result in PyUnicode_MAXFREELIST *
12329
3cc77848a62a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12320
diff changeset
63 (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT +
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 malloc()-overhead) bytes of unused garbage.
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 Setting the limit to 0 effectively turns the feature off.
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
12329
3cc77848a62a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12320
diff changeset
68 Note: This is an experimental feature ! If you get core dumps when
3cc77848a62a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12320
diff changeset
69 using Unicode objects, turn this feature off.
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
12329
3cc77848a62a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12320
diff changeset
73 #define KEEPALIVE_SIZE_LIMIT 9
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 /* Endianness switches; defaults to little endian */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
76
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 #ifdef WORDS_BIGENDIAN
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 # define BYTEORDER_IS_BIG_ENDIAN
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 #else
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 # define BYTEORDER_IS_LITTLE_ENDIAN
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 #endif
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
12899
9df91f91e5cc Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12876
diff changeset
83 /* --- Globals ------------------------------------------------------------
9df91f91e5cc Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12876
diff changeset
84
9df91f91e5cc Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12876
diff changeset
85 The globals are initialized by the _PyUnicode_Init() API and should
9df91f91e5cc Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12876
diff changeset
86 not be used before calling that API.
9df91f91e5cc Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12876
diff changeset
87
9df91f91e5cc Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12876
diff changeset
88 */
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
89
37655
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37596
diff changeset
90
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37596
diff changeset
91 #ifdef __cplusplus
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37596
diff changeset
92 extern "C" {
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37596
diff changeset
93 #endif
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37596
diff changeset
94
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 /* Free list for Unicode objects */
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 45053
diff changeset
96 static PyUnicodeObject *free_list;
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 45053
diff changeset
97 static int numfree;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
98
17618
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
99 /* The empty Unicode object is shared to improve performance. */
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
100 static PyUnicodeObject *unicode_empty;
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
101
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
102 /* Single character Unicode strings in the Latin-1 range are being
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
103 shared as well. */
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
104 static PyUnicodeObject *unicode_latin1[256];
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
105
12645
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
106 /* Default encoding to use and assume when NULL is passed as encoding
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
107 parameter; it is initialized by _PyUnicode_Init().
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
108
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
109 Always use the PyUnicode_SetDefaultEncoding() and
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
110 PyUnicode_GetDefaultEncoding() APIs to access this global.
12645
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
111
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
112 */
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
113 static char unicode_default_encoding[100];
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
114
45053
26278796d4da Patch #1970 by Antoine Pitrou: Speedup unicode whitespace and linebreak detection. The speedup is about 25% for split() (571 / 457 usec) and 35% (175 / 127 usec )for splitlines()
Christian Heimes <christian@cheimes.de>
parents: 44945
diff changeset
115 /* Fast detection of the most frequent whitespace characters */
26278796d4da Patch #1970 by Antoine Pitrou: Speedup unicode whitespace and linebreak detection. The speedup is about 25% for split() (571 / 457 usec) and 35% (175 / 127 usec )for splitlines()
Christian Heimes <christian@cheimes.de>
parents: 44945
diff changeset
116 const unsigned char _Py_ascii_whitespace[] = {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
117 0, 0, 0, 0, 0, 0, 0, 0,
49386
642f20b22577 Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 49115
diff changeset
118 /* case 0x0009: * HORIZONTAL TABULATION */
642f20b22577 Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 49115
diff changeset
119 /* case 0x000A: * LINE FEED */
642f20b22577 Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 49115
diff changeset
120 /* case 0x000B: * VERTICAL TABULATION */
642f20b22577 Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 49115
diff changeset
121 /* case 0x000C: * FORM FEED */
642f20b22577 Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 49115
diff changeset
122 /* case 0x000D: * CARRIAGE RETURN */
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
123 0, 1, 1, 1, 1, 1, 0, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
124 0, 0, 0, 0, 0, 0, 0, 0,
49386
642f20b22577 Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 49115
diff changeset
125 /* case 0x001C: * FILE SEPARATOR */
642f20b22577 Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 49115
diff changeset
126 /* case 0x001D: * GROUP SEPARATOR */
642f20b22577 Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 49115
diff changeset
127 /* case 0x001E: * RECORD SEPARATOR */
642f20b22577 Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 49115
diff changeset
128 /* case 0x001F: * UNIT SEPARATOR */
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
129 0, 0, 0, 0, 1, 1, 1, 1,
49386
642f20b22577 Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 49115
diff changeset
130 /* case 0x0020: * SPACE */
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
131 1, 0, 0, 0, 0, 0, 0, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
132 0, 0, 0, 0, 0, 0, 0, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
133 0, 0, 0, 0, 0, 0, 0, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
134 0, 0, 0, 0, 0, 0, 0, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
135
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
136 0, 0, 0, 0, 0, 0, 0, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
137 0, 0, 0, 0, 0, 0, 0, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
138 0, 0, 0, 0, 0, 0, 0, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
139 0, 0, 0, 0, 0, 0, 0, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
140 0, 0, 0, 0, 0, 0, 0, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
141 0, 0, 0, 0, 0, 0, 0, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
142 0, 0, 0, 0, 0, 0, 0, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
143 0, 0, 0, 0, 0, 0, 0, 0
45053
26278796d4da Patch #1970 by Antoine Pitrou: Speedup unicode whitespace and linebreak detection. The speedup is about 25% for split() (571 / 457 usec) and 35% (175 / 127 usec )for splitlines()
Christian Heimes <christian@cheimes.de>
parents: 44945
diff changeset
144 };
26278796d4da Patch #1970 by Antoine Pitrou: Speedup unicode whitespace and linebreak detection. The speedup is about 25% for split() (571 / 457 usec) and 35% (175 / 127 usec )for splitlines()
Christian Heimes <christian@cheimes.de>
parents: 44945
diff changeset
145
26278796d4da Patch #1970 by Antoine Pitrou: Speedup unicode whitespace and linebreak detection. The speedup is about 25% for split() (571 / 457 usec) and 35% (175 / 127 usec )for splitlines()
Christian Heimes <christian@cheimes.de>
parents: 44945
diff changeset
146 /* Same for linebreaks */
26278796d4da Patch #1970 by Antoine Pitrou: Speedup unicode whitespace and linebreak detection. The speedup is about 25% for split() (571 / 457 usec) and 35% (175 / 127 usec )for splitlines()
Christian Heimes <christian@cheimes.de>
parents: 44945
diff changeset
147 static unsigned char ascii_linebreak[] = {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
148 0, 0, 0, 0, 0, 0, 0, 0,
49386
642f20b22577 Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 49115
diff changeset
149 /* 0x000A, * LINE FEED */
642f20b22577 Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 49115
diff changeset
150 /* 0x000D, * CARRIAGE RETURN */
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
151 0, 0, 1, 0, 0, 1, 0, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
152 0, 0, 0, 0, 0, 0, 0, 0,
49386
642f20b22577 Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 49115
diff changeset
153 /* 0x001C, * FILE SEPARATOR */
642f20b22577 Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 49115
diff changeset
154 /* 0x001D, * GROUP SEPARATOR */
642f20b22577 Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 49115
diff changeset
155 /* 0x001E, * RECORD SEPARATOR */
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
156 0, 0, 0, 0, 1, 1, 1, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
157 0, 0, 0, 0, 0, 0, 0, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
158 0, 0, 0, 0, 0, 0, 0, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
159 0, 0, 0, 0, 0, 0, 0, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
160 0, 0, 0, 0, 0, 0, 0, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
161
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
162 0, 0, 0, 0, 0, 0, 0, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
163 0, 0, 0, 0, 0, 0, 0, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
164 0, 0, 0, 0, 0, 0, 0, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
165 0, 0, 0, 0, 0, 0, 0, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
166 0, 0, 0, 0, 0, 0, 0, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
167 0, 0, 0, 0, 0, 0, 0, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
168 0, 0, 0, 0, 0, 0, 0, 0,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
169 0, 0, 0, 0, 0, 0, 0, 0
45053
26278796d4da Patch #1970 by Antoine Pitrou: Speedup unicode whitespace and linebreak detection. The speedup is about 25% for split() (571 / 457 usec) and 35% (175 / 127 usec )for splitlines()
Christian Heimes <christian@cheimes.de>
parents: 44945
diff changeset
170 };
26278796d4da Patch #1970 by Antoine Pitrou: Speedup unicode whitespace and linebreak detection. The speedup is about 25% for split() (571 / 457 usec) and 35% (175 / 127 usec )for splitlines()
Christian Heimes <christian@cheimes.de>
parents: 44945
diff changeset
171
26278796d4da Patch #1970 by Antoine Pitrou: Speedup unicode whitespace and linebreak detection. The speedup is about 25% for split() (571 / 457 usec) and 35% (175 / 127 usec )for splitlines()
Christian Heimes <christian@cheimes.de>
parents: 44945
diff changeset
172
18298
982163c4e9d2 Encode surrogates in UTF-8 even for a wide Py_UNICODE.
Martin v. Löwis <martin@v.loewis.de>
parents: 18295
diff changeset
173 Py_UNICODE
18774
57684d3cefce Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents: 18773
diff changeset
174 PyUnicode_GetMax(void)
18298
982163c4e9d2 Encode surrogates in UTF-8 even for a wide Py_UNICODE.
Martin v. Löwis <martin@v.loewis.de>
parents: 18295
diff changeset
175 {
18324
3ba7a358ce02 use Py_UNICODE_WIDE instead of USE_UCS4_STORAGE and Py_UNICODE_SIZE
Fredrik Lundh <fredrik@pythonware.com>
parents: 18298
diff changeset
176 #ifdef Py_UNICODE_WIDE
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
177 return 0x10FFFF;
18298
982163c4e9d2 Encode surrogates in UTF-8 even for a wide Py_UNICODE.
Martin v. Löwis <martin@v.loewis.de>
parents: 18295
diff changeset
178 #else
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
179 /* This is actually an illegal character, so it should
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
180 not be passed to unichr. */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
181 return 0xFFFF;
18298
982163c4e9d2 Encode surrogates in UTF-8 even for a wide Py_UNICODE.
Martin v. Löwis <martin@v.loewis.de>
parents: 18295
diff changeset
182 #endif
982163c4e9d2 Encode surrogates in UTF-8 even for a wide Py_UNICODE.
Martin v. Löwis <martin@v.loewis.de>
parents: 18295
diff changeset
183 }
982163c4e9d2 Encode surrogates in UTF-8 even for a wide Py_UNICODE.
Martin v. Löwis <martin@v.loewis.de>
parents: 18295
diff changeset
184
38257
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
185 /* --- Bloom Filters ----------------------------------------------------- */
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
186
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
187 /* stuff to implement simple "bloom filters" for Unicode characters.
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
188 to keep things simple, we use a single bitmask, using the least 5
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
189 bits from each unicode characters as the bit index. */
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
190
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
191 /* the linebreak mask is set up by Unicode_Init below */
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
192
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
193 #define BLOOM_MASK unsigned long
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
194
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
195 static BLOOM_MASK bloom_linebreak;
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
196
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
197 #define BLOOM(mask, ch) ((mask & (1 << ((ch) & 0x1F))))
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
198
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
199 #define BLOOM_LINEBREAK(ch) \
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
200 ((ch) < 128U ? ascii_linebreak[(ch)] : \
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
201 (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch)))
38257
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
202
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
203 Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len)
38257
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
204 {
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
205 /* calculate simple bloom-style bitmask for a given unicode string */
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
206
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
207 long mask;
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
208 Py_ssize_t i;
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
209
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
210 mask = 0;
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
211 for (i = 0; i < len; i++)
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
212 mask |= (1 << (ptr[i] & 0x1F));
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
213
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
214 return mask;
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
215 }
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
216
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
217 Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen)
38257
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
218 {
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
219 Py_ssize_t i;
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
220
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
221 for (i = 0; i < setlen; i++)
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
222 if (set[i] == chr)
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
223 return 1;
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
224
38270
1dc5657f1745 return 0 on misses, not -1.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38257
diff changeset
225 return 0;
38257
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
226 }
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
227
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
228 #define BLOOM_MEMBER(mask, chr, set, setlen) \
38257
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
229 BLOOM(mask, chr) && unicode_member(chr, set, setlen)
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
230
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 /* --- Unicode Object ----------------------------------------------------- */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
232
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 static
17618
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
234 int unicode_resize(register PyUnicodeObject *unicode,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
235 Py_ssize_t length)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 void *oldstr;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
238
12329
3cc77848a62a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12320
diff changeset
239 /* Shortcut if there's nothing much to do. */
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 if (unicode->length == length)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
241 goto reset;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
242
17618
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
243 /* Resizing shared object (unicode_empty or single character
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
244 objects) in-place is not allowed. Use PyUnicode_Resize()
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
245 instead ! */
38335
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
246
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
247 if (unicode == unicode_empty ||
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
248 (unicode->length == 1 &&
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
249 unicode->str[0] < 256U &&
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
250 unicode_latin1[unicode->str[0]] == unicode)) {
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 PyErr_SetString(PyExc_SystemError,
17618
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
252 "can't resize shared unicode objects");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 return -1;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
255
38335
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
256 /* We allocate one more byte to make sure the string is Ux0000 terminated.
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
257 The overallocation is also used by fastsearch, which assumes that it's
38405
3128cfada6b4 Comment typo
Andrew M. Kuchling <amk@amk.ca>
parents: 38404
diff changeset
258 safe to look at str[length] (without making any assumptions about what
38335
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
259 it contains). */
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
260
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 oldstr = unicode->str;
45804
d33cadb595f6 Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents: 45731
diff changeset
262 unicode->str = PyObject_REALLOC(unicode->str,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
263 sizeof(Py_UNICODE) * (length + 1));
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 if (!unicode->str) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
265 unicode->str = (Py_UNICODE *)oldstr;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 PyErr_NoMemory();
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 return -1;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 unicode->str[length] = 0;
37662
0678ce9fb6b2 Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 37661
diff changeset
270 unicode->length = length;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
271
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
272 reset:
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 /* Reset the object caches */
13837
2badb322355f This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents: 13828
diff changeset
274 if (unicode->defenc) {
2badb322355f This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents: 13828
diff changeset
275 Py_DECREF(unicode->defenc);
2badb322355f This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents: 13828
diff changeset
276 unicode->defenc = NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 unicode->hash = -1;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
279
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 return 0;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
282
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 /* We allocate one more byte to make sure the string is
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
284 Ux0000 terminated -- XXX is this needed ?
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
285
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 XXX This allocator could further be enhanced by assuring that the
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
287 free list never reduces its size below 1.
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
288
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
290
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 static
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
292 PyUnicodeObject *_PyUnicode_New(Py_ssize_t length)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 register PyUnicodeObject *unicode;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
295
38426
9f3375e33db9 fixed typo
Andrew Dalke <dalke@dalkescientific.com>
parents: 38424
diff changeset
296 /* Optimization for empty strings */
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 if (length == 0 && unicode_empty != NULL) {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 Py_INCREF(unicode_empty);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 return unicode_empty;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
301
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
302 /* Ensure we won't overflow the size. */
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
303 if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) {
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
304 return (PyUnicodeObject *)PyErr_NoMemory();
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
305 }
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
306
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 /* Unicode freelist & memory allocation */
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 45053
diff changeset
308 if (free_list) {
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 45053
diff changeset
309 unicode = free_list;
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 45053
diff changeset
310 free_list = *(PyUnicodeObject **)unicode;
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 45053
diff changeset
311 numfree--;
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
312 if (unicode->str) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
313 /* Keep-Alive optimization: we only upsize the buffer,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
314 never downsize it. */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
315 if ((unicode->length < length) &&
30431
cd0c3f916b34 Change checks of PyUnicode_Resize() return value for clarity.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30216
diff changeset
316 unicode_resize(unicode, length) < 0) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
317 PyObject_DEL(unicode->str);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
318 unicode->str = NULL;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
319 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
320 }
18120
c29f3135a20c Fix a mis-indentation in _PyUnicode_New() that caused me to stare at
Guido van Rossum <guido@python.org>
parents: 18051
diff changeset
321 else {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
322 size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
323 unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size);
18120
c29f3135a20c Fix a mis-indentation in _PyUnicode_New() that caused me to stare at
Guido van Rossum <guido@python.org>
parents: 18051
diff changeset
324 }
c29f3135a20c Fix a mis-indentation in _PyUnicode_New() that caused me to stare at
Guido van Rossum <guido@python.org>
parents: 18051
diff changeset
325 PyObject_INIT(unicode, &PyUnicode_Type);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 else {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
328 size_t new_size;
22981
dc1fb19d990d Remove PyMalloc_*.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22938
diff changeset
329 unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 if (unicode == NULL)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 return NULL;
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
332 new_size = sizeof(Py_UNICODE) * ((size_t)length + 1);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
333 unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
335
12511
8747f1c8b5fb Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12369
diff changeset
336 if (!unicode->str) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
337 PyErr_NoMemory();
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
338 goto onError;
12511
8747f1c8b5fb Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12369
diff changeset
339 }
30439
30e495efe90a Double-fix of crash in Unicode freelist handling.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30431
diff changeset
340 /* Initialize the first element to guard against cases where
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
341 * the caller fails before initializing str -- unicode_resize()
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
342 * reads str[0], and the Keep-Alive optimization can keep memory
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
343 * allocated for str alive across a call to unicode_dealloc(unicode).
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
344 * We don't want unicode_resize to read uninitialized memory in
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
345 * that case.
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
346 */
30439
30e495efe90a Double-fix of crash in Unicode freelist handling.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30431
diff changeset
347 unicode->str[0] = 0;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 unicode->str[length] = 0;
37668
4342e441b11c Remove another INT_MAX limitation
Martin v. Löwis <martin@v.loewis.de>
parents: 37662
diff changeset
349 unicode->length = length;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 unicode->hash = -1;
13837
2badb322355f This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents: 13828
diff changeset
351 unicode->defenc = NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 return unicode;
12025
7994d9db7f4d On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents: 11997
diff changeset
353
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
354 onError:
48117
a94f136b5283 Correct a crash when two successive unicode allocations fail with a MemoryError:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48114
diff changeset
355 /* XXX UNREF/NEWREF interface should be more symmetrical */
a94f136b5283 Correct a crash when two successive unicode allocations fail with a MemoryError:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48114
diff changeset
356 _Py_DEC_REFTOTAL;
12025
7994d9db7f4d On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents: 11997
diff changeset
357 _Py_ForgetReference((PyObject *)unicode);
22981
dc1fb19d990d Remove PyMalloc_*.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22938
diff changeset
358 PyObject_Del(unicode);
12025
7994d9db7f4d On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents: 11997
diff changeset
359 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
361
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 static
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20018
diff changeset
363 void unicode_dealloc(register PyUnicodeObject *unicode)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 {
21262
e8fea95a82a4 Fix for #489669 (Neil Norwitz): memory leak in test_descr (unicode).
Guido van Rossum <guido@python.org>
parents: 21082
diff changeset
365 if (PyUnicode_CheckExact(unicode) &&
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
366 numfree < PyUnicode_MAXFREELIST) {
12329
3cc77848a62a Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12320
diff changeset
367 /* Keep-Alive optimization */
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
368 if (unicode->length >= KEEPALIVE_SIZE_LIMIT) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
369 PyObject_DEL(unicode->str);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
370 unicode->str = NULL;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
371 unicode->length = 0;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
372 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
373 if (unicode->defenc) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
374 Py_DECREF(unicode->defenc);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
375 unicode->defenc = NULL;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
376 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
377 /* Add to free list */
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 45053
diff changeset
378 *(PyUnicodeObject **)unicode = free_list;
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 45053
diff changeset
379 free_list = unicode;
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 45053
diff changeset
380 numfree++;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 else {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
383 PyObject_DEL(unicode->str);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
384 Py_XDECREF(unicode->defenc);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
385 Py_TYPE(unicode)->tp_free((PyObject *)unicode);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
388
50305
e673632e1885 Merged revisions 67898,67904-67907,67912,67918,67920-67921,67923-67924,67927,67930,67932,67943 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
389 static
e673632e1885 Merged revisions 67898,67904-67907,67912,67918,67920-67921,67923-67924,67927,67930,67932,67943 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
390 int _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length)
17618
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
391 {
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
392 register PyUnicodeObject *v;
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
393
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
394 /* Argument checks */
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
395 if (unicode == NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
396 PyErr_BadInternalCall();
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
397 return -1;
17618
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
398 }
50305
e673632e1885 Merged revisions 67898,67904-67907,67912,67918,67920-67921,67923-67924,67927,67930,67932,67943 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
399 v = *unicode;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43932
diff changeset
400 if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
401 PyErr_BadInternalCall();
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
402 return -1;
17618
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
403 }
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
404
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
405 /* Resizing unicode_empty and single character objects is not
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
406 possible since these are being shared. We simply return a fresh
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
407 copy with the same Unicode content. */
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
408 if (v->length != length &&
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
409 (v == unicode_empty || v->length == 1)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
410 PyUnicodeObject *w = _PyUnicode_New(length);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
411 if (w == NULL)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
412 return -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
413 Py_UNICODE_COPY(w->str, v->str,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
414 length < v->length ? length : v->length);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
415 Py_DECREF(*unicode);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
416 *unicode = w;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
417 return 0;
17618
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
418 }
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
419
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
420 /* Note that we don't have to modify *unicode for unshared Unicode
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
421 objects, since we can modify them in-place. */
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
422 return unicode_resize(v, length);
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
423 }
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
424
50305
e673632e1885 Merged revisions 67898,67904-67907,67912,67918,67920-67921,67923-67924,67927,67930,67932,67943 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
425 int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length)
e673632e1885 Merged revisions 67898,67904-67907,67912,67918,67920-67921,67923-67924,67927,67930,67932,67943 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
426 {
e673632e1885 Merged revisions 67898,67904-67907,67912,67918,67920-67921,67923-67924,67927,67930,67932,67943 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
427 return _PyUnicode_Resize((PyUnicodeObject **)unicode, length);
e673632e1885 Merged revisions 67898,67904-67907,67912,67918,67920-67921,67923-67924,67927,67930,67932,67943 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
428 }
17618
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
429
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
430 PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
431 Py_ssize_t size)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
432 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 PyUnicodeObject *unicode;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
434
17618
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
435 /* If the Unicode data is known at construction time, we can apply
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
436 some optimizations which share commonly used objects. */
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
437 if (u != NULL) {
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
438
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
439 /* Optimization for empty strings */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
440 if (size == 0 && unicode_empty != NULL) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
441 Py_INCREF(unicode_empty);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
442 return (PyObject *)unicode_empty;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
443 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
444
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
445 /* Single character Unicode objects in the Latin-1 range are
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
446 shared when using this constructor */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
447 if (size == 1 && *u < 256) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
448 unicode = unicode_latin1[*u];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
449 if (!unicode) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
450 unicode = _PyUnicode_New(1);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
451 if (!unicode)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
452 return NULL;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
453 unicode->str[0] = *u;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
454 unicode_latin1[*u] = unicode;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
455 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
456 Py_INCREF(unicode);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
457 return (PyObject *)unicode;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
458 }
17618
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
459 }
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
460
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 unicode = _PyUnicode_New(size);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 if (!unicode)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
464
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
465 /* Copy the Unicode data into the new object */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
466 if (u != NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
467 Py_UNICODE_COPY(unicode->str, u, size);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
468
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 return (PyObject *)unicode;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
471
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
472 PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size)
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
473 {
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
474 PyUnicodeObject *unicode;
46383
0b55875c1c9c Raise SystemError when size < 0 is passed into PyString_FromStringAndSize,
Gregory P. Smith <greg@mad-scientist.com>
parents: 46333
diff changeset
475
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
476 if (size < 0) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
477 PyErr_SetString(PyExc_SystemError,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
478 "Negative size passed to PyUnicode_FromStringAndSize");
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
479 return NULL;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
480 }
46383
0b55875c1c9c Raise SystemError when size < 0 is passed into PyString_FromStringAndSize,
Gregory P. Smith <greg@mad-scientist.com>
parents: 46333
diff changeset
481
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
482 /* If the Unicode data is known at construction time, we can apply
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
483 some optimizations which share commonly used objects.
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
484 Also, this means the input must be UTF-8, so fall back to the
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
485 UTF-8 decoder at the end. */
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
486 if (u != NULL) {
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
487
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
488 /* Optimization for empty strings */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
489 if (size == 0 && unicode_empty != NULL) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
490 Py_INCREF(unicode_empty);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
491 return (PyObject *)unicode_empty;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
492 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
493
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
494 /* Single characters are shared when using this constructor.
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
495 Restrict to ASCII, since the input must be UTF-8. */
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
496 if (size == 1 && Py_CHARMASK(*u) < 128) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
497 unicode = unicode_latin1[Py_CHARMASK(*u)];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
498 if (!unicode) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
499 unicode = _PyUnicode_New(1);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
500 if (!unicode)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
501 return NULL;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
502 unicode->str[0] = Py_CHARMASK(*u);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
503 unicode_latin1[Py_CHARMASK(*u)] = unicode;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
504 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
505 Py_INCREF(unicode);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
506 return (PyObject *)unicode;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
507 }
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
508
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
509 return PyUnicode_DecodeUTF8(u, size, NULL);
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
510 }
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
511
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
512 unicode = _PyUnicode_New(size);
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
513 if (!unicode)
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
514 return NULL;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
515
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
516 return (PyObject *)unicode;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
517 }
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
518
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
519 PyObject *PyUnicode_FromString(const char *u)
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
520 {
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
521 size_t size = strlen(u);
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
522 if (size > PY_SSIZE_T_MAX) {
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
523 PyErr_SetString(PyExc_OverflowError, "input too long");
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
524 return NULL;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
525 }
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
526
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
527 return PyUnicode_FromStringAndSize(u, size);
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
528 }
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
529
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
530 #ifdef HAVE_WCHAR_H
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
531
52210
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
532 #if (Py_UNICODE_SIZE == 2) && defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
533 # define CONVERT_WCHAR_TO_SURROGATES
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
534 #endif
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
535
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
536 #ifdef CONVERT_WCHAR_TO_SURROGATES
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
537
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
538 /* Here sizeof(wchar_t) is 4 but Py_UNICODE_SIZE == 2, so we need
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
539 to convert from UTF32 to UTF16. */
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
540
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
541 PyObject *PyUnicode_FromWideChar(register const wchar_t *w,
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
542 Py_ssize_t size)
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
543 {
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
544 PyUnicodeObject *unicode;
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
545 register Py_ssize_t i;
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
546 Py_ssize_t alloc;
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
547 const wchar_t *orig_w;
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
548
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
549 if (w == NULL) {
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
550 PyErr_BadInternalCall();
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
551 return NULL;
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
552 }
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
553
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
554 alloc = size;
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
555 orig_w = w;
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
556 for (i = size; i > 0; i--) {
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
557 if (*w > 0xFFFF)
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
558 alloc++;
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
559 w++;
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
560 }
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
561 w = orig_w;
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
562 unicode = _PyUnicode_New(alloc);
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
563 if (!unicode)
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
564 return NULL;
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
565
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
566 /* Copy the wchar_t data into the new object */
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
567 {
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
568 register Py_UNICODE *u;
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
569 u = PyUnicode_AS_UNICODE(unicode);
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
570 for (i = size; i > 0; i--) {
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
571 if (*w > 0xFFFF) {
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
572 wchar_t ordinal = *w++;
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
573 ordinal -= 0x10000;
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
574 *u++ = 0xD800 | (ordinal >> 10);
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
575 *u++ = 0xDC00 | (ordinal & 0x3FF);
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
576 }
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
577 else
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
578 *u++ = *w++;
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
579 }
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
580 }
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
581 return (PyObject *)unicode;
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
582 }
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
583
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
584 #else
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
585
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
586 PyObject *PyUnicode_FromWideChar(register const wchar_t *w,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
587 Py_ssize_t size)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
588 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
589 PyUnicodeObject *unicode;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
590
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
591 if (w == NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
592 PyErr_BadInternalCall();
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
593 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
594 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
595
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
596 unicode = _PyUnicode_New(size);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
597 if (!unicode)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
598 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
599
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
600 /* Copy the wchar_t data into the new object */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
601 #ifdef HAVE_USABLE_WCHAR_T
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
602 memcpy(unicode->str, w, size * sizeof(wchar_t));
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
603 #else
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
604 {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
605 register Py_UNICODE *u;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
606 register Py_ssize_t i;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
607 u = PyUnicode_AS_UNICODE(unicode);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
608 for (i = size; i > 0; i--)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
609 *u++ = *w++;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
610 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
611 #endif
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
612
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
613 return (PyObject *)unicode;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
615
52210
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
616 #endif /* CONVERT_WCHAR_TO_SURROGATES */
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
617
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
618 #undef CONVERT_WCHAR_TO_SURROGATES
c73dd25c01c1 Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents: 51775
diff changeset
619
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
620 static void
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
621 makefmt(char *fmt, int longflag, int size_tflag, int zeropad, int width, int precision, char c)
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
622 {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
623 *fmt++ = '%';
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
624 if (width) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
625 if (zeropad)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
626 *fmt++ = '0';
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
627 fmt += sprintf(fmt, "%d", width);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
628 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
629 if (precision)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
630 fmt += sprintf(fmt, ".%d", precision);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
631 if (longflag)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
632 *fmt++ = 'l';
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
633 else if (size_tflag) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
634 char *f = PY_FORMAT_SIZE_T;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
635 while (*f)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
636 *fmt++ = *f++;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
637 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
638 *fmt++ = c;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
639 *fmt = '\0';
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
640 }
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
641
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
642 #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;}
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
643
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
644 PyObject *
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
645 PyUnicode_FromFormatV(const char *format, va_list vargs)
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
646 {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
647 va_list count;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
648 Py_ssize_t callcount = 0;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
649 PyObject **callresults = NULL;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
650 PyObject **callresult = NULL;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
651 Py_ssize_t n = 0;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
652 int width = 0;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
653 int precision = 0;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
654 int zeropad;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
655 const char* f;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
656 Py_UNICODE *s;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
657 PyObject *string;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
658 /* used by sprintf */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
659 char buffer[21];
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
660 /* use abuffer instead of buffer, if we need more space
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
661 * (which can happen if there's a format specifier with width). */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
662 char *abuffer = NULL;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
663 char *realbuffer;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
664 Py_ssize_t abuffersize = 0;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
665 char fmt[60]; /* should be enough for %0width.precisionld */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
666 const char *copy;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
667
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
668 #ifdef VA_LIST_IS_ARRAY
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
669 Py_MEMCPY(count, vargs, sizeof(va_list));
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
670 #else
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
671 #ifdef __va_copy
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
672 __va_copy(count, vargs);
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
673 #else
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
674 count = vargs;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
675 #endif
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
676 #endif
53719
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
677 /* step 1: count the number of %S/%R/%s format specifications
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
678 * (we call PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() for these
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
679 * objects once during step 3 and put the result in an array) */
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
680 for (f = format; *f; f++) {
53719
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
681 if (*f == '%') {
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
682 if (*(f+1)=='%')
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
683 continue;
53722
e4af971b9184 There's no %A in Python 2.x!
Walter Dörwald <walter@livinglogic.de>
parents: 53719
diff changeset
684 if (*(f+1)=='S' || *(f+1)=='R')
53719
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
685 ++callcount;
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
686 while (isdigit((unsigned)*f))
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
687 width = (width*10) + *f++ - '0';
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
688 while (*++f && *f != '%' && !isalpha((unsigned)*f))
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
689 ;
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
690 if (*f == 's')
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
691 ++callcount;
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
692 }
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
693 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
694 /* step 2: allocate memory for the results of
53719
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
695 * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
696 if (callcount) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
697 callresults = PyObject_Malloc(sizeof(PyObject *)*callcount);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
698 if (!callresults) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
699 PyErr_NoMemory();
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
700 return NULL;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
701 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
702 callresult = callresults;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
703 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
704 /* step 3: figure out how large a buffer we need */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
705 for (f = format; *f; f++) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
706 if (*f == '%') {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
707 const char* p = f;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
708 width = 0;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
709 while (isdigit((unsigned)*f))
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
710 width = (width*10) + *f++ - '0';
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
711 while (*++f && *f != '%' && !isalpha((unsigned)*f))
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
712 ;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
713
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
714 /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
715 * they don't affect the amount of space we reserve.
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
716 */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
717 if ((*f == 'l' || *f == 'z') &&
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
718 (f[1] == 'd' || f[1] == 'u'))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
719 ++f;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
720
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
721 switch (*f) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
722 case 'c':
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
723 (void)va_arg(count, int);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
724 /* fall through... */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
725 case '%':
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
726 n++;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
727 break;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
728 case 'd': case 'u': case 'i': case 'x':
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
729 (void) va_arg(count, int);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
730 /* 20 bytes is enough to hold a 64-bit
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
731 integer. Decimal takes the most space.
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
732 This isn't enough for octal.
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
733 If a width is specified we need more
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
734 (which we allocate later). */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
735 if (width < 20)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
736 width = 20;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
737 n += width;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
738 if (abuffersize < width)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
739 abuffersize = width;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
740 break;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
741 case 's':
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
742 {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
743 /* UTF-8 */
53780
5878ca784a84 #5929: fix signedness warning.
Georg Brandl <georg@python.org>
parents: 53740
diff changeset
744 const char *s = va_arg(count, const char*);
53719
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
745 PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace");
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
746 if (!str)
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
747 goto fail;
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
748 n += PyUnicode_GET_SIZE(str);
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
749 /* Remember the str and switch to the next slot */
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
750 *callresult++ = str;
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
751 break;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
752 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
753 case 'U':
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
754 {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
755 PyObject *obj = va_arg(count, PyObject *);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
756 assert(obj && PyUnicode_Check(obj));
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
757 n += PyUnicode_GET_SIZE(obj);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
758 break;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
759 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
760 case 'V':
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
761 {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
762 PyObject *obj = va_arg(count, PyObject *);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
763 const char *str = va_arg(count, const char *);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
764 assert(obj || str);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
765 assert(!obj || PyUnicode_Check(obj));
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
766 if (obj)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
767 n += PyUnicode_GET_SIZE(obj);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
768 else
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
769 n += strlen(str);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
770 break;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
771 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
772 case 'S':
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
773 {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
774 PyObject *obj = va_arg(count, PyObject *);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
775 PyObject *str;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
776 assert(obj);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
777 str = PyObject_Str(obj);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
778 if (!str)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
779 goto fail;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
780 n += PyUnicode_GET_SIZE(str);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
781 /* Remember the str and switch to the next slot */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
782 *callresult++ = str;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
783 break;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
784 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
785 case 'R':
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
786 {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
787 PyObject *obj = va_arg(count, PyObject *);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
788 PyObject *repr;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
789 assert(obj);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
790 repr = PyObject_Repr(obj);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
791 if (!repr)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
792 goto fail;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
793 n += PyUnicode_GET_SIZE(repr);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
794 /* Remember the repr and switch to the next slot */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
795 *callresult++ = repr;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
796 break;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
797 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
798 case 'p':
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
799 (void) va_arg(count, int);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
800 /* maximum 64-bit pointer representation:
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
801 * 0xffffffffffffffff
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
802 * so 19 characters is enough.
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
803 * XXX I count 18 -- what's the extra for?
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
804 */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
805 n += 19;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
806 break;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
807 default:
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
808 /* if we stumble upon an unknown
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
809 formatting code, copy the rest of
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
810 the format string to the output
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
811 string. (we cannot just skip the
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
812 code, since there's no way to know
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
813 what's in the argument list) */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
814 n += strlen(p);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
815 goto expand;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
816 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
817 } else
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
818 n++;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
819 }
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
820 expand:
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
821 if (abuffersize > 20) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
822 abuffer = PyObject_Malloc(abuffersize);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
823 if (!abuffer) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
824 PyErr_NoMemory();
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
825 goto fail;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
826 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
827 realbuffer = abuffer;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
828 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
829 else
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
830 realbuffer = buffer;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
831 /* step 4: fill the buffer */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
832 /* Since we've analyzed how much space we need for the worst case,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
833 we don't have to resize the string.
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
834 There can be no errors beyond this point. */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
835 string = PyUnicode_FromUnicode(NULL, n);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
836 if (!string)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
837 goto fail;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
838
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
839 s = PyUnicode_AS_UNICODE(string);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
840 callresult = callresults;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
841
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
842 for (f = format; *f; f++) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
843 if (*f == '%') {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
844 const char* p = f++;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
845 int longflag = 0;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
846 int size_tflag = 0;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
847 zeropad = (*f == '0');
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
848 /* parse the width.precision part */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
849 width = 0;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
850 while (isdigit((unsigned)*f))
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
851 width = (width*10) + *f++ - '0';
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
852 precision = 0;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
853 if (*f == '.') {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
854 f++;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
855 while (isdigit((unsigned)*f))
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
856 precision = (precision*10) + *f++ - '0';
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
857 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
858 /* handle the long flag, but only for %ld and %lu.
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
859 others can be added when necessary. */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
860 if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
861 longflag = 1;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
862 ++f;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
863 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
864 /* handle the size_t flag. */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
865 if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
866 size_tflag = 1;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
867 ++f;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
868 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
869
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
870 switch (*f) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
871 case 'c':
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
872 *s++ = va_arg(vargs, int);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
873 break;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
874 case 'd':
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
875 makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd');
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
876 if (longflag)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
877 sprintf(realbuffer, fmt, va_arg(vargs, long));
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
878 else if (size_tflag)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
879 sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t));
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
880 else
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
881 sprintf(realbuffer, fmt, va_arg(vargs, int));
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
882 appendstring(realbuffer);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
883 break;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
884 case 'u':
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
885 makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u');
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
886 if (longflag)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
887 sprintf(realbuffer, fmt, va_arg(vargs, unsigned long));
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
888 else if (size_tflag)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
889 sprintf(realbuffer, fmt, va_arg(vargs, size_t));
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
890 else
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
891 sprintf(realbuffer, fmt, va_arg(vargs, unsigned int));
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
892 appendstring(realbuffer);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
893 break;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
894 case 'i':
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
895 makefmt(fmt, 0, 0, zeropad, width, precision, 'i');
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
896 sprintf(realbuffer, fmt, va_arg(vargs, int));
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
897 appendstring(realbuffer);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
898 break;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
899 case 'x':
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
900 makefmt(fmt, 0, 0, zeropad, width, precision, 'x');
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
901 sprintf(realbuffer, fmt, va_arg(vargs, int));
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
902 appendstring(realbuffer);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
903 break;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
904 case 's':
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
905 {
53719
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
906 /* unused, since we already have the result */
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
907 (void) va_arg(vargs, char *);
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
908 Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult),
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
909 PyUnicode_GET_SIZE(*callresult));
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
910 s += PyUnicode_GET_SIZE(*callresult);
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
911 /* We're done with the unicode()/repr() => forget it */
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
912 Py_DECREF(*callresult);
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
913 /* switch to next unicode()/repr() result */
1c27ecbed8c4 Issue #5108: Handle %s like %S and %R in PyUnicode_FromFormatV(): Call
Walter Dörwald <walter@livinglogic.de>
parents: 53442
diff changeset
914 ++callresult;
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
915 break;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
916 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
917 case 'U':
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
918 {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
919 PyObject *obj = va_arg(vargs, PyObject *);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
920 Py_ssize_t size = PyUnicode_GET_SIZE(obj);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
921 Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
922 s += size;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
923 break;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
924 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
925 case 'V':
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
926 {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
927 PyObject *obj = va_arg(vargs, PyObject *);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
928 const char *str = va_arg(vargs, const char *);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
929 if (obj) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
930 Py_ssize_t size = PyUnicode_GET_SIZE(obj);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
931 Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
932 s += size;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
933 } else {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
934 appendstring(str);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
935 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
936 break;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
937 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
938 case 'S':
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
939 case 'R':
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
940 {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
941 Py_UNICODE *ucopy;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
942 Py_ssize_t usize;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
943 Py_ssize_t upos;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
944 /* unused, since we already have the result */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
945 (void) va_arg(vargs, PyObject *);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
946 ucopy = PyUnicode_AS_UNICODE(*callresult);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
947 usize = PyUnicode_GET_SIZE(*callresult);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
948 for (upos = 0; upos<usize;)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
949 *s++ = ucopy[upos++];
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
950 /* We're done with the unicode()/repr() => forget it */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
951 Py_DECREF(*callresult);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
952 /* switch to next unicode()/repr() result */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
953 ++callresult;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
954 break;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
955 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
956 case 'p':
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
957 sprintf(buffer, "%p", va_arg(vargs, void*));
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
958 /* %p is ill-defined: ensure leading 0x. */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
959 if (buffer[1] == 'X')
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
960 buffer[1] = 'x';
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
961 else if (buffer[1] != 'x') {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
962 memmove(buffer+2, buffer, strlen(buffer)+1);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
963 buffer[0] = '0';
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
964 buffer[1] = 'x';
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
965 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
966 appendstring(buffer);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
967 break;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
968 case '%':
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
969 *s++ = '%';
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
970 break;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
971 default:
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
972 appendstring(p);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
973 goto end;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
974 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
975 } else
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
976 *s++ = *f;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
977 }
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
978
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
979 end:
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
980 if (callresults)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
981 PyObject_Free(callresults);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
982 if (abuffer)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
983 PyObject_Free(abuffer);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
984 PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string));
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
985 return string;
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
986 fail:
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
987 if (callresults) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
988 PyObject **callresult2 = callresults;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
989 while (callresult2 < callresult) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
990 Py_DECREF(*callresult2);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
991 ++callresult2;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
992 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
993 PyObject_Free(callresults);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
994 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
995 if (abuffer)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
996 PyObject_Free(abuffer);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
997 return NULL;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
998 }
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
999
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
1000 #undef appendstring
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
1001
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
1002 PyObject *
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
1003 PyUnicode_FromFormat(const char *format, ...)
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
1004 {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
1005 PyObject* ret;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
1006 va_list vargs;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
1007
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
1008 #ifdef HAVE_STDARG_PROTOTYPES
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
1009 va_start(vargs, format);
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
1010 #else
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
1011 va_start(vargs);
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
1012 #endif
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
1013 ret = PyUnicode_FromFormatV(format, vargs);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
1014 va_end(vargs);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
1015 return ret;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
1016 }
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
1017
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
1018 Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1019 wchar_t *w,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1020 Py_ssize_t size)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1021 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1022 if (unicode == NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1023 PyErr_BadInternalCall();
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1024 return -1;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1025 }
34016
264e45baacd5 Correct the handling of 0-termination of PyUnicode_AsWideChar()
Marc-André Lemburg <mal@egenix.com>
parents: 33766
diff changeset
1026
264e45baacd5 Correct the handling of 0-termination of PyUnicode_AsWideChar()
Marc-André Lemburg <mal@egenix.com>
parents: 33766
diff changeset
1027 /* If possible, try to copy the 0-termination as well */
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1028 if (size > PyUnicode_GET_SIZE(unicode))
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1029 size = PyUnicode_GET_SIZE(unicode) + 1;
34016
264e45baacd5 Correct the handling of 0-termination of PyUnicode_AsWideChar()
Marc-André Lemburg <mal@egenix.com>
parents: 33766
diff changeset
1030
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1031 #ifdef HAVE_USABLE_WCHAR_T
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1032 memcpy(w, unicode->str, size * sizeof(wchar_t));
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1033 #else
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1034 {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1035 register Py_UNICODE *u;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1036 register Py_ssize_t i;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1037 u = PyUnicode_AS_UNICODE(unicode);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1038 for (i = size; i > 0; i--)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1039 *w++ = *u++;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1040 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1041 #endif
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1042
34016
264e45baacd5 Correct the handling of 0-termination of PyUnicode_AsWideChar()
Marc-André Lemburg <mal@egenix.com>
parents: 33766
diff changeset
1043 if (size > PyUnicode_GET_SIZE(unicode))
264e45baacd5 Correct the handling of 0-termination of PyUnicode_AsWideChar()
Marc-André Lemburg <mal@egenix.com>
parents: 33766
diff changeset
1044 return PyUnicode_GET_SIZE(unicode);
264e45baacd5 Correct the handling of 0-termination of PyUnicode_AsWideChar()
Marc-André Lemburg <mal@egenix.com>
parents: 33766
diff changeset
1045 else
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1046 return size;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1047 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1048
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1049 #endif
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1050
24737
eaa87632b1e8 Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents: 24736
diff changeset
1051 PyObject *PyUnicode_FromOrdinal(int ordinal)
eaa87632b1e8 Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents: 24736
diff changeset
1052 {
31873
227cb7402c6e SF Patch #926375: Remove a useless UTF-16 support code that is never
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31455
diff changeset
1053 Py_UNICODE s[1];
24737
eaa87632b1e8 Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents: 24736
diff changeset
1054
eaa87632b1e8 Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents: 24736
diff changeset
1055 #ifdef Py_UNICODE_WIDE
eaa87632b1e8 Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents: 24736
diff changeset
1056 if (ordinal < 0 || ordinal > 0x10ffff) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1057 PyErr_SetString(PyExc_ValueError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1058 "unichr() arg not in range(0x110000) "
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1059 "(wide Python build)");
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1060 return NULL;
24737
eaa87632b1e8 Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents: 24736
diff changeset
1061 }
eaa87632b1e8 Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents: 24736
diff changeset
1062 #else
eaa87632b1e8 Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents: 24736
diff changeset
1063 if (ordinal < 0 || ordinal > 0xffff) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1064 PyErr_SetString(PyExc_ValueError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1065 "unichr() arg not in range(0x10000) "
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1066 "(narrow Python build)");
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1067 return NULL;
24737
eaa87632b1e8 Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents: 24736
diff changeset
1068 }
eaa87632b1e8 Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents: 24736
diff changeset
1069 #endif
eaa87632b1e8 Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents: 24736
diff changeset
1070
31873
227cb7402c6e SF Patch #926375: Remove a useless UTF-16 support code that is never
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31455
diff changeset
1071 s[0] = (Py_UNICODE)ordinal;
227cb7402c6e SF Patch #926375: Remove a useless UTF-16 support code that is never
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31455
diff changeset
1072 return PyUnicode_FromUnicode(s, 1);
24737
eaa87632b1e8 Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents: 24736
diff changeset
1073 }
eaa87632b1e8 Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents: 24736
diff changeset
1074
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1075 PyObject *PyUnicode_FromObject(register PyObject *obj)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1076 {
20554
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1077 /* XXX Perhaps we should make this API an alias of
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1078 PyObject_Unicode() instead ?! */
20554
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1079 if (PyUnicode_CheckExact(obj)) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1080 Py_INCREF(obj);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1081 return obj;
20554
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1082 }
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1083 if (PyUnicode_Check(obj)) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1084 /* For a Unicode subtype that's not a Unicode object,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1085 return a true Unicode object with the same data. */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1086 return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj),
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1087 PyUnicode_GET_SIZE(obj));
20554
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1088 }
13401
8f1a2deeb9b0 Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents: 13396
diff changeset
1089 return PyUnicode_FromEncodedObject(obj, NULL, "strict");
8f1a2deeb9b0 Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents: 13396
diff changeset
1090 }
8f1a2deeb9b0 Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents: 13396
diff changeset
1091
8f1a2deeb9b0 Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents: 13396
diff changeset
1092 PyObject *PyUnicode_FromEncodedObject(register PyObject *obj,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1093 const char *encoding,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1094 const char *errors)
13401
8f1a2deeb9b0 Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents: 13396
diff changeset
1095 {
19999
1f4e2627b627 Implement the changes proposed in patch #413333. unicode(obj) now
Marc-André Lemburg <mal@egenix.com>
parents: 19998
diff changeset
1096 const char *s = NULL;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
1097 Py_ssize_t len;
13401
8f1a2deeb9b0 Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents: 13396
diff changeset
1098 PyObject *v;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
1099
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1100 if (obj == NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1101 PyErr_BadInternalCall();
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1102 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1103 }
13401
8f1a2deeb9b0 Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents: 13396
diff changeset
1104
20554
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1105 #if 0
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1106 /* For b/w compatibility we also accept Unicode objects provided
20563
9058e49c6c57 Additional test and documentation for the unicode() changes.
Marc-André Lemburg <mal@egenix.com>
parents: 20554
diff changeset
1107 that no encodings is given and then redirect to
9058e49c6c57 Additional test and documentation for the unicode() changes.
Marc-André Lemburg <mal@egenix.com>
parents: 20554
diff changeset
1108 PyObject_Unicode() which then applies the additional logic for
9058e49c6c57 Additional test and documentation for the unicode() changes.
Marc-André Lemburg <mal@egenix.com>
parents: 20554
diff changeset
1109 Unicode subclasses.
20554
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1110
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1111 NOTE: This API should really only be used for object which
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1112 represent *encoded* Unicode !
20554
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1113
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1114 */
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
1115 if (PyUnicode_Check(obj)) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
1116 if (encoding) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1117 PyErr_SetString(PyExc_TypeError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1118 "decoding Unicode is not supported");
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1119 return NULL;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1120 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1121 return PyObject_Unicode(obj);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1122 }
20554
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1123 #else
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1124 if (PyUnicode_Check(obj)) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1125 PyErr_SetString(PyExc_TypeError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1126 "decoding Unicode is not supported");
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1127 return NULL;
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
1128 }
20554
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1129 #endif
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1130
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1131 /* Coerce object */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
1132 if (PyString_Check(obj)) {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
1133 s = PyString_AS_STRING(obj);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
1134 len = PyString_GET_SIZE(obj);
46107
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46062
diff changeset
1135 }
47291
04bd3690fb1f First step of the C API rename:
Christian Heimes <christian@cheimes.de>
parents: 46908
diff changeset
1136 else if (PyByteArray_Check(obj)) {
46107
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46062
diff changeset
1137 /* Python 2.x specific */
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46062
diff changeset
1138 PyErr_Format(PyExc_TypeError,
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46062
diff changeset
1139 "decoding bytearray is not supported");
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46062
diff changeset
1140 return NULL;
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46062
diff changeset
1141 }
20554
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1142 else if (PyObject_AsCharBuffer(obj, &s, &len)) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1143 /* Overwrite the error message with something more useful in
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1144 case of a TypeError. */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1145 if (PyErr_ExceptionMatches(PyExc_TypeError))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1146 PyErr_Format(PyExc_TypeError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1147 "coercing to Unicode: need string or buffer, "
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1148 "%.80s found",
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1149 Py_TYPE(obj)->tp_name);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1150 goto onError;
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12193
diff changeset
1151 }
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
1152
13401
8f1a2deeb9b0 Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents: 13396
diff changeset
1153 /* Convert to Unicode */
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1154 if (len == 0) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1155 Py_INCREF(unicode_empty);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1156 v = (PyObject *)unicode_empty;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1157 }
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
1158 else
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1159 v = PyUnicode_Decode(s, len, encoding, errors);
15952
1f92d63b20f0 This patch adds a new builtin unistr() which behaves like str()
Marc-André Lemburg <mal@egenix.com>
parents: 15910
diff changeset
1160
13401
8f1a2deeb9b0 Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents: 13396
diff changeset
1161 return v;
8f1a2deeb9b0 Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents: 13396
diff changeset
1162
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1163 onError:
13401
8f1a2deeb9b0 Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents: 13396
diff changeset
1164 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1165 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1166
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1167 PyObject *PyUnicode_Decode(const char *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1168 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1169 const char *encoding,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1170 const char *errors)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1171 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1172 PyObject *buffer = NULL, *unicode;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1173
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1174 if (encoding == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1175 encoding = PyUnicode_GetDefaultEncoding();
12645
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1176
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1177 /* Shortcuts for common default encodings */
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1178 if (strcmp(encoding, "utf-8") == 0)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1179 return PyUnicode_DecodeUTF8(s, size, errors);
12645
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1180 else if (strcmp(encoding, "latin-1") == 0)
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1181 return PyUnicode_DecodeLatin1(s, size, errors);
29706
a4fead0e24f8 Support 'mbcs' as a 'built-in' encoding, so the C API can use it without
Mark Hammond <mhammond@skippinet.com.au>
parents: 29641
diff changeset
1182 #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
a4fead0e24f8 Support 'mbcs' as a 'built-in' encoding, so the C API can use it without
Mark Hammond <mhammond@skippinet.com.au>
parents: 29641
diff changeset
1183 else if (strcmp(encoding, "mbcs") == 0)
a4fead0e24f8 Support 'mbcs' as a 'built-in' encoding, so the C API can use it without
Mark Hammond <mhammond@skippinet.com.au>
parents: 29641
diff changeset
1184 return PyUnicode_DecodeMBCS(s, size, errors);
a4fead0e24f8 Support 'mbcs' as a 'built-in' encoding, so the C API can use it without
Mark Hammond <mhammond@skippinet.com.au>
parents: 29641
diff changeset
1185 #endif
12645
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1186 else if (strcmp(encoding, "ascii") == 0)
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1187 return PyUnicode_DecodeASCII(s, size, errors);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1188
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1189 /* Decode via the codec registry */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1190 buffer = PyBuffer_FromMemory((void *)s, size);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1191 if (buffer == NULL)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1192 goto onError;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1193 unicode = PyCodec_Decode(buffer, encoding, errors);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1194 if (unicode == NULL)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1195 goto onError;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1196 if (!PyUnicode_Check(unicode)) {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1197 PyErr_Format(PyExc_TypeError,
12320
5de0b939fe61 Skip Montanaro: add string precisions to calls to PyErr_Format
Guido van Rossum <guido@python.org>
parents: 12275
diff changeset
1198 "decoder did not return an unicode object (type=%.400s)",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43932
diff changeset
1199 Py_TYPE(unicode)->tp_name);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1200 Py_DECREF(unicode);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1201 goto onError;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1202 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1203 Py_DECREF(buffer);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1204 return unicode;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
1205
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1206 onError:
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1207 Py_XDECREF(buffer);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1208 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1209 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1210
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1211 PyObject *PyUnicode_AsDecodedObject(PyObject *unicode,
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1212 const char *encoding,
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1213 const char *errors)
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1214 {
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1215 PyObject *v;
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1216
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1217 if (!PyUnicode_Check(unicode)) {
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1218 PyErr_BadArgument();
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1219 goto onError;
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1220 }
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1221
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1222 if (encoding == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1223 encoding = PyUnicode_GetDefaultEncoding();
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1224
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1225 /* Decode via the codec registry */
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1226 v = PyCodec_Decode(unicode, encoding, errors);
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1227 if (v == NULL)
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1228 goto onError;
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1229 return v;
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1230
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1231 onError:
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1232 return NULL;
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1233 }
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1234
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1235 PyObject *PyUnicode_Encode(const Py_UNICODE *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1236 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1237 const char *encoding,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1238 const char *errors)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1239 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1240 PyObject *v, *unicode;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
1241
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1242 unicode = PyUnicode_FromUnicode(s, size);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1243 if (unicode == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1244 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1245 v = PyUnicode_AsEncodedString(unicode, encoding, errors);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1246 Py_DECREF(unicode);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1247 return v;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1248 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1249
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1250 PyObject *PyUnicode_AsEncodedObject(PyObject *unicode,
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1251 const char *encoding,
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1252 const char *errors)
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1253 {
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1254 PyObject *v;
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1255
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1256 if (!PyUnicode_Check(unicode)) {
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1257 PyErr_BadArgument();
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1258 goto onError;
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1259 }
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1260
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1261 if (encoding == NULL)
51212
10daee3a3609 fix indentation
Benjamin Peterson <benjamin@python.org>
parents: 51204
diff changeset
1262 encoding = PyUnicode_GetDefaultEncoding();
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1263
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1264 /* Encode via the codec registry */
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1265 v = PyCodec_Encode(unicode, encoding, errors);
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1266 if (v == NULL)
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1267 goto onError;
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1268 return v;
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1269
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1270 onError:
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1271 return NULL;
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1272 }
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
1273
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1274 PyObject *PyUnicode_AsEncodedString(PyObject *unicode,
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1275 const char *encoding,
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1276 const char *errors)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1277 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1278 PyObject *v;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
1279
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1280 if (!PyUnicode_Check(unicode)) {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1281 PyErr_BadArgument();
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1282 goto onError;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1283 }
12645
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1284
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
1285 if (encoding == NULL)
51212
10daee3a3609 fix indentation
Benjamin Peterson <benjamin@python.org>
parents: 51204
diff changeset
1286 encoding = PyUnicode_GetDefaultEncoding();
12645
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1287
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1288 /* Shortcuts for common default encodings */
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1289 if (errors == NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1290 if (strcmp(encoding, "utf-8") == 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1291 return PyUnicode_AsUTF8String(unicode);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1292 else if (strcmp(encoding, "latin-1") == 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1293 return PyUnicode_AsLatin1String(unicode);
29706
a4fead0e24f8 Support 'mbcs' as a 'built-in' encoding, so the C API can use it without
Mark Hammond <mhammond@skippinet.com.au>
parents: 29641
diff changeset
1294 #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1295 else if (strcmp(encoding, "mbcs") == 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1296 return PyUnicode_AsMBCSString(unicode);
29706
a4fead0e24f8 Support 'mbcs' as a 'built-in' encoding, so the C API can use it without
Mark Hammond <mhammond@skippinet.com.au>
parents: 29641
diff changeset
1297 #endif
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1298 else if (strcmp(encoding, "ascii") == 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1299 return PyUnicode_AsASCIIString(unicode);
12645
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1300 }
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1301
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1302 /* Encode via the codec registry */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1303 v = PyCodec_Encode(unicode, encoding, errors);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1304 if (v == NULL)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1305 goto onError;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
1306 if (!PyString_Check(v)) {
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1307 PyErr_Format(PyExc_TypeError,
12320
5de0b939fe61 Skip Montanaro: add string precisions to calls to PyErr_Format
Guido van Rossum <guido@python.org>
parents: 12275
diff changeset
1308 "encoder did not return a string object (type=%.400s)",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43932
diff changeset
1309 Py_TYPE(v)->tp_name);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1310 Py_DECREF(v);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1311 goto onError;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1312 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1313 return v;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
1314
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1315 onError:
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1316 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1317 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1318
13837
2badb322355f This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents: 13828
diff changeset
1319 PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1320 const char *errors)
13837
2badb322355f This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents: 13828
diff changeset
1321 {
2badb322355f This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents: 13828
diff changeset
1322 PyObject *v = ((PyUnicodeObject *)unicode)->defenc;
2badb322355f This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents: 13828
diff changeset
1323
2badb322355f This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents: 13828
diff changeset
1324 if (v)
2badb322355f This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents: 13828
diff changeset
1325 return v;
2badb322355f This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents: 13828
diff changeset
1326 v = PyUnicode_AsEncodedString(unicode, NULL, errors);
2badb322355f This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents: 13828
diff changeset
1327 if (v && errors == NULL)
2badb322355f This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents: 13828
diff changeset
1328 ((PyUnicodeObject *)unicode)->defenc = v;
2badb322355f This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents: 13828
diff changeset
1329 return v;
2badb322355f This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents: 13828
diff changeset
1330 }
2badb322355f This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents: 13828
diff changeset
1331
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1332 Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1333 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1334 if (!PyUnicode_Check(unicode)) {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1335 PyErr_BadArgument();
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1336 goto onError;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1337 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1338 return PyUnicode_AS_UNICODE(unicode);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1339
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1340 onError:
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1341 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1342 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1343
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
1344 Py_ssize_t PyUnicode_GetSize(PyObject *unicode)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1345 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1346 if (!PyUnicode_Check(unicode)) {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1347 PyErr_BadArgument();
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1348 goto onError;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1349 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1350 return PyUnicode_GET_SIZE(unicode);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1351
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1352 onError:
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1353 return -1;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1354 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1355
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13635
diff changeset
1356 const char *PyUnicode_GetDefaultEncoding(void)
12645
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1357 {
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1358 return unicode_default_encoding;
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1359 }
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1360
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1361 int PyUnicode_SetDefaultEncoding(const char *encoding)
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1362 {
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1363 PyObject *v;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
1364
12645
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1365 /* Make sure the encoding is valid. As side effect, this also
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1366 loads the encoding into the codec registry cache. */
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1367 v = _PyCodec_Lookup(encoding);
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1368 if (v == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1369 goto onError;
12645
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1370 Py_DECREF(v);
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1371 strncpy(unicode_default_encoding,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1372 encoding,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1373 sizeof(unicode_default_encoding));
12645
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1374 return 0;
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1375
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1376 onError:
12645
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1377 return -1;
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1378 }
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
1379
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1380 /* error handling callback helper:
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1381 build arguments, call the callback and check the arguments,
35806
44fe157255bc fix typos, mostly in comments
Fred Drake <fdrake@acm.org>
parents: 35758
diff changeset
1382 if no exception occurred, copy the replacement to the output
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1383 and adjust various state variables.
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1384 return 0 on success, -1 on error
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1385 */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1386
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1387 static
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1388 int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1389 const char *encoding, const char *reason,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1390 const char *input, Py_ssize_t insize, Py_ssize_t *startinpos,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1391 Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1392 PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr)
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
1393 {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
1394 static char *argparse = "O!n;decoding error handler must return (unicode, int) tuple";
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1395
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1396 PyObject *restuple = NULL;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1397 PyObject *repunicode = NULL;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
1398 Py_ssize_t outsize = PyUnicode_GET_SIZE(*output);
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
1399 Py_ssize_t requiredsize;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
1400 Py_ssize_t newpos;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1401 Py_UNICODE *repptr;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
1402 Py_ssize_t repsize;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1403 int res = -1;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1404
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1405 if (*errorHandler == NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1406 *errorHandler = PyCodec_LookupError(errors);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1407 if (*errorHandler == NULL)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1408 goto onError;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1409 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1410
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1411 if (*exceptionObject == NULL) {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
1412 *exceptionObject = PyUnicodeDecodeError_Create(
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1413 encoding, input, insize, *startinpos, *endinpos, reason);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1414 if (*exceptionObject == NULL)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1415 goto onError;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1416 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1417 else {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1418 if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1419 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1420 if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1421 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1422 if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1423 goto onError;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1424 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1425
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1426 restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1427 if (restuple == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1428 goto onError;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1429 if (!PyTuple_Check(restuple)) {
51598
512c5a5de00b Fix warnings GCC emits where the argument of PyErr_Format is a single variable.
Georg Brandl <georg@python.org>
parents: 51218
diff changeset
1430 PyErr_SetString(PyExc_TypeError, &argparse[4]);
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1431 goto onError;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1432 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1433 if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos))
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1434 goto onError;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1435 if (newpos<0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1436 newpos = insize+newpos;
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27521
diff changeset
1437 if (newpos<0 || newpos>insize) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1438 PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1439 goto onError;
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27521
diff changeset
1440 }
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1441
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1442 /* need more space? (at least enough for what we
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1443 have+the replacement+the rest of the string (starting
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1444 at the new input position), so we won't have to check space
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1445 when there are no errors in the rest of the string) */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1446 repptr = PyUnicode_AS_UNICODE(repunicode);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1447 repsize = PyUnicode_GET_SIZE(repunicode);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1448 requiredsize = *outpos + repsize + insize-newpos;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1449 if (requiredsize > outsize) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1450 if (requiredsize<2*outsize)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1451 requiredsize = 2*outsize;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1452 if (_PyUnicode_Resize(output, requiredsize) < 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1453 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1454 *outptr = PyUnicode_AS_UNICODE(*output) + *outpos;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1455 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1456 *endinpos = newpos;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1457 *inptr = input + newpos;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1458 Py_UNICODE_COPY(*outptr, repptr, repsize);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1459 *outptr += repsize;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1460 *outpos += repsize;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1461 /* we made it! */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1462 res = 0;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1463
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1464 onError:
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1465 Py_XDECREF(restuple);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1466 return res;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1467 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1468
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1469 /* --- UTF-7 Codec -------------------------------------------------------- */
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1470
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1471 /* See RFC2152 for details. We encode conservatively and decode liberally. */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1472
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1473 /* Three simple macros defining base-64. */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1474
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1475 /* Is c a base-64 character? */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1476
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1477 #define IS_BASE64(c) \
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1478 (isalnum(c) || (c) == '+' || (c) == '/')
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1479
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1480 /* given that c is a base-64 character, what is its base-64 value? */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1481
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1482 #define FROM_BASE64(c) \
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1483 (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1484 ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1485 ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1486 (c) == '+' ? 62 : 63)
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1487
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1488 /* What is the base-64 character of the bottom 6 bits of n? */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1489
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1490 #define TO_BASE64(n) \
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1491 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f])
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1492
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1493 /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1494 * decoded as itself. We are permissive on decoding; the only ASCII
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1495 * byte not decoding to itself is the + which begins a base64
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1496 * string. */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1497
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1498 #define DECODE_DIRECT(c) \
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1499 ((c) <= 127 && (c) != '+')
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1500
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1501 /* The UTF-7 encoder treats ASCII characters differently according to
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1502 * whether they are Set D, Set O, Whitespace, or special (i.e. none of
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1503 * the above). See RFC2152. This array identifies these different
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1504 * sets:
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1505 * 0 : "Set D"
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1506 * alphanumeric and '(),-./:?
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1507 * 1 : "Set O"
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1508 * !"#$%&*;<=>@[]^_`{|}
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1509 * 2 : "whitespace"
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1510 * ht nl cr sp
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1511 * 3 : special (must be base64 encoded)
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1512 * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127)
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1513 */
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1514
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
1515 static
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1516 char utf7_category[128] = {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1517 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1518 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3,
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1519 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1520 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1521 /* sp ! " # $ % & ' ( ) * + , - . / */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1522 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0,
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1523 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1524 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1525 /* @ A B C D E F G H I J K L M N O */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1526 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1527 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1528 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1,
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1529 /* ` a b c d e f g h i j k l m n o */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1530 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1531 /* p q r s t u v w x y z { | } ~ del */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1532 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 3, 3,
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1533 };
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1534
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1535 /* ENCODE_DIRECT: this character should be encoded as itself. The
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1536 * answer depends on whether we are encoding set O as itself, and also
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1537 * on whether we are encoding whitespace as itself. RFC2152 makes it
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1538 * clear that the answers to these questions vary between
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1539 * applications, so this code needs to be flexible. */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1540
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1541 #define ENCODE_DIRECT(c, directO, directWS) \
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1542 ((c) < 128 && (c) > 0 && \
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1543 ((utf7_category[(c)] == 0) || \
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1544 (directWS && (utf7_category[(c)] == 2)) || \
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1545 (directO && (utf7_category[(c)] == 1))))
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1546
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1547 PyObject *PyUnicode_DecodeUTF7(const char *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1548 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1549 const char *errors)
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1550 {
43932
e8ac1ae4503c The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43888
diff changeset
1551 return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL);
e8ac1ae4503c The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43888
diff changeset
1552 }
e8ac1ae4503c The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43888
diff changeset
1553
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1554 /* The decoder. The only state we preserve is our read position,
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1555 * i.e. how many characters we have consumed. So if we end in the
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1556 * middle of a shift sequence we have to back off the read position
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1557 * and the output to the beginning of the sequence, otherwise we lose
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1558 * all the shift state (seen bits, number of bits seen, high
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1559 * surrogate). */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1560
43932
e8ac1ae4503c The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43888
diff changeset
1561 PyObject *PyUnicode_DecodeUTF7Stateful(const char *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1562 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1563 const char *errors,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1564 Py_ssize_t *consumed)
43932
e8ac1ae4503c The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43888
diff changeset
1565 {
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1566 const char *starts = s;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
1567 Py_ssize_t startinpos;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
1568 Py_ssize_t endinpos;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
1569 Py_ssize_t outpos;
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1570 const char *e;
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1571 PyUnicodeObject *unicode;
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1572 Py_UNICODE *p;
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1573 const char *errmsg = "";
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1574 int inShift = 0;
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1575 Py_UNICODE *shiftOutStart;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1576 unsigned int base64bits = 0;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1577 unsigned long base64buffer = 0;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1578 Py_UNICODE surrogate = 0;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1579 PyObject *errorHandler = NULL;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1580 PyObject *exc = NULL;
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1581
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1582 unicode = _PyUnicode_New(size);
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1583 if (!unicode)
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1584 return NULL;
43932
e8ac1ae4503c The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43888
diff changeset
1585 if (size == 0) {
e8ac1ae4503c The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43888
diff changeset
1586 if (consumed)
e8ac1ae4503c The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43888
diff changeset
1587 *consumed = 0;
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1588 return (PyObject *)unicode;
43932
e8ac1ae4503c The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43888
diff changeset
1589 }
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1590
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1591 p = unicode->str;
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1592 shiftOutStart = p;
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1593 e = s + size;
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1594
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1595 while (s < e) {
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1596 Py_UNICODE ch = (unsigned char) *s;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1597
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1598 if (inShift) { /* in a base-64 section */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1599 if (IS_BASE64(ch)) { /* consume a base-64 character */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1600 base64buffer = (base64buffer << 6) | FROM_BASE64(ch);
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1601 base64bits += 6;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1602 s++;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1603 if (base64bits >= 16) {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1604 /* we have enough bits for a UTF-16 value */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1605 Py_UNICODE outCh = (Py_UNICODE)
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1606 (base64buffer >> (base64bits-16));
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1607 base64bits -= 16;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1608 base64buffer &= (1 << base64bits) - 1; /* clear high bits */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1609 if (surrogate) {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1610 /* expecting a second surrogate */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1611 if (outCh >= 0xDC00 && outCh <= 0xDFFF) {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1612 #ifdef Py_UNICODE_WIDE
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1613 *p++ = (((surrogate & 0x3FF)<<10)
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1614 | (outCh & 0x3FF)) + 0x10000;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1615 #else
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1616 *p++ = surrogate;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1617 *p++ = outCh;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1618 #endif
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1619 surrogate = 0;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1620 }
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1621 else {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1622 surrogate = 0;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1623 errmsg = "second surrogate missing";
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1624 goto utf7Error;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1625 }
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1626 }
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1627 else if (outCh >= 0xD800 && outCh <= 0xDBFF) {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1628 /* first surrogate */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1629 surrogate = outCh;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1630 }
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1631 else if (outCh >= 0xDC00 && outCh <= 0xDFFF) {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1632 errmsg = "unexpected second surrogate";
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1633 goto utf7Error;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1634 }
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1635 else {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1636 *p++ = outCh;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1637 }
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1638 }
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1639 }
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1640 else { /* now leaving a base-64 section */
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1641 inShift = 0;
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1642 s++;
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1643 if (surrogate) {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1644 errmsg = "second surrogate missing at end of shift sequence";
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
1645 goto utf7Error;
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1646 }
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1647 if (base64bits > 0) { /* left-over bits */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1648 if (base64bits >= 6) {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1649 /* We've seen at least one base-64 character */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1650 errmsg = "partial character in shift sequence";
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1651 goto utf7Error;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1652 }
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1653 else {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1654 /* Some bits remain; they should be zero */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1655 if (base64buffer != 0) {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1656 errmsg = "non-zero padding bits in shift sequence";
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1657 goto utf7Error;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1658 }
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1659 }
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1660 }
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1661 if (ch != '-') {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1662 /* '-' is absorbed; other terminating
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1663 characters are preserved */
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1664 *p++ = ch;
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1665 }
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1666 }
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1667 }
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1668 else if ( ch == '+' ) {
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1669 startinpos = s-starts;
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1670 s++; /* consume '+' */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1671 if (s < e && *s == '-') { /* '+-' encodes '+' */
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1672 s++;
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1673 *p++ = '+';
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1674 }
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1675 else { /* begin base64-encoded section */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1676 inShift = 1;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1677 shiftOutStart = p;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1678 base64bits = 0;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1679 }
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1680 }
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1681 else if (DECODE_DIRECT(ch)) { /* character decodes as itself */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1682 *p++ = ch;
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1683 s++;
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1684 }
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1685 else {
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1686 startinpos = s-starts;
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1687 s++;
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1688 errmsg = "unexpected special character";
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1689 goto utf7Error;
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1690 }
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1691 continue;
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1692 utf7Error:
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1693 outpos = p-PyUnicode_AS_UNICODE(unicode);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1694 endinpos = s-starts;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1695 if (unicode_decode_call_errorhandler(
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1696 errors, &errorHandler,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1697 "utf7", errmsg,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1698 starts, size, &startinpos, &endinpos, &exc, &s,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1699 &unicode, &outpos, &p))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1700 goto onError;
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1701 }
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1702
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1703 /* end of string */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1704
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1705 if (inShift && !consumed) { /* in shift sequence, no more to follow */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1706 /* if we're in an inconsistent state, that's an error */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1707 if (surrogate ||
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1708 (base64bits >= 6) ||
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1709 (base64bits > 0 && base64buffer != 0)) {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1710 outpos = p-PyUnicode_AS_UNICODE(unicode);
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1711 endinpos = size;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1712 if (unicode_decode_call_errorhandler(
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1713 errors, &errorHandler,
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1714 "utf7", "unterminated shift sequence",
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1715 starts, size, &startinpos, &endinpos, &exc, &s,
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1716 &unicode, &outpos, &p))
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1717 goto onError;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1718 }
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1719 }
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1720
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1721 /* return state */
43932
e8ac1ae4503c The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43888
diff changeset
1722 if (consumed) {
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1723 if (inShift) {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1724 p = shiftOutStart; /* back off output */
43932
e8ac1ae4503c The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43888
diff changeset
1725 *consumed = startinpos;
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1726 }
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1727 else {
43932
e8ac1ae4503c The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43888
diff changeset
1728 *consumed = s-starts;
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1729 }
43932
e8ac1ae4503c The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43888
diff changeset
1730 }
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1731
30431
cd0c3f916b34 Change checks of PyUnicode_Resize() return value for clarity.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30216
diff changeset
1732 if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0)
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1733 goto onError;
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1734
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1735 Py_XDECREF(errorHandler);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1736 Py_XDECREF(exc);
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1737 return (PyObject *)unicode;
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1738
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1739 onError:
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1740 Py_XDECREF(errorHandler);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1741 Py_XDECREF(exc);
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1742 Py_DECREF(unicode);
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1743 return NULL;
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1744 }
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1745
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1746
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1747 PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1748 Py_ssize_t size,
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1749 int base64SetO,
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1750 int base64WhiteSpace,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1751 const char *errors)
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1752 {
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1753 PyObject *v;
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1754 /* It might be possible to tighten this worst case */
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1755 Py_ssize_t allocated = 5 * size;
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1756 int inShift = 0;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
1757 Py_ssize_t i = 0;
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1758 unsigned int base64bits = 0;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1759 unsigned long base64buffer = 0;
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1760 char * out;
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1761 char * start;
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1762
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1763 if (allocated / 5 != size)
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
1764 return PyErr_NoMemory();
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
1765
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1766 if (size == 0)
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
1767 return PyString_FromStringAndSize(NULL, 0);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
1768
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1769 v = PyString_FromStringAndSize(NULL, allocated);
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1770 if (v == NULL)
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1771 return NULL;
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1772
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
1773 start = out = PyString_AS_STRING(v);
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1774 for (;i < size; ++i) {
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1775 Py_UNICODE ch = s[i];
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1776
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1777 if (inShift) {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1778 if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1779 /* shifting out */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1780 if (base64bits) { /* output remaining bits */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1781 *out++ = TO_BASE64(base64buffer << (6-base64bits));
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1782 base64buffer = 0;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1783 base64bits = 0;
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1784 }
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1785 inShift = 0;
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1786 /* Characters not in the BASE64 set implicitly unshift the sequence
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1787 so no '-' is required, except if the character is itself a '-' */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1788 if (IS_BASE64(ch) || ch == '-') {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1789 *out++ = '-';
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1790 }
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1791 *out++ = (char) ch;
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1792 }
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1793 else {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1794 goto encode_char;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1795 }
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1796 }
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1797 else { /* not in a shift sequence */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1798 if (ch == '+') {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1799 *out++ = '+';
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1800 *out++ = '-';
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1801 }
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1802 else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1803 *out++ = (char) ch;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1804 }
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1805 else {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1806 *out++ = '+';
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1807 inShift = 1;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1808 goto encode_char;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
1809 }
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1810 }
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1811 continue;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1812 encode_char:
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1813 #ifdef Py_UNICODE_WIDE
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1814 if (ch >= 0x10000) {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1815 /* code first surrogate */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1816 base64bits += 16;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1817 base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10);
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1818 while (base64bits >= 6) {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1819 *out++ = TO_BASE64(base64buffer >> (base64bits-6));
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1820 base64bits -= 6;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1821 }
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1822 /* prepare second surrogate */
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1823 ch = 0xDC00 | ((ch-0x10000) & 0x3FF);
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1824 }
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1825 #endif
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1826 base64bits += 16;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1827 base64buffer = (base64buffer << 16) | ch;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1828 while (base64bits >= 6) {
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1829 *out++ = TO_BASE64(base64buffer >> (base64bits-6));
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1830 base64bits -= 6;
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1831 }
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1832 }
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1833 if (base64bits)
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1834 *out++= TO_BASE64(base64buffer << (6-base64bits) );
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1835 if (inShift)
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1836 *out++ = '-';
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1837
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
1838 _PyString_Resize(&v, out - start);
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1839 return v;
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1840 }
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1841
53740
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1842 #undef IS_BASE64
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1843 #undef FROM_BASE64
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1844 #undef TO_BASE64
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1845 #undef DECODE_DIRECT
12d8fd499722 Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53722
diff changeset
1846 #undef ENCODE_DIRECT
19998
8a0c97453df4 Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents: 19886
diff changeset
1847
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1848 /* --- UTF-8 Codec -------------------------------------------------------- */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1849
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
1850 static
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1851 char utf8_code_length[256] = {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1852 /* Map UTF-8 encoded prefix byte to sequence length. zero means
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1853 illegal prefix. see RFC 2279 for details */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1854 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1855 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1856 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1857 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1858 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1859 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1860 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1861 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1862 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1863 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1864 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1865 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1866 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1867 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1868 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1869 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1870 };
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1871
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1872 PyObject *PyUnicode_DecodeUTF8(const char *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1873 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1874 const char *errors)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1875 {
33428
57478a13fbf6 SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents: 33311
diff changeset
1876 return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL);
57478a13fbf6 SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents: 33311
diff changeset
1877 }
57478a13fbf6 SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents: 33311
diff changeset
1878
57478a13fbf6 SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents: 33311
diff changeset
1879 PyObject *PyUnicode_DecodeUTF8Stateful(const char *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1880 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1881 const char *errors,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1882 Py_ssize_t *consumed)
33428
57478a13fbf6 SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents: 33311
diff changeset
1883 {
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1884 const char *starts = s;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1885 int n;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
1886 Py_ssize_t startinpos;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
1887 Py_ssize_t endinpos;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
1888 Py_ssize_t outpos;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1889 const char *e;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1890 PyUnicodeObject *unicode;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1891 Py_UNICODE *p;
13635
dfb7ecfffe3d Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents: 13633
diff changeset
1892 const char *errmsg = "";
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1893 PyObject *errorHandler = NULL;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
1894 PyObject *exc = NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1895
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1896 /* Note: size will always be longer than the resulting Unicode
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1897 character count */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1898 unicode = _PyUnicode_New(size);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1899 if (!unicode)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1900 return NULL;
33428
57478a13fbf6 SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents: 33311
diff changeset
1901 if (size == 0) {
57478a13fbf6 SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents: 33311
diff changeset
1902 if (consumed)
57478a13fbf6 SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents: 33311
diff changeset
1903 *consumed = 0;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1904 return (PyObject *)unicode;
33428
57478a13fbf6 SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents: 33311
diff changeset
1905 }
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1906
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1907 /* Unpack UTF-8 encoded data */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1908 p = unicode->str;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1909 e = s + size;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1910
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1911 while (s < e) {
13411
9a8391054840 New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13401
diff changeset
1912 Py_UCS4 ch = (unsigned char)*s;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1913
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1914 if (ch < 0x80) {
13411
9a8391054840 New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13401
diff changeset
1915 *p++ = (Py_UNICODE)ch;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1916 s++;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1917 continue;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1918 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1919
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1920 n = utf8_code_length[ch];
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1921
13635
dfb7ecfffe3d Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents: 13633
diff changeset
1922 if (s + n > e) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1923 if (consumed)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1924 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1925 else {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1926 errmsg = "unexpected end of data";
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1927 startinpos = s-starts;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1928 endinpos = size;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1929 goto utf8Error;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1930 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1931 }
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1932
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1933 switch (n) {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1934
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1935 case 0:
13635
dfb7ecfffe3d Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents: 13633
diff changeset
1936 errmsg = "unexpected code byte";
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1937 startinpos = s-starts;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1938 endinpos = startinpos+1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1939 goto utf8Error;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1940
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1941 case 1:
13635
dfb7ecfffe3d Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents: 13633
diff changeset
1942 errmsg = "internal error";
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1943 startinpos = s-starts;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1944 endinpos = startinpos+1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1945 goto utf8Error;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1946
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1947 case 2:
13635
dfb7ecfffe3d Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents: 13633
diff changeset
1948 if ((s[1] & 0xc0) != 0x80) {
dfb7ecfffe3d Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents: 13633
diff changeset
1949 errmsg = "invalid data";
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1950 startinpos = s-starts;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1951 endinpos = startinpos+2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1952 goto utf8Error;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1953 }
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1954 ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f);
13635
dfb7ecfffe3d Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents: 13633
diff changeset
1955 if (ch < 0x80) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1956 startinpos = s-starts;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1957 endinpos = startinpos+2;
13635
dfb7ecfffe3d Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents: 13633
diff changeset
1958 errmsg = "illegal encoding";
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1959 goto utf8Error;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1960 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1961 else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1962 *p++ = (Py_UNICODE)ch;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1963 break;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1964
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1965 case 3:
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
1966 if ((s[1] & 0xc0) != 0x80 ||
13635
dfb7ecfffe3d Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents: 13633
diff changeset
1967 (s[2] & 0xc0) != 0x80) {
dfb7ecfffe3d Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents: 13633
diff changeset
1968 errmsg = "invalid data";
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1969 startinpos = s-starts;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1970 endinpos = startinpos+3;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1971 goto utf8Error;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1972 }
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1973 ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f);
21886
abedb2418f62 Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents: 21884
diff changeset
1974 if (ch < 0x0800) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1975 /* Note: UTF-8 encodings of surrogates are considered
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1976 legal UTF-8 sequences;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1977
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1978 XXX For wide builds (UCS-4) we should probably try
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1979 to recombine the surrogates into a single code
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1980 unit.
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1981 */
13635
dfb7ecfffe3d Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents: 13633
diff changeset
1982 errmsg = "illegal encoding";
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1983 startinpos = s-starts;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1984 endinpos = startinpos+3;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1985 goto utf8Error;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1986 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1987 else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1988 *p++ = (Py_UNICODE)ch;
13411
9a8391054840 New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13401
diff changeset
1989 break;
9a8391054840 New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13401
diff changeset
1990
9a8391054840 New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13401
diff changeset
1991 case 4:
9a8391054840 New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13401
diff changeset
1992 if ((s[1] & 0xc0) != 0x80 ||
9a8391054840 New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13401
diff changeset
1993 (s[2] & 0xc0) != 0x80 ||
13635
dfb7ecfffe3d Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents: 13633
diff changeset
1994 (s[3] & 0xc0) != 0x80) {
dfb7ecfffe3d Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents: 13633
diff changeset
1995 errmsg = "invalid data";
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1996 startinpos = s-starts;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1997 endinpos = startinpos+4;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1998 goto utf8Error;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
1999 }
13411
9a8391054840 New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13401
diff changeset
2000 ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) +
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2001 ((s[2] & 0x3f) << 6) + (s[3] & 0x3f);
13411
9a8391054840 New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13401
diff changeset
2002 /* validate and convert to UTF-16 */
18292
ffbb9c918ac0 Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents: 18280
diff changeset
2003 if ((ch < 0x10000) /* minimum value allowed for 4
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2004 byte encoding */
18292
ffbb9c918ac0 Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents: 18280
diff changeset
2005 || (ch > 0x10ffff)) /* maximum value allowed for
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2006 UTF-16 */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2007 {
13635
dfb7ecfffe3d Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents: 13633
diff changeset
2008 errmsg = "illegal encoding";
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2009 startinpos = s-starts;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2010 endinpos = startinpos+4;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2011 goto utf8Error;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2012 }
18324
3ba7a358ce02 use Py_UNICODE_WIDE instead of USE_UCS4_STORAGE and Py_UNICODE_SIZE
Fredrik Lundh <fredrik@pythonware.com>
parents: 18298
diff changeset
2013 #ifdef Py_UNICODE_WIDE
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2014 *p++ = (Py_UNICODE)ch;
18292
ffbb9c918ac0 Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents: 18280
diff changeset
2015 #else
13411
9a8391054840 New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13401
diff changeset
2016 /* compute and append the two surrogates: */
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
2017
13411
9a8391054840 New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13401
diff changeset
2018 /* translate from 10000..10FFFF to 0..FFFF */
9a8391054840 New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13401
diff changeset
2019 ch -= 0x10000;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
2020
13411
9a8391054840 New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13401
diff changeset
2021 /* high surrogate = top 10 bits added to D800 */
9a8391054840 New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13401
diff changeset
2022 *p++ = (Py_UNICODE)(0xD800 + (ch >> 10));
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
2023
13411
9a8391054840 New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13401
diff changeset
2024 /* low surrogate = bottom 10 bits added to DC00 */
18279
8b7ff44b4669 experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents: 18278
diff changeset
2025 *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF));
18292
ffbb9c918ac0 Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents: 18280
diff changeset
2026 #endif
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2027 break;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2028
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2029 default:
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2030 /* Other sizes are only needed for UCS-4 */
13635
dfb7ecfffe3d Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents: 13633
diff changeset
2031 errmsg = "unsupported Unicode code range";
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2032 startinpos = s-starts;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2033 endinpos = startinpos+n;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2034 goto utf8Error;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2035 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2036 s += n;
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2037 continue;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2038
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2039 utf8Error:
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2040 outpos = p-PyUnicode_AS_UNICODE(unicode);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2041 if (unicode_decode_call_errorhandler(
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2042 errors, &errorHandler,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2043 "utf8", errmsg,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2044 starts, size, &startinpos, &endinpos, &exc, &s,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2045 &unicode, &outpos, &p))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2046 goto onError;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2047 }
33428
57478a13fbf6 SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents: 33311
diff changeset
2048 if (consumed)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2049 *consumed = s-starts;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2050
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2051 /* Adjust length */
30431
cd0c3f916b34 Change checks of PyUnicode_Resize() return value for clarity.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30216
diff changeset
2052 if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2053 goto onError;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2054
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2055 Py_XDECREF(errorHandler);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2056 Py_XDECREF(exc);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2057 return (PyObject *)unicode;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2058
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2059 onError:
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2060 Py_XDECREF(errorHandler);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2061 Py_XDECREF(exc);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2062 Py_DECREF(unicode);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2063 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2064 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2065
23294
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2066 /* Allocation strategy: if the string is short, convert into a stack buffer
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2067 and allocate exactly as much space needed at the end. Else allocate the
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2068 maximum possible needed (4 result bytes per Unicode character), and return
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2069 the excess memory at the end.
23203
583c226afde6 Back out 2.140.
Martin v. Löwis <martin@v.loewis.de>
parents: 23198
diff changeset
2070 */
23198
950a22eb8cc8 PyUnicode_EncodeUTF8: squash compiler wng. The difference of two
Tim Peters <tim.peters@gmail.com>
parents: 23192
diff changeset
2071 PyObject *
950a22eb8cc8 PyUnicode_EncodeUTF8: squash compiler wng. The difference of two
Tim Peters <tim.peters@gmail.com>
parents: 23192
diff changeset
2072 PyUnicode_EncodeUTF8(const Py_UNICODE *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2073 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2074 const char *errors)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2075 {
23294
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2076 #define MAX_SHORT_UNICHARS 300 /* largest size we'll do on the stack */
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2077
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
2078 Py_ssize_t i; /* index into s of next input byte */
23294
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2079 PyObject *v; /* result string object */
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2080 char *p; /* next free byte in output buffer */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
2081 Py_ssize_t nallocated; /* number of result bytes allocated */
37662
0678ce9fb6b2 Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 37661
diff changeset
2082 Py_ssize_t nneeded; /* number of result bytes needed */
23294
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2083 char stackbuf[MAX_SHORT_UNICHARS * 4];
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2084
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2085 assert(s != NULL);
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2086 assert(size >= 0);
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2087
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2088 if (size <= MAX_SHORT_UNICHARS) {
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2089 /* Write into the stack buffer; nallocated can't overflow.
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2090 * At the end, we'll allocate exactly as much heap space as it
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2091 * turns out we need.
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2092 */
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2093 nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int);
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2094 v = NULL; /* will allocate after we're done */
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2095 p = stackbuf;
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2096 }
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2097 else {
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2098 /* Overallocate on the heap, and give the excess back at the end. */
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2099 nallocated = size * 4;
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2100 if (nallocated / 4 != size) /* overflow! */
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2101 return PyErr_NoMemory();
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
2102 v = PyString_FromStringAndSize(NULL, nallocated);
23294
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2103 if (v == NULL)
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2104 return NULL;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
2105 p = PyString_AS_STRING(v);
23294
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2106 }
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2107
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2108 for (i = 0; i < size;) {
13411
9a8391054840 New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13401
diff changeset
2109 Py_UCS4 ch = s[i++];
21882
94bac88f150f Fix for the UTF-8 memory allocation bug and the UTF-8 encoding
Marc-André Lemburg <mal@egenix.com>
parents: 21262
diff changeset
2110
23203
583c226afde6 Back out 2.140.
Martin v. Löwis <martin@v.loewis.de>
parents: 23198
diff changeset
2111 if (ch < 0x80)
23294
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2112 /* Encode ASCII */
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2113 *p++ = (char) ch;
21882
94bac88f150f Fix for the UTF-8 memory allocation bug and the UTF-8 encoding
Marc-André Lemburg <mal@egenix.com>
parents: 21262
diff changeset
2114
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2115 else if (ch < 0x0800) {
23294
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2116 /* Encode Latin-1 */
21884
04cf4e13e5c9 Cosmetics.
Marc-André Lemburg <mal@egenix.com>
parents: 21883
diff changeset
2117 *p++ = (char)(0xc0 | (ch >> 6));
04cf4e13e5c9 Cosmetics.
Marc-André Lemburg <mal@egenix.com>
parents: 21883
diff changeset
2118 *p++ = (char)(0x80 | (ch & 0x3f));
13411
9a8391054840 New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13401
diff changeset
2119 }
21882
94bac88f150f Fix for the UTF-8 memory allocation bug and the UTF-8 encoding
Marc-André Lemburg <mal@egenix.com>
parents: 21262
diff changeset
2120 else {
23294
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2121 /* Encode UCS2 Unicode ordinals */
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2122 if (ch < 0x10000) {
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2123 /* Special case: check for high surrogate */
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2124 if (0xD800 <= ch && ch <= 0xDBFF && i != size) {
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2125 Py_UCS4 ch2 = s[i];
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2126 /* Check for low surrogate and combine the two to
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2127 form a UCS4 value */
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2128 if (0xDC00 <= ch2 && ch2 <= 0xDFFF) {
23203
583c226afde6 Back out 2.140.
Martin v. Löwis <martin@v.loewis.de>
parents: 23198
diff changeset
2129 ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000;
23294
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2130 i++;
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2131 goto encodeUCS4;
13411
9a8391054840 New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13401
diff changeset
2132 }
23294
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2133 /* Fall through: handles isolated high surrogates */
13411
9a8391054840 New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13401
diff changeset
2134 }
9a8391054840 New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13401
diff changeset
2135 *p++ = (char)(0xe0 | (ch >> 12));
23294
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2136 *p++ = (char)(0x80 | ((ch >> 6) & 0x3f));
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2137 *p++ = (char)(0x80 | (ch & 0x3f));
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2138 continue;
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
2139 }
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2140 encodeUCS4:
23294
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2141 /* Encode UCS4 Unicode ordinals */
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2142 *p++ = (char)(0xf0 | (ch >> 18));
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2143 *p++ = (char)(0x80 | ((ch >> 12) & 0x3f));
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2144 *p++ = (char)(0x80 | ((ch >> 6) & 0x3f));
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2145 *p++ = (char)(0x80 | (ch & 0x3f));
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2146 }
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2147 }
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2148
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2149 if (v == NULL) {
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2150 /* This was stack allocated. */
37662
0678ce9fb6b2 Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 37661
diff changeset
2151 nneeded = p - stackbuf;
23294
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2152 assert(nneeded <= nallocated);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
2153 v = PyString_FromStringAndSize(stackbuf, nneeded);
23294
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2154 }
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2155 else {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
2156 /* Cut back to size actually needed. */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
2157 nneeded = p - PyString_AS_STRING(v);
23294
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2158 assert(nneeded <= nallocated);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
2159 _PyString_Resize(&v, nneeded);
23294
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2160 }
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2161 return v;
23203
583c226afde6 Back out 2.140.
Martin v. Löwis <martin@v.loewis.de>
parents: 23198
diff changeset
2162
23294
b735e89ccda7 SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents: 23228
diff changeset
2163 #undef MAX_SHORT_UNICHARS
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2164 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2165
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2166 PyObject *PyUnicode_AsUTF8String(PyObject *unicode)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2167 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2168 if (!PyUnicode_Check(unicode)) {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2169 PyErr_BadArgument();
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2170 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2171 }
14059
d0348a26b093 PyUnicode_AsUTF8String(): Don't need to explicitly incref str since
Barry Warsaw <barry@python.org>
parents: 13966
diff changeset
2172 return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode),
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2173 PyUnicode_GET_SIZE(unicode),
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2174 NULL);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2175 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2176
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2177 /* --- UTF-32 Codec ------------------------------------------------------- */
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2178
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2179 PyObject *
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2180 PyUnicode_DecodeUTF32(const char *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2181 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2182 const char *errors,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2183 int *byteorder)
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2184 {
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2185 return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL);
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2186 }
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2187
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2188 PyObject *
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2189 PyUnicode_DecodeUTF32Stateful(const char *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2190 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2191 const char *errors,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2192 int *byteorder,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2193 Py_ssize_t *consumed)
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2194 {
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2195 const char *starts = s;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2196 Py_ssize_t startinpos;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2197 Py_ssize_t endinpos;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2198 Py_ssize_t outpos;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2199 PyUnicodeObject *unicode;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2200 Py_UNICODE *p;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2201 #ifndef Py_UNICODE_WIDE
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2202 int i, pairs;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2203 #else
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2204 const int pairs = 0;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2205 #endif
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2206 const unsigned char *q, *e;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2207 int bo = 0; /* assume native ordering by default */
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2208 const char *errmsg = "";
42650
f2f0e95a797b Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents: 42649
diff changeset
2209 /* Offsets from q for retrieving bytes in the right order. */
f2f0e95a797b Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents: 42649
diff changeset
2210 #ifdef BYTEORDER_IS_LITTLE_ENDIAN
f2f0e95a797b Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents: 42649
diff changeset
2211 int iorder[] = {0, 1, 2, 3};
f2f0e95a797b Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents: 42649
diff changeset
2212 #else
f2f0e95a797b Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents: 42649
diff changeset
2213 int iorder[] = {3, 2, 1, 0};
f2f0e95a797b Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents: 42649
diff changeset
2214 #endif
42652
9c4f5b1d6b9d Move another variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents: 42650
diff changeset
2215 PyObject *errorHandler = NULL;
9c4f5b1d6b9d Move another variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents: 42650
diff changeset
2216 PyObject *exc = NULL;
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2217 /* On narrow builds we split characters outside the BMP into two
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2218 codepoints => count how much extra space we need. */
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2219 #ifndef Py_UNICODE_WIDE
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2220 for (i = pairs = 0; i < size/4; i++)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2221 if (((Py_UCS4 *)s)[i] >= 0x10000)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2222 pairs++;
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2223 #endif
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2224
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2225 /* This might be one to much, because of a BOM */
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2226 unicode = _PyUnicode_New((size+3)/4+pairs);
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2227 if (!unicode)
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2228 return NULL;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2229 if (size == 0)
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2230 return (PyObject *)unicode;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2231
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2232 /* Unpack UTF-32 encoded data */
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2233 p = unicode->str;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2234 q = (unsigned char *)s;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2235 e = q + size;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2236
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2237 if (byteorder)
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2238 bo = *byteorder;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2239
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2240 /* Check for BOM marks (U+FEFF) in the input and adjust current
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2241 byte order setting accordingly. In native mode, the leading BOM
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2242 mark is skipped, in all other modes, it is copied to the output
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2243 stream as-is (giving a ZWNBSP character). */
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2244 if (bo == 0) {
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2245 if (size >= 4) {
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2246 const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) |
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2247 (q[iorder[1]] << 8) | q[iorder[0]];
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2248 #ifdef BYTEORDER_IS_LITTLE_ENDIAN
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2249 if (bom == 0x0000FEFF) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2250 q += 4;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2251 bo = -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2252 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2253 else if (bom == 0xFFFE0000) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2254 q += 4;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2255 bo = 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2256 }
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2257 #else
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2258 if (bom == 0x0000FEFF) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2259 q += 4;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2260 bo = 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2261 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2262 else if (bom == 0xFFFE0000) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2263 q += 4;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2264 bo = -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2265 }
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2266 #endif
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2267 }
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2268 }
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2269
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2270 if (bo == -1) {
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2271 /* force LE */
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2272 iorder[0] = 0;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2273 iorder[1] = 1;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2274 iorder[2] = 2;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2275 iorder[3] = 3;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2276 }
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2277 else if (bo == 1) {
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2278 /* force BE */
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2279 iorder[0] = 3;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2280 iorder[1] = 2;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2281 iorder[2] = 1;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2282 iorder[3] = 0;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2283 }
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2284
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2285 while (q < e) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2286 Py_UCS4 ch;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2287 /* remaining bytes at the end? (size should be divisible by 4) */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2288 if (e-q<4) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2289 if (consumed)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2290 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2291 errmsg = "truncated data";
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2292 startinpos = ((const char *)q)-starts;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2293 endinpos = ((const char *)e)-starts;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2294 goto utf32Error;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2295 /* The remaining input chars are ignored if the callback
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2296 chooses to skip the input */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2297 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2298 ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) |
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2299 (q[iorder[1]] << 8) | q[iorder[0]];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2300
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2301 if (ch >= 0x110000)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2302 {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2303 errmsg = "codepoint not in range(0x110000)";
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2304 startinpos = ((const char *)q)-starts;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2305 endinpos = startinpos+4;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2306 goto utf32Error;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2307 }
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2308 #ifndef Py_UNICODE_WIDE
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2309 if (ch >= 0x10000)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2310 {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2311 *p++ = 0xD800 | ((ch-0x10000) >> 10);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2312 *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2313 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2314 else
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2315 #endif
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2316 *p++ = ch;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2317 q += 4;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2318 continue;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2319 utf32Error:
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2320 outpos = p-PyUnicode_AS_UNICODE(unicode);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2321 if (unicode_decode_call_errorhandler(
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2322 errors, &errorHandler,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2323 "utf32", errmsg,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2324 starts, size, &startinpos, &endinpos, &exc, &s,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2325 &unicode, &outpos, &p))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2326 goto onError;
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2327 }
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2328
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2329 if (byteorder)
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2330 *byteorder = bo;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2331
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2332 if (consumed)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2333 *consumed = (const char *)q-starts;
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2334
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2335 /* Adjust length */
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2336 if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0)
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2337 goto onError;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2338
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2339 Py_XDECREF(errorHandler);
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2340 Py_XDECREF(exc);
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2341 return (PyObject *)unicode;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2342
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2343 onError:
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2344 Py_DECREF(unicode);
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2345 Py_XDECREF(errorHandler);
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2346 Py_XDECREF(exc);
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2347 return NULL;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2348 }
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2349
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2350 PyObject *
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2351 PyUnicode_EncodeUTF32(const Py_UNICODE *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2352 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2353 const char *errors,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2354 int byteorder)
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2355 {
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2356 PyObject *v;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2357 unsigned char *p;
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
2358 Py_ssize_t nsize, bytesize;
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2359 #ifndef Py_UNICODE_WIDE
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
2360 Py_ssize_t i, pairs;
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2361 #else
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2362 const int pairs = 0;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2363 #endif
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2364 /* Offsets from p for storing byte pairs in the right order. */
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2365 #ifdef BYTEORDER_IS_LITTLE_ENDIAN
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2366 int iorder[] = {0, 1, 2, 3};
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2367 #else
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2368 int iorder[] = {3, 2, 1, 0};
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2369 #endif
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2370
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2371 #define STORECHAR(CH) \
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2372 do { \
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2373 p[iorder[3]] = ((CH) >> 24) & 0xff; \
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2374 p[iorder[2]] = ((CH) >> 16) & 0xff; \
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2375 p[iorder[1]] = ((CH) >> 8) & 0xff; \
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2376 p[iorder[0]] = (CH) & 0xff; \
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2377 p += 4; \
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2378 } while(0)
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2379
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2380 /* In narrow builds we can output surrogate pairs as one codepoint,
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2381 so we need less space. */
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2382 #ifndef Py_UNICODE_WIDE
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2383 for (i = pairs = 0; i < size-1; i++)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2384 if (0xD800 <= s[i] && s[i] <= 0xDBFF &&
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2385 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2386 pairs++;
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2387 #endif
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
2388 nsize = (size - pairs + (byteorder == 0));
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
2389 bytesize = nsize * 4;
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
2390 if (bytesize / 4 != nsize)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2391 return PyErr_NoMemory();
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
2392 v = PyString_FromStringAndSize(NULL, bytesize);
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2393 if (v == NULL)
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2394 return NULL;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2395
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
2396 p = (unsigned char *)PyString_AS_STRING(v);
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2397 if (byteorder == 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2398 STORECHAR(0xFEFF);
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2399 if (size == 0)
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2400 return v;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2401
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2402 if (byteorder == -1) {
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2403 /* force LE */
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2404 iorder[0] = 0;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2405 iorder[1] = 1;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2406 iorder[2] = 2;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2407 iorder[3] = 3;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2408 }
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2409 else if (byteorder == 1) {
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2410 /* force BE */
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2411 iorder[0] = 3;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2412 iorder[1] = 2;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2413 iorder[2] = 1;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2414 iorder[3] = 0;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2415 }
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2416
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2417 while (size-- > 0) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2418 Py_UCS4 ch = *s++;
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2419 #ifndef Py_UNICODE_WIDE
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2420 if (0xD800 <= ch && ch <= 0xDBFF && size > 0) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2421 Py_UCS4 ch2 = *s;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2422 if (0xDC00 <= ch2 && ch2 <= 0xDFFF) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2423 ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2424 s++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2425 size--;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2426 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2427 }
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2428 #endif
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2429 STORECHAR(ch);
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2430 }
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2431 return v;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2432 #undef STORECHAR
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2433 }
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2434
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2435 PyObject *PyUnicode_AsUTF32String(PyObject *unicode)
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2436 {
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2437 if (!PyUnicode_Check(unicode)) {
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2438 PyErr_BadArgument();
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2439 return NULL;
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2440 }
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2441 return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode),
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2442 PyUnicode_GET_SIZE(unicode),
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2443 NULL,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2444 0);
42649
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2445 }
c916731321c0 Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents: 42402
diff changeset
2446
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2447 /* --- UTF-16 Codec ------------------------------------------------------- */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2448
19121
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2449 PyObject *
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2450 PyUnicode_DecodeUTF16(const char *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2451 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2452 const char *errors,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2453 int *byteorder)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2454 {
33428
57478a13fbf6 SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents: 33311
diff changeset
2455 return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL);
57478a13fbf6 SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents: 33311
diff changeset
2456 }
57478a13fbf6 SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents: 33311
diff changeset
2457
57478a13fbf6 SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents: 33311
diff changeset
2458 PyObject *
57478a13fbf6 SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents: 33311
diff changeset
2459 PyUnicode_DecodeUTF16Stateful(const char *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2460 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2461 const char *errors,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2462 int *byteorder,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2463 Py_ssize_t *consumed)
33428
57478a13fbf6 SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents: 33311
diff changeset
2464 {
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2465 const char *starts = s;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
2466 Py_ssize_t startinpos;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
2467 Py_ssize_t endinpos;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
2468 Py_ssize_t outpos;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2469 PyUnicodeObject *unicode;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2470 Py_UNICODE *p;
19121
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2471 const unsigned char *q, *e;
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2472 int bo = 0; /* assume native ordering by default */
13635
dfb7ecfffe3d Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents: 13633
diff changeset
2473 const char *errmsg = "";
19121
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2474 /* Offsets from q for retrieving byte pairs in the right order. */
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2475 #ifdef BYTEORDER_IS_LITTLE_ENDIAN
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2476 int ihi = 1, ilo = 0;
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2477 #else
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2478 int ihi = 0, ilo = 1;
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2479 #endif
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2480 PyObject *errorHandler = NULL;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2481 PyObject *exc = NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2482
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2483 /* Note: size will always be longer than the resulting Unicode
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2484 character count */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2485 unicode = _PyUnicode_New(size);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2486 if (!unicode)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2487 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2488 if (size == 0)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2489 return (PyObject *)unicode;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2490
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2491 /* Unpack UTF-16 encoded data */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2492 p = unicode->str;
19121
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2493 q = (unsigned char *)s;
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2494 e = q + size;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2495
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2496 if (byteorder)
19121
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2497 bo = *byteorder;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2498
17887
fd5f83188778 This patch changes the behaviour of the UTF-16 codec family. Only the
Marc-André Lemburg <mal@egenix.com>
parents: 17700
diff changeset
2499 /* Check for BOM marks (U+FEFF) in the input and adjust current
fd5f83188778 This patch changes the behaviour of the UTF-16 codec family. Only the
Marc-André Lemburg <mal@egenix.com>
parents: 17700
diff changeset
2500 byte order setting accordingly. In native mode, the leading BOM
fd5f83188778 This patch changes the behaviour of the UTF-16 codec family. Only the
Marc-André Lemburg <mal@egenix.com>
parents: 17700
diff changeset
2501 mark is skipped, in all other modes, it is copied to the output
fd5f83188778 This patch changes the behaviour of the UTF-16 codec family. Only the
Marc-André Lemburg <mal@egenix.com>
parents: 17700
diff changeset
2502 stream as-is (giving a ZWNBSP character). */
fd5f83188778 This patch changes the behaviour of the UTF-16 codec family. Only the
Marc-André Lemburg <mal@egenix.com>
parents: 17700
diff changeset
2503 if (bo == 0) {
33428
57478a13fbf6 SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents: 33311
diff changeset
2504 if (size >= 2) {
57478a13fbf6 SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents: 33311
diff changeset
2505 const Py_UNICODE bom = (q[ihi] << 8) | q[ilo];
17887
fd5f83188778 This patch changes the behaviour of the UTF-16 codec family. Only the
Marc-André Lemburg <mal@egenix.com>
parents: 17700
diff changeset
2506 #ifdef BYTEORDER_IS_LITTLE_ENDIAN
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2507 if (bom == 0xFEFF) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2508 q += 2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2509 bo = -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2510 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2511 else if (bom == 0xFFFE) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2512 q += 2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2513 bo = 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2514 }
33428
57478a13fbf6 SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents: 33311
diff changeset
2515 #else
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2516 if (bom == 0xFEFF) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2517 q += 2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2518 bo = 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2519 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2520 else if (bom == 0xFFFE) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2521 q += 2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2522 bo = -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2523 }
33428
57478a13fbf6 SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents: 33311
diff changeset
2524 #endif
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2525 }
17887
fd5f83188778 This patch changes the behaviour of the UTF-16 codec family. Only the
Marc-André Lemburg <mal@egenix.com>
parents: 17700
diff changeset
2526 }
19121
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2527
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2528 if (bo == -1) {
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2529 /* force LE */
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2530 ihi = 1;
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2531 ilo = 0;
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2532 }
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2533 else if (bo == 1) {
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2534 /* force BE */
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2535 ihi = 0;
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2536 ilo = 1;
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2537 }
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2538
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2539 while (q < e) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2540 Py_UNICODE ch;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2541 /* remaining bytes at the end? (size should be even) */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2542 if (e-q<2) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2543 if (consumed)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2544 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2545 errmsg = "truncated data";
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2546 startinpos = ((const char *)q)-starts;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2547 endinpos = ((const char *)e)-starts;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2548 goto utf16Error;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2549 /* The remaining input chars are ignored if the callback
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2550 chooses to skip the input */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2551 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2552 ch = (q[ihi] << 8) | q[ilo];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2553
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
2554 q += 2;
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2555
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2556 if (ch < 0xD800 || ch > 0xDFFF) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2557 *p++ = ch;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2558 continue;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2559 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2560
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2561 /* UTF-16 code pair: */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2562 if (q >= e) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2563 errmsg = "unexpected end of data";
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2564 startinpos = (((const char *)q)-2)-starts;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2565 endinpos = ((const char *)e)-starts;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2566 goto utf16Error;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2567 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2568 if (0xD800 <= ch && ch <= 0xDBFF) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2569 Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2570 q += 2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2571 if (0xDC00 <= ch2 && ch2 <= 0xDFFF) {
18324
3ba7a358ce02 use Py_UNICODE_WIDE instead of USE_UCS4_STORAGE and Py_UNICODE_SIZE
Fredrik Lundh <fredrik@pythonware.com>
parents: 18298
diff changeset
2572 #ifndef Py_UNICODE_WIDE
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2573 *p++ = ch;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2574 *p++ = ch2;
18292
ffbb9c918ac0 Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents: 18280
diff changeset
2575 #else
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2576 *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000;
18774
57684d3cefce Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents: 18773
diff changeset
2577 #endif
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2578 continue;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2579 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2580 else {
18292
ffbb9c918ac0 Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents: 18280
diff changeset
2581 errmsg = "illegal UTF-16 surrogate";
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2582 startinpos = (((const char *)q)-4)-starts;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2583 endinpos = startinpos+2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2584 goto utf16Error;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2585 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2586
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2587 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2588 errmsg = "illegal encoding";
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2589 startinpos = (((const char *)q)-2)-starts;
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
2590 endinpos = startinpos+2;
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2591 /* Fall through to report the error */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2592
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2593 utf16Error:
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2594 outpos = p-PyUnicode_AS_UNICODE(unicode);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2595 if (unicode_decode_call_errorhandler(
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2596 errors, &errorHandler,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2597 "utf16", errmsg,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2598 starts, size, &startinpos, &endinpos, &exc, (const char **)&q,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2599 &unicode, &outpos, &p))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2600 goto onError;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2601 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2602
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2603 if (byteorder)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2604 *byteorder = bo;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2605
33428
57478a13fbf6 SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents: 33311
diff changeset
2606 if (consumed)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2607 *consumed = (const char *)q-starts;
33428
57478a13fbf6 SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents: 33311
diff changeset
2608
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2609 /* Adjust length */
30431
cd0c3f916b34 Change checks of PyUnicode_Resize() return value for clarity.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30216
diff changeset
2610 if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2611 goto onError;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2612
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2613 Py_XDECREF(errorHandler);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2614 Py_XDECREF(exc);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2615 return (PyObject *)unicode;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2616
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2617 onError:
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2618 Py_DECREF(unicode);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2619 Py_XDECREF(errorHandler);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2620 Py_XDECREF(exc);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2621 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2622 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2623
19121
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2624 PyObject *
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2625 PyUnicode_EncodeUTF16(const Py_UNICODE *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2626 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2627 const char *errors,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2628 int byteorder)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2629 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2630 PyObject *v;
19121
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2631 unsigned char *p;
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
2632 Py_ssize_t nsize, bytesize;
31238
4edf200a8118 SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31199
diff changeset
2633 #ifdef Py_UNICODE_WIDE
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
2634 Py_ssize_t i, pairs;
31238
4edf200a8118 SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31199
diff changeset
2635 #else
4edf200a8118 SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31199
diff changeset
2636 const int pairs = 0;
4edf200a8118 SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31199
diff changeset
2637 #endif
19121
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2638 /* Offsets from p for storing byte pairs in the right order. */
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2639 #ifdef BYTEORDER_IS_LITTLE_ENDIAN
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2640 int ihi = 1, ilo = 0;
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2641 #else
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2642 int ihi = 0, ilo = 1;
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2643 #endif
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2644
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2645 #define STORECHAR(CH) \
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2646 do { \
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2647 p[ihi] = ((CH) >> 8) & 0xff; \
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2648 p[ilo] = (CH) & 0xff; \
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2649 p += 2; \
19121
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2650 } while(0)
18292
ffbb9c918ac0 Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents: 18280
diff changeset
2651
31238
4edf200a8118 SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31199
diff changeset
2652 #ifdef Py_UNICODE_WIDE
18292
ffbb9c918ac0 Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents: 18280
diff changeset
2653 for (i = pairs = 0; i < size; i++)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2654 if (s[i] >= 0x10000)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2655 pairs++;
31238
4edf200a8118 SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31199
diff changeset
2656 #endif
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
2657 /* 2 * (size + pairs + (byteorder == 0)) */
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
2658 if (size > PY_SSIZE_T_MAX ||
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
2659 size > PY_SSIZE_T_MAX - pairs - (byteorder == 0))
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2660 return PyErr_NoMemory();
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
2661 nsize = size + pairs + (byteorder == 0);
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
2662 bytesize = nsize * 2;
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
2663 if (bytesize / 2 != nsize)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2664 return PyErr_NoMemory();
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
2665 v = PyString_FromStringAndSize(NULL, bytesize);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2666 if (v == NULL)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2667 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2668
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
2669 p = (unsigned char *)PyString_AS_STRING(v);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2670 if (byteorder == 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2671 STORECHAR(0xFEFF);
13396
759f698740cc Fix to bug #393 (UTF16 codec didn't like empty strings) and
Marc-André Lemburg <mal@egenix.com>
parents: 13394
diff changeset
2672 if (size == 0)
13966
91ddeed12fa7 Fixed a couple of instances where a 0-length string was being
Marc-André Lemburg <mal@egenix.com>
parents: 13946
diff changeset
2673 return v;
19121
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2674
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2675 if (byteorder == -1) {
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2676 /* force LE */
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2677 ihi = 1;
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2678 ilo = 0;
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2679 }
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2680 else if (byteorder == 1) {
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2681 /* force BE */
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2682 ihi = 0;
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2683 ilo = 1;
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2684 }
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2685
18292
ffbb9c918ac0 Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents: 18280
diff changeset
2686 while (size-- > 0) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2687 Py_UNICODE ch = *s++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2688 Py_UNICODE ch2 = 0;
31238
4edf200a8118 SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31199
diff changeset
2689 #ifdef Py_UNICODE_WIDE
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2690 if (ch >= 0x10000) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2691 ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2692 ch = 0xD800 | ((ch-0x10000) >> 10);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2693 }
31238
4edf200a8118 SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31199
diff changeset
2694 #endif
19121
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2695 STORECHAR(ch);
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2696 if (ch2)
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2697 STORECHAR(ch2);
18292
ffbb9c918ac0 Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents: 18280
diff changeset
2698 }
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2699 return v;
19121
77e405f3a942 SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
2700 #undef STORECHAR
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2701 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2702
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2703 PyObject *PyUnicode_AsUTF16String(PyObject *unicode)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2704 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2705 if (!PyUnicode_Check(unicode)) {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2706 PyErr_BadArgument();
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2707 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2708 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2709 return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode),
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2710 PyUnicode_GET_SIZE(unicode),
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2711 NULL,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2712 0);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2713 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2714
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2715 /* --- Unicode Escape Codec ----------------------------------------------- */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2716
16200
3484670c37f2 Move uchhash functionality into unicodedata (after the recent
Fredrik Lundh <fredrik@pythonware.com>
parents: 16064
diff changeset
2717 static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL;
13063
eb243443b884 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 13049
diff changeset
2718
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2719 PyObject *PyUnicode_DecodeUnicodeEscape(const char *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2720 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2721 const char *errors)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2722 {
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2723 const char *starts = s;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
2724 Py_ssize_t startinpos;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
2725 Py_ssize_t endinpos;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
2726 Py_ssize_t outpos;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2727 int i;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2728 PyUnicodeObject *v;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2729 Py_UNICODE *p;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2730 const char *end;
16667
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2731 char* message;
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2732 Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2733 PyObject *errorHandler = NULL;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2734 PyObject *exc = NULL;
16667
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2735
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2736 /* Escaped strings will always be longer than the resulting
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2737 Unicode string, so we start with size here and then reduce the
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2738 length after conversion to the true value.
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2739 (but if the error callback returns a long replacement string
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2740 we'll have to allocate more space) */
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2741 v = _PyUnicode_New(size);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2742 if (v == NULL)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2743 goto onError;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2744 if (size == 0)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2745 return (PyObject *)v;
16667
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2746
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2747 p = PyUnicode_AS_UNICODE(v);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2748 end = s + size;
16667
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2749
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2750 while (s < end) {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2751 unsigned char c;
13396
759f698740cc Fix to bug #393 (UTF16 codec didn't like empty strings) and
Marc-André Lemburg <mal@egenix.com>
parents: 13394
diff changeset
2752 Py_UNICODE x;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2753 int digits;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2754
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2755 /* Non-escape characters are interpreted as Unicode ordinals */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2756 if (*s != '\\') {
16667
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2757 *p++ = (unsigned char) *s++;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2758 continue;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2759 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2760
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2761 startinpos = s-starts;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2762 /* \ - Escapes */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2763 s++;
43710
124cbf1a0e3e Backport fixes for the code that decodes octal escapes (and for PyString
Guido van Rossum <guido@python.org>
parents: 43053
diff changeset
2764 c = *s++;
124cbf1a0e3e Backport fixes for the code that decodes octal escapes (and for PyString
Guido van Rossum <guido@python.org>
parents: 43053
diff changeset
2765 if (s > end)
124cbf1a0e3e Backport fixes for the code that decodes octal escapes (and for PyString
Guido van Rossum <guido@python.org>
parents: 43053
diff changeset
2766 c = '\0'; /* Invalid after \ */
124cbf1a0e3e Backport fixes for the code that decodes octal escapes (and for PyString
Guido van Rossum <guido@python.org>
parents: 43053
diff changeset
2767 switch (c) {
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2768
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2769 /* \x escapes */
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2770 case '\n': break;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2771 case '\\': *p++ = '\\'; break;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2772 case '\'': *p++ = '\''; break;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2773 case '\"': *p++ = '\"'; break;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2774 case 'b': *p++ = '\b'; break;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2775 case 'f': *p++ = '\014'; break; /* FF */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2776 case 't': *p++ = '\t'; break;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2777 case 'n': *p++ = '\n'; break;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2778 case 'r': *p++ = '\r'; break;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2779 case 'v': *p++ = '\013'; break; /* VT */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2780 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2781
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2782 /* \OOO (octal) escapes */
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2783 case '0': case '1': case '2': case '3':
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2784 case '4': case '5': case '6': case '7':
12544
599bfa4ffd49 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12511
diff changeset
2785 x = s[-1] - '0';
43710
124cbf1a0e3e Backport fixes for the code that decodes octal escapes (and for PyString
Guido van Rossum <guido@python.org>
parents: 43053
diff changeset
2786 if (s < end && '0' <= *s && *s <= '7') {
12544
599bfa4ffd49 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12511
diff changeset
2787 x = (x<<3) + *s++ - '0';
43710
124cbf1a0e3e Backport fixes for the code that decodes octal escapes (and for PyString
Guido van Rossum <guido@python.org>
parents: 43053
diff changeset
2788 if (s < end && '0' <= *s && *s <= '7')
12544
599bfa4ffd49 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12511
diff changeset
2789 x = (x<<3) + *s++ - '0';
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2790 }
12544
599bfa4ffd49 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12511
diff changeset
2791 *p++ = x;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2792 break;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2793
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2794 /* hex escapes */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2795 /* \xXX */
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2796 case 'x':
16667
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2797 digits = 2;
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2798 message = "truncated \\xXX escape";
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2799 goto hexescape;
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2800
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2801 /* \uXXXX */
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2802 case 'u':
16667
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2803 digits = 4;
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2804 message = "truncated \\uXXXX escape";
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2805 goto hexescape;
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2806
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2807 /* \UXXXXXXXX */
16667
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2808 case 'U':
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2809 digits = 8;
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2810 message = "truncated \\UXXXXXXXX escape";
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2811 hexescape:
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2812 chr = 0;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2813 outpos = p-PyUnicode_AS_UNICODE(v);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2814 if (s+digits>end) {
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2815 endinpos = size;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2816 if (unicode_decode_call_errorhandler(
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2817 errors, &errorHandler,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2818 "unicodeescape", "end of string in escape sequence",
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2819 starts, size, &startinpos, &endinpos, &exc, &s,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2820 &v, &outpos, &p))
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2821 goto onError;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2822 goto nextByte;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2823 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2824 for (i = 0; i < digits; ++i) {
16667
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2825 c = (unsigned char) s[i];
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2826 if (!isxdigit(c)) {
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2827 endinpos = (s+i+1)-starts;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2828 if (unicode_decode_call_errorhandler(
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2829 errors, &errorHandler,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2830 "unicodeescape", message,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2831 starts, size, &startinpos, &endinpos, &exc, &s,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2832 &v, &outpos, &p))
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2833 goto onError;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2834 goto nextByte;
14388
ef2714bb0730 changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 14067
diff changeset
2835 }
ef2714bb0730 changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 14067
diff changeset
2836 chr = (chr<<4) & ~0xF;
ef2714bb0730 changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 14067
diff changeset
2837 if (c >= '0' && c <= '9')
ef2714bb0730 changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 14067
diff changeset
2838 chr += c - '0';
ef2714bb0730 changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 14067
diff changeset
2839 else if (c >= 'a' && c <= 'f')
ef2714bb0730 changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 14067
diff changeset
2840 chr += 10 + c - 'a';
ef2714bb0730 changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 14067
diff changeset
2841 else
ef2714bb0730 changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 14067
diff changeset
2842 chr += 10 + c - 'A';
ef2714bb0730 changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 14067
diff changeset
2843 }
ef2714bb0730 changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 14067
diff changeset
2844 s += i;
30653
3b47d6057151 Fix for SF bug [ 817156 ] invalid \U escape gives 0=length unistr.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30441
diff changeset
2845 if (chr == 0xffffffff && PyErr_Occurred())
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2846 /* _decoding_error will have already written into the
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2847 target buffer. */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2848 break;
16667
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2849 store:
14388
ef2714bb0730 changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 14067
diff changeset
2850 /* when we get here, chr is a 32-bit unicode character */
ef2714bb0730 changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 14067
diff changeset
2851 if (chr <= 0xffff)
ef2714bb0730 changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 14067
diff changeset
2852 /* UCS-2 character */
ef2714bb0730 changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 14067
diff changeset
2853 *p++ = (Py_UNICODE) chr;
ef2714bb0730 changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 14067
diff changeset
2854 else if (chr <= 0x10ffff) {
18774
57684d3cefce Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents: 18773
diff changeset
2855 /* UCS-4 character. Either store directly, or as
22521
795bdd692176 Fix whitespace.
Walter Dörwald <walter@livinglogic.de>
parents: 22446
diff changeset
2856 surrogate pair. */
18324
3ba7a358ce02 use Py_UNICODE_WIDE instead of USE_UCS4_STORAGE and Py_UNICODE_SIZE
Fredrik Lundh <fredrik@pythonware.com>
parents: 18298
diff changeset
2857 #ifdef Py_UNICODE_WIDE
18292
ffbb9c918ac0 Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents: 18280
diff changeset
2858 *p++ = chr;
ffbb9c918ac0 Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents: 18280
diff changeset
2859 #else
14388
ef2714bb0730 changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 14067
diff changeset
2860 chr -= 0x10000L;
ef2714bb0730 changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 14067
diff changeset
2861 *p++ = 0xD800 + (Py_UNICODE) (chr >> 10);
18279
8b7ff44b4669 experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents: 18278
diff changeset
2862 *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF);
18292
ffbb9c918ac0 Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents: 18280
diff changeset
2863 #endif
14388
ef2714bb0730 changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 14067
diff changeset
2864 } else {
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2865 endinpos = s-starts;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2866 outpos = p-PyUnicode_AS_UNICODE(v);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2867 if (unicode_decode_call_errorhandler(
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2868 errors, &errorHandler,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2869 "unicodeescape", "illegal Unicode character",
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2870 starts, size, &startinpos, &endinpos, &exc, &s,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2871 &v, &outpos, &p))
14388
ef2714bb0730 changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 14067
diff changeset
2872 goto onError;
ef2714bb0730 changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 14067
diff changeset
2873 }
16667
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2874 break;
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2875
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2876 /* \N{name} */
16667
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2877 case 'N':
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2878 message = "malformed \\N character escape";
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2879 if (ucnhash_CAPI == NULL) {
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2880 /* load the unicode data module */
36877
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36675
diff changeset
2881 PyObject *m, *api;
44439
0cbe94bea2c7 Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
2882 m = PyImport_ImportModuleNoBlock("unicodedata");
16667
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2883 if (m == NULL)
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2884 goto ucnhashError;
36877
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36675
diff changeset
2885 api = PyObject_GetAttrString(m, "ucnhash_CAPI");
16667
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2886 Py_DECREF(m);
36877
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36675
diff changeset
2887 if (api == NULL)
16667
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2888 goto ucnhashError;
37596
1b078493d538 More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37306
diff changeset
2889 ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api);
36877
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36675
diff changeset
2890 Py_DECREF(api);
16667
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2891 if (ucnhash_CAPI == NULL)
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2892 goto ucnhashError;
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2893 }
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2894 if (*s == '{') {
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2895 const char *start = s+1;
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2896 /* look for the closing brace */
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2897 while (*s != '}' && s < end)
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2898 s++;
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2899 if (s > start && s < end && *s == '}') {
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2900 /* found a name. look it up in the unicode database */
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2901 message = "unknown Unicode character name";
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2902 s++;
36924
48e0a5e39403 Update Unicode database to Unicode 4.1.
Martin v. Löwis <martin@v.loewis.de>
parents: 36882
diff changeset
2903 if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr))
16667
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2904 goto store;
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2905 }
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2906 }
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2907 endinpos = s-starts;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2908 outpos = p-PyUnicode_AS_UNICODE(v);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2909 if (unicode_decode_call_errorhandler(
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2910 errors, &errorHandler,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2911 "unicodeescape", message,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2912 starts, size, &startinpos, &endinpos, &exc, &s,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2913 &v, &outpos, &p))
16667
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2914 goto onError;
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2915 break;
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2916
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2917 default:
22521
795bdd692176 Fix whitespace.
Walter Dörwald <walter@livinglogic.de>
parents: 22446
diff changeset
2918 if (s > end) {
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2919 message = "\\ at end of string";
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2920 s--;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2921 endinpos = s-starts;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2922 outpos = p-PyUnicode_AS_UNICODE(v);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2923 if (unicode_decode_call_errorhandler(
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2924 errors, &errorHandler,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2925 "unicodeescape", message,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2926 starts, size, &startinpos, &endinpos, &exc, &s,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2927 &v, &outpos, &p))
22521
795bdd692176 Fix whitespace.
Walter Dörwald <walter@livinglogic.de>
parents: 22446
diff changeset
2928 goto onError;
795bdd692176 Fix whitespace.
Walter Dörwald <walter@livinglogic.de>
parents: 22446
diff changeset
2929 }
795bdd692176 Fix whitespace.
Walter Dörwald <walter@livinglogic.de>
parents: 22446
diff changeset
2930 else {
795bdd692176 Fix whitespace.
Walter Dörwald <walter@livinglogic.de>
parents: 22446
diff changeset
2931 *p++ = '\\';
795bdd692176 Fix whitespace.
Walter Dörwald <walter@livinglogic.de>
parents: 22446
diff changeset
2932 *p++ = (unsigned char)s[-1];
795bdd692176 Fix whitespace.
Walter Dörwald <walter@livinglogic.de>
parents: 22446
diff changeset
2933 }
16667
725238293869 reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents: 16332
diff changeset
2934 break;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2935 }
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2936 nextByte:
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2937 ;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2938 }
37662
0678ce9fb6b2 Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 37661
diff changeset
2939 if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2940 goto onError;
30160
3329b058db20 Fix another refcounting leak (in PyUnicode_DecodeUnicodeEscape()).
Walter Dörwald <walter@livinglogic.de>
parents: 30149
diff changeset
2941 Py_XDECREF(errorHandler);
3329b058db20 Fix another refcounting leak (in PyUnicode_DecodeUnicodeEscape()).
Walter Dörwald <walter@livinglogic.de>
parents: 30149
diff changeset
2942 Py_XDECREF(exc);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2943 return (PyObject *)v;
22521
795bdd692176 Fix whitespace.
Walter Dörwald <walter@livinglogic.de>
parents: 22446
diff changeset
2944
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2945 ucnhashError:
16200
3484670c37f2 Move uchhash functionality into unicodedata (after the recent
Fredrik Lundh <fredrik@pythonware.com>
parents: 16064
diff changeset
2946 PyErr_SetString(
3484670c37f2 Move uchhash functionality into unicodedata (after the recent
Fredrik Lundh <fredrik@pythonware.com>
parents: 16064
diff changeset
2947 PyExc_UnicodeError,
3484670c37f2 Move uchhash functionality into unicodedata (after the recent
Fredrik Lundh <fredrik@pythonware.com>
parents: 16064
diff changeset
2948 "\\N escapes not supported (can't load unicodedata module)"
3484670c37f2 Move uchhash functionality into unicodedata (after the recent
Fredrik Lundh <fredrik@pythonware.com>
parents: 16064
diff changeset
2949 );
36877
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36675
diff changeset
2950 Py_XDECREF(v);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2951 Py_XDECREF(errorHandler);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2952 Py_XDECREF(exc);
16064
a97aa9238771 Better error message if ucnhash cannot be found (obscure attribute
Fredrik Lundh <fredrik@pythonware.com>
parents: 16029
diff changeset
2953 return NULL;
a97aa9238771 Better error message if ucnhash cannot be found (obscure attribute
Fredrik Lundh <fredrik@pythonware.com>
parents: 16029
diff changeset
2954
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2955 onError:
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2956 Py_XDECREF(v);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2957 Py_XDECREF(errorHandler);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
2958 Py_XDECREF(exc);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2959 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2960 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2961
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2962 /* Return a Unicode-Escape string version of the Unicode object.
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2963
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2964 If quotes is true, the string is enclosed in u"" or u'' quotes as
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2965 appropriate.
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2966
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2967 */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2968
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
2969 Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2970 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
2971 Py_UNICODE ch)
38278
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
2972 {
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
2973 /* like wcschr, but doesn't stop at NULL characters */
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
2974
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
2975 while (size-- > 0) {
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
2976 if (*s == ch)
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
2977 return s;
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
2978 s++;
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
2979 }
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
2980
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
2981 return NULL;
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
2982 }
12025
7994d9db7f4d On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents: 11997
diff changeset
2983
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2984 static
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2985 PyObject *unicodeescape_string(const Py_UNICODE *s,
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
2986 Py_ssize_t size,
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2987 int quotes)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2988 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2989 PyObject *repr;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2990 char *p;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2991
16226
c7c404b35f33 Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents: 16200
diff changeset
2992 static const char *hexdigit = "0123456789abcdef";
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
2993 #ifdef Py_UNICODE_WIDE
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
2994 const Py_ssize_t expandsize = 10;
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
2995 #else
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
2996 const Py_ssize_t expandsize = 6;
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
2997 #endif
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2998
39621
e8a65bbd6a29 Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents: 39491
diff changeset
2999 /* XXX(nnorwitz): rather than over-allocating, it would be
e8a65bbd6a29 Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents: 39491
diff changeset
3000 better to choose a different scheme. Perhaps scan the
e8a65bbd6a29 Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents: 39491
diff changeset
3001 first N-chars of the string and allocate based on that size.
e8a65bbd6a29 Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents: 39491
diff changeset
3002 */
e8a65bbd6a29 Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents: 39491
diff changeset
3003 /* Initial allocation is based on the longest-possible unichr
e8a65bbd6a29 Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents: 39491
diff changeset
3004 escape.
e8a65bbd6a29 Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents: 39491
diff changeset
3005
e8a65bbd6a29 Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents: 39491
diff changeset
3006 In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source
e8a65bbd6a29 Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents: 39491
diff changeset
3007 unichr, so in this case it's the longest unichr escape. In
e8a65bbd6a29 Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents: 39491
diff changeset
3008 narrow (UTF-16) builds this is five chars per source unichr
e8a65bbd6a29 Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents: 39491
diff changeset
3009 since there are two unichrs in the surrogate pair, so in narrow
e8a65bbd6a29 Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents: 39491
diff changeset
3010 (UTF-16) builds it's not the longest unichr escape.
e8a65bbd6a29 Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents: 39491
diff changeset
3011
e8a65bbd6a29 Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents: 39491
diff changeset
3012 In wide or narrow builds '\uxxxx' is 6 chars per source unichr,
e8a65bbd6a29 Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents: 39491
diff changeset
3013 so in the narrow (UTF-16) build case it's the longest unichr
e8a65bbd6a29 Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents: 39491
diff changeset
3014 escape.
e8a65bbd6a29 Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents: 39491
diff changeset
3015 */
e8a65bbd6a29 Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents: 39491
diff changeset
3016
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
3017 if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3018 return PyErr_NoMemory();
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
3019
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
3020 repr = PyString_FromStringAndSize(NULL,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3021 2
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3022 + expandsize*size
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3023 + 1);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3024 if (repr == NULL)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3025 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3026
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
3027 p = PyString_AS_STRING(repr);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3028
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3029 if (quotes) {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3030 *p++ = 'u';
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
3031 *p++ = (findchar(s, size, '\'') &&
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3032 !findchar(s, size, '"')) ? '"' : '\'';
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3033 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3034 while (size-- > 0) {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3035 Py_UNICODE ch = *s++;
18828
081d96fbcf43 Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents: 18774
diff changeset
3036
36056
f1ae11457d34 Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents: 35999
diff changeset
3037 /* Escape quotes and backslashes */
f1ae11457d34 Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents: 35999
diff changeset
3038 if ((quotes &&
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3039 ch == (Py_UNICODE) PyString_AS_STRING(repr)[1]) || ch == '\\') {
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3040 *p++ = '\\';
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3041 *p++ = (char) ch;
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3042 continue;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
3043 }
18828
081d96fbcf43 Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents: 18774
diff changeset
3044
18773
743f8e1d953c #ifdef out generation of \U escapes unless Py_UNICODE_WIDE. This
Guido van Rossum <guido@python.org>
parents: 18324
diff changeset
3045 #ifdef Py_UNICODE_WIDE
18292
ffbb9c918ac0 Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents: 18280
diff changeset
3046 /* Map 21-bit characters to '\U00xxxxxx' */
ffbb9c918ac0 Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents: 18280
diff changeset
3047 else if (ch >= 0x10000) {
ffbb9c918ac0 Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents: 18280
diff changeset
3048 *p++ = '\\';
ffbb9c918ac0 Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents: 18280
diff changeset
3049 *p++ = 'U';
18774
57684d3cefce Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents: 18773
diff changeset
3050 *p++ = hexdigit[(ch >> 28) & 0x0000000F];
57684d3cefce Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents: 18773
diff changeset
3051 *p++ = hexdigit[(ch >> 24) & 0x0000000F];
57684d3cefce Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents: 18773
diff changeset
3052 *p++ = hexdigit[(ch >> 20) & 0x0000000F];
57684d3cefce Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents: 18773
diff changeset
3053 *p++ = hexdigit[(ch >> 16) & 0x0000000F];
57684d3cefce Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents: 18773
diff changeset
3054 *p++ = hexdigit[(ch >> 12) & 0x0000000F];
57684d3cefce Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents: 18773
diff changeset
3055 *p++ = hexdigit[(ch >> 8) & 0x0000000F];
57684d3cefce Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents: 18773
diff changeset
3056 *p++ = hexdigit[(ch >> 4) & 0x0000000F];
18828
081d96fbcf43 Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents: 18774
diff changeset
3057 *p++ = hexdigit[ch & 0x0000000F];
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3058 continue;
18292
ffbb9c918ac0 Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents: 18280
diff changeset
3059 }
39621
e8a65bbd6a29 Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents: 39491
diff changeset
3060 #else
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3061 /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3062 else if (ch >= 0xD800 && ch < 0xDC00) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3063 Py_UNICODE ch2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3064 Py_UCS4 ucs;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3065
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3066 ch2 = *s++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3067 size--;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3068 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3069 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3070 *p++ = '\\';
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3071 *p++ = 'U';
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3072 *p++ = hexdigit[(ucs >> 28) & 0x0000000F];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3073 *p++ = hexdigit[(ucs >> 24) & 0x0000000F];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3074 *p++ = hexdigit[(ucs >> 20) & 0x0000000F];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3075 *p++ = hexdigit[(ucs >> 16) & 0x0000000F];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3076 *p++ = hexdigit[(ucs >> 12) & 0x0000000F];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3077 *p++ = hexdigit[(ucs >> 8) & 0x0000000F];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3078 *p++ = hexdigit[(ucs >> 4) & 0x0000000F];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3079 *p++ = hexdigit[ucs & 0x0000000F];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3080 continue;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3081 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3082 /* Fall through: isolated surrogates are copied as-is */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3083 s--;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3084 size++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3085 }
39621
e8a65bbd6a29 Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents: 39491
diff changeset
3086 #endif
18774
57684d3cefce Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents: 18773
diff changeset
3087
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3088 /* Map 16-bit characters to '\uxxxx' */
18774
57684d3cefce Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents: 18773
diff changeset
3089 if (ch >= 256) {
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3090 *p++ = '\\';
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3091 *p++ = 'u';
18774
57684d3cefce Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents: 18773
diff changeset
3092 *p++ = hexdigit[(ch >> 12) & 0x000F];
57684d3cefce Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents: 18773
diff changeset
3093 *p++ = hexdigit[(ch >> 8) & 0x000F];
57684d3cefce Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents: 18773
diff changeset
3094 *p++ = hexdigit[(ch >> 4) & 0x000F];
57684d3cefce Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents: 18773
diff changeset
3095 *p++ = hexdigit[ch & 0x000F];
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3096 }
18828
081d96fbcf43 Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents: 18774
diff changeset
3097
16226
c7c404b35f33 Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents: 16200
diff changeset
3098 /* Map special whitespace to '\t', \n', '\r' */
c7c404b35f33 Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents: 16200
diff changeset
3099 else if (ch == '\t') {
c7c404b35f33 Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents: 16200
diff changeset
3100 *p++ = '\\';
c7c404b35f33 Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents: 16200
diff changeset
3101 *p++ = 't';
c7c404b35f33 Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents: 16200
diff changeset
3102 }
c7c404b35f33 Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents: 16200
diff changeset
3103 else if (ch == '\n') {
c7c404b35f33 Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents: 16200
diff changeset
3104 *p++ = '\\';
c7c404b35f33 Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents: 16200
diff changeset
3105 *p++ = 'n';
c7c404b35f33 Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents: 16200
diff changeset
3106 }
c7c404b35f33 Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents: 16200
diff changeset
3107 else if (ch == '\r') {
c7c404b35f33 Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents: 16200
diff changeset
3108 *p++ = '\\';
c7c404b35f33 Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents: 16200
diff changeset
3109 *p++ = 'r';
c7c404b35f33 Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents: 16200
diff changeset
3110 }
18828
081d96fbcf43 Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents: 18774
diff changeset
3111
16226
c7c404b35f33 Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents: 16200
diff changeset
3112 /* Map non-printable US ASCII to '\xhh' */
21067
c9c4c523dcbf Fix for bug #485951: repr diff between string and unicode.
Marc-André Lemburg <mal@egenix.com>
parents: 21028
diff changeset
3113 else if (ch < ' ' || ch >= 0x7F) {
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3114 *p++ = '\\';
16226
c7c404b35f33 Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents: 16200
diff changeset
3115 *p++ = 'x';
18774
57684d3cefce Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents: 18773
diff changeset
3116 *p++ = hexdigit[(ch >> 4) & 0x000F];
57684d3cefce Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents: 18773
diff changeset
3117 *p++ = hexdigit[ch & 0x000F];
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
3118 }
18828
081d96fbcf43 Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents: 18774
diff changeset
3119
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3120 /* Copy everything else as-is */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3121 else
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3122 *p++ = (char) ch;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3123 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3124 if (quotes)
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
3125 *p++ = PyString_AS_STRING(repr)[1];
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3126
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3127 *p = '\0';
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
3128 _PyString_Resize(&repr, p - PyString_AS_STRING(repr));
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3129 return repr;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3130 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3131
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3132 PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3133 Py_ssize_t size)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3134 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3135 return unicodeescape_string(s, size, 0);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3136 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3137
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3138 PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3139 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3140 if (!PyUnicode_Check(unicode)) {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3141 PyErr_BadArgument();
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3142 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3143 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3144 return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode),
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3145 PyUnicode_GET_SIZE(unicode));
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3146 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3147
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3148 /* --- Raw Unicode Escape Codec ------------------------------------------- */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3149
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3150 PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3151 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3152 const char *errors)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3153 {
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3154 const char *starts = s;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
3155 Py_ssize_t startinpos;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
3156 Py_ssize_t endinpos;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
3157 Py_ssize_t outpos;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3158 PyUnicodeObject *v;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3159 Py_UNICODE *p;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3160 const char *end;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3161 const char *bs;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3162 PyObject *errorHandler = NULL;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3163 PyObject *exc = NULL;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
3164
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3165 /* Escaped strings will always be longer than the resulting
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3166 Unicode string, so we start with size here and then reduce the
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3167 length after conversion to the true value. (But decoding error
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3168 handler might have to resize the string) */
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3169 v = _PyUnicode_New(size);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3170 if (v == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3171 goto onError;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3172 if (size == 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3173 return (PyObject *)v;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3174 p = PyUnicode_AS_UNICODE(v);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3175 end = s + size;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3176 while (s < end) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3177 unsigned char c;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3178 Py_UCS4 x;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3179 int i;
29164
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28575
diff changeset
3180 int count;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3181
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3182 /* Non-escape characters are interpreted as Unicode ordinals */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3183 if (*s != '\\') {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3184 *p++ = (unsigned char)*s++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3185 continue;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3186 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3187 startinpos = s-starts;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3188
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3189 /* \u-escapes are only interpreted iff the number of leading
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3190 backslashes if odd */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3191 bs = s;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3192 for (;s < end;) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3193 if (*s != '\\')
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3194 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3195 *p++ = (unsigned char)*s++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3196 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3197 if (((s - bs) & 1) == 0 ||
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3198 s >= end ||
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3199 (*s != 'u' && *s != 'U')) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3200 continue;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3201 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3202 p--;
29164
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28575
diff changeset
3203 count = *s=='u' ? 4 : 8;
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3204 s++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3205
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3206 /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3207 outpos = p-PyUnicode_AS_UNICODE(v);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3208 for (x = 0, i = 0; i < count; ++i, ++s) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3209 c = (unsigned char)*s;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3210 if (!isxdigit(c)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3211 endinpos = s-starts;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3212 if (unicode_decode_call_errorhandler(
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3213 errors, &errorHandler,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3214 "rawunicodeescape", "truncated \\uXXXX",
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3215 starts, size, &startinpos, &endinpos, &exc, &s,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3216 &v, &outpos, &p))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3217 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3218 goto nextByte;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3219 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3220 x = (x<<4) & ~0xF;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3221 if (c >= '0' && c <= '9')
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3222 x += c - '0';
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3223 else if (c >= 'a' && c <= 'f')
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3224 x += 10 + c - 'a';
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3225 else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3226 x += 10 + c - 'A';
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3227 }
46028
8710ba7fba2b #1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46026
diff changeset
3228 if (x <= 0xffff)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3229 /* UCS-2 character */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3230 *p++ = (Py_UNICODE) x;
46028
8710ba7fba2b #1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46026
diff changeset
3231 else if (x <= 0x10ffff) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3232 /* UCS-4 character. Either store directly, or as
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3233 surrogate pair. */
46028
8710ba7fba2b #1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46026
diff changeset
3234 #ifdef Py_UNICODE_WIDE
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3235 *p++ = (Py_UNICODE) x;
46028
8710ba7fba2b #1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46026
diff changeset
3236 #else
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3237 x -= 0x10000L;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3238 *p++ = 0xD800 + (Py_UNICODE) (x >> 10);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3239 *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF);
46028
8710ba7fba2b #1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46026
diff changeset
3240 #endif
8710ba7fba2b #1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46026
diff changeset
3241 } else {
8710ba7fba2b #1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46026
diff changeset
3242 endinpos = s-starts;
8710ba7fba2b #1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46026
diff changeset
3243 outpos = p-PyUnicode_AS_UNICODE(v);
29164
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28575
diff changeset
3244 if (unicode_decode_call_errorhandler(
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28575
diff changeset
3245 errors, &errorHandler,
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28575
diff changeset
3246 "rawunicodeescape", "\\Uxxxxxxxx out of range",
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3247 starts, size, &startinpos, &endinpos, &exc, &s,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3248 &v, &outpos, &p))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3249 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3250 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3251 nextByte:
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3252 ;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3253 }
37662
0678ce9fb6b2 Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 37661
diff changeset
3254 if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3255 goto onError;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3256 Py_XDECREF(errorHandler);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3257 Py_XDECREF(exc);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3258 return (PyObject *)v;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
3259
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3260 onError:
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3261 Py_XDECREF(v);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3262 Py_XDECREF(errorHandler);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3263 Py_XDECREF(exc);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3264 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3265 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3266
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3267 PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3268 Py_ssize_t size)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3269 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3270 PyObject *repr;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3271 char *p;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3272 char *q;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3273
16226
c7c404b35f33 Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents: 16200
diff changeset
3274 static const char *hexdigit = "0123456789abcdef";
29164
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28575
diff changeset
3275 #ifdef Py_UNICODE_WIDE
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
3276 const Py_ssize_t expandsize = 10;
29164
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28575
diff changeset
3277 #else
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
3278 const Py_ssize_t expandsize = 6;
29164
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28575
diff changeset
3279 #endif
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
3280
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
3281 if (size > PY_SSIZE_T_MAX / expandsize)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3282 return PyErr_NoMemory();
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
3283
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
3284 repr = PyString_FromStringAndSize(NULL, expandsize * size);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3285 if (repr == NULL)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3286 return NULL;
13966
91ddeed12fa7 Fixed a couple of instances where a 0-length string was being
Marc-André Lemburg <mal@egenix.com>
parents: 13946
diff changeset
3287 if (size == 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3288 return repr;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3289
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
3290 p = q = PyString_AS_STRING(repr);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3291 while (size-- > 0) {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3292 Py_UNICODE ch = *s++;
29164
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28575
diff changeset
3293 #ifdef Py_UNICODE_WIDE
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3294 /* Map 32-bit characters to '\Uxxxxxxxx' */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3295 if (ch >= 0x10000) {
29164
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28575
diff changeset
3296 *p++ = '\\';
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28575
diff changeset
3297 *p++ = 'U';
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28575
diff changeset
3298 *p++ = hexdigit[(ch >> 28) & 0xf];
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28575
diff changeset
3299 *p++ = hexdigit[(ch >> 24) & 0xf];
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28575
diff changeset
3300 *p++ = hexdigit[(ch >> 20) & 0xf];
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28575
diff changeset
3301 *p++ = hexdigit[(ch >> 16) & 0xf];
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28575
diff changeset
3302 *p++ = hexdigit[(ch >> 12) & 0xf];
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28575
diff changeset
3303 *p++ = hexdigit[(ch >> 8) & 0xf];
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28575
diff changeset
3304 *p++ = hexdigit[(ch >> 4) & 0xf];
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28575
diff changeset
3305 *p++ = hexdigit[ch & 15];
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
3306 }
29164
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28575
diff changeset
3307 else
46028
8710ba7fba2b #1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46026
diff changeset
3308 #else
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3309 /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3310 if (ch >= 0xD800 && ch < 0xDC00) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3311 Py_UNICODE ch2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3312 Py_UCS4 ucs;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3313
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3314 ch2 = *s++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3315 size--;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3316 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3317 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3318 *p++ = '\\';
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3319 *p++ = 'U';
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3320 *p++ = hexdigit[(ucs >> 28) & 0xf];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3321 *p++ = hexdigit[(ucs >> 24) & 0xf];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3322 *p++ = hexdigit[(ucs >> 20) & 0xf];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3323 *p++ = hexdigit[(ucs >> 16) & 0xf];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3324 *p++ = hexdigit[(ucs >> 12) & 0xf];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3325 *p++ = hexdigit[(ucs >> 8) & 0xf];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3326 *p++ = hexdigit[(ucs >> 4) & 0xf];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3327 *p++ = hexdigit[ucs & 0xf];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3328 continue;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3329 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3330 /* Fall through: isolated surrogates are copied as-is */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3331 s--;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3332 size++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3333 }
29164
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28575
diff changeset
3334 #endif
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3335 /* Map 16-bit characters to '\uxxxx' */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3336 if (ch >= 256) {
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3337 *p++ = '\\';
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3338 *p++ = 'u';
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3339 *p++ = hexdigit[(ch >> 12) & 0xf];
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3340 *p++ = hexdigit[(ch >> 8) & 0xf];
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3341 *p++ = hexdigit[(ch >> 4) & 0xf];
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3342 *p++ = hexdigit[ch & 15];
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3343 }
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3344 /* Copy everything else as-is */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3345 else
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3346 *p++ = (char) ch;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3347 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3348 *p = '\0';
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
3349 _PyString_Resize(&repr, p - q);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3350 return repr;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3351 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3352
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3353 PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3354 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3355 if (!PyUnicode_Check(unicode)) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3356 PyErr_BadArgument();
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3357 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3358 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3359 return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode),
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3360 PyUnicode_GET_SIZE(unicode));
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3361 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3362
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3363 /* --- Unicode Internal Codec ------------------------------------------- */
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3364
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3365 PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3366 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3367 const char *errors)
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3368 {
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3369 const char *starts = s;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
3370 Py_ssize_t startinpos;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
3371 Py_ssize_t endinpos;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
3372 Py_ssize_t outpos;
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3373 PyUnicodeObject *v;
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3374 Py_UNICODE *p;
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3375 const char *end;
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3376 const char *reason;
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3377 PyObject *errorHandler = NULL;
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3378 PyObject *exc = NULL;
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3379
36236
ecd423718a32 Fix icc warnings: remove (sometimes) unused variable conditionally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36203
diff changeset
3380 #ifdef Py_UNICODE_WIDE
ecd423718a32 Fix icc warnings: remove (sometimes) unused variable conditionally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36203
diff changeset
3381 Py_UNICODE unimax = PyUnicode_GetMax();
ecd423718a32 Fix icc warnings: remove (sometimes) unused variable conditionally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36203
diff changeset
3382 #endif
ecd423718a32 Fix icc warnings: remove (sometimes) unused variable conditionally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36203
diff changeset
3383
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39734
diff changeset
3384 /* XXX overflow detection missing */
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3385 v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE);
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3386 if (v == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3387 goto onError;
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3388 if (PyUnicode_GetSize((PyObject *)v) == 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3389 return (PyObject *)v;
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3390 p = PyUnicode_AS_UNICODE(v);
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3391 end = s + size;
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3392
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3393 while (s < end) {
38182
d8355cccff4a Patch #1488312, Fix memory alignment problem on SPARC in unicode. Will backport
Neal Norwitz <nnorwitz@gmail.com>
parents: 37775
diff changeset
3394 memcpy(p, s, sizeof(Py_UNICODE));
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3395 /* We have to sanity check the raw data, otherwise doom looms for
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3396 some malformed UCS-4 data. */
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3397 if (
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3398 #ifdef Py_UNICODE_WIDE
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3399 *p > unimax || *p < 0 ||
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3400 #endif
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3401 end-s < Py_UNICODE_SIZE
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3402 )
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3403 {
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3404 startinpos = s - starts;
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3405 if (end-s < Py_UNICODE_SIZE) {
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3406 endinpos = end-starts;
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3407 reason = "truncated input";
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3408 }
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3409 else {
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3410 endinpos = s - starts + Py_UNICODE_SIZE;
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3411 reason = "illegal code point (> 0x10FFFF)";
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3412 }
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3413 outpos = p - PyUnicode_AS_UNICODE(v);
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3414 if (unicode_decode_call_errorhandler(
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3415 errors, &errorHandler,
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3416 "unicode_internal", reason,
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3417 starts, size, &startinpos, &endinpos, &exc, &s,
50305
e673632e1885 Merged revisions 67898,67904-67907,67912,67918,67920-67921,67923-67924,67927,67930,67932,67943 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
3418 &v, &outpos, &p)) {
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3419 goto onError;
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3420 }
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3421 }
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3422 else {
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3423 p++;
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3424 s += Py_UNICODE_SIZE;
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3425 }
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3426 }
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3427
37662
0678ce9fb6b2 Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 37661
diff changeset
3428 if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0)
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3429 goto onError;
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3430 Py_XDECREF(errorHandler);
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3431 Py_XDECREF(exc);
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3432 return (PyObject *)v;
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3433
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3434 onError:
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3435 Py_XDECREF(v);
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3436 Py_XDECREF(errorHandler);
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3437 Py_XDECREF(exc);
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3438 return NULL;
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3439 }
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34016
diff changeset
3440
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3441 /* --- Latin-1 Codec ------------------------------------------------------ */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3442
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3443 PyObject *PyUnicode_DecodeLatin1(const char *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3444 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3445 const char *errors)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3446 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3447 PyUnicodeObject *v;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3448 Py_UNICODE *p;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
3449
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3450 /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */
31238
4edf200a8118 SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31199
diff changeset
3451 if (size == 1) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3452 Py_UNICODE r = *(unsigned char*)s;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3453 return PyUnicode_FromUnicode(&r, 1);
17618
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
3454 }
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
3455
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3456 v = _PyUnicode_New(size);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3457 if (v == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3458 goto onError;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3459 if (size == 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3460 return (PyObject *)v;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3461 p = PyUnicode_AS_UNICODE(v);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3462 while (size-- > 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3463 *p++ = (unsigned char)*s++;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3464 return (PyObject *)v;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
3465
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3466 onError:
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3467 Py_XDECREF(v);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3468 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3469 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3470
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3471 /* create or adjust a UnicodeEncodeError */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3472 static void make_encode_exception(PyObject **exceptionObject,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3473 const char *encoding,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3474 const Py_UNICODE *unicode, Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3475 Py_ssize_t startpos, Py_ssize_t endpos,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3476 const char *reason)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3477 {
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3478 if (*exceptionObject == NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3479 *exceptionObject = PyUnicodeEncodeError_Create(
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3480 encoding, unicode, size, startpos, endpos, reason);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3481 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3482 else {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3483 if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3484 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3485 if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3486 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3487 if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3488 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3489 return;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3490 onError:
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3491 Py_DECREF(*exceptionObject);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3492 *exceptionObject = NULL;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3493 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3494 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3495
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3496 /* raises a UnicodeEncodeError */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3497 static void raise_encode_exception(PyObject **exceptionObject,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3498 const char *encoding,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3499 const Py_UNICODE *unicode, Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3500 Py_ssize_t startpos, Py_ssize_t endpos,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3501 const char *reason)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3502 {
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3503 make_encode_exception(exceptionObject,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3504 encoding, unicode, size, startpos, endpos, reason);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3505 if (*exceptionObject != NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3506 PyCodec_StrictErrors(*exceptionObject);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3507 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3508
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3509 /* error handling callback helper:
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3510 build arguments, call the callback and check the arguments,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3511 put the result into newpos and return the replacement string, which
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3512 has to be freed by the caller */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3513 static PyObject *unicode_encode_call_errorhandler(const char *errors,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3514 PyObject **errorHandler,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3515 const char *encoding, const char *reason,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3516 const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3517 Py_ssize_t startpos, Py_ssize_t endpos,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3518 Py_ssize_t *newpos)
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
3519 {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
3520 static char *argparse = "O!n;encoding error handler must return (unicode, int) tuple";
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3521
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3522 PyObject *restuple;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3523 PyObject *resunicode;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3524
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3525 if (*errorHandler == NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3526 *errorHandler = PyCodec_LookupError(errors);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3527 if (*errorHandler == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3528 return NULL;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3529 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3530
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3531 make_encode_exception(exceptionObject,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3532 encoding, unicode, size, startpos, endpos, reason);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3533 if (*exceptionObject == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3534 return NULL;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3535
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3536 restuple = PyObject_CallFunctionObjArgs(
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3537 *errorHandler, *exceptionObject, NULL);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3538 if (restuple == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3539 return NULL;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3540 if (!PyTuple_Check(restuple)) {
51598
512c5a5de00b Fix warnings GCC emits where the argument of PyErr_Format is a single variable.
Georg Brandl <georg@python.org>
parents: 51218
diff changeset
3541 PyErr_SetString(PyExc_TypeError, &argparse[4]);
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3542 Py_DECREF(restuple);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3543 return NULL;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3544 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3545 if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3546 &resunicode, newpos)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3547 Py_DECREF(restuple);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3548 return NULL;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3549 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3550 if (*newpos<0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3551 *newpos = size+*newpos;
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27521
diff changeset
3552 if (*newpos<0 || *newpos>size) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3553 PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3554 Py_DECREF(restuple);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3555 return NULL;
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27521
diff changeset
3556 }
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3557 Py_INCREF(resunicode);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3558 Py_DECREF(restuple);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3559 return resunicode;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3560 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3561
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3562 static PyObject *unicode_encode_ucs1(const Py_UNICODE *p,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3563 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3564 const char *errors,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3565 int limit)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3566 {
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3567 /* output object */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3568 PyObject *res;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3569 /* pointers to the beginning and end+1 of input */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3570 const Py_UNICODE *startp = p;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3571 const Py_UNICODE *endp = p + size;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3572 /* pointer to the beginning of the unencodable characters */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3573 /* const Py_UNICODE *badp = NULL; */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3574 /* pointer into the output */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3575 char *str;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3576 /* current output position */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
3577 Py_ssize_t respos = 0;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
3578 Py_ssize_t ressize;
37596
1b078493d538 More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37306
diff changeset
3579 const char *encoding = (limit == 256) ? "latin-1" : "ascii";
1b078493d538 More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37306
diff changeset
3580 const char *reason = (limit == 256) ? "ordinal not in range(256)" : "ordinal not in range(128)";
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3581 PyObject *errorHandler = NULL;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3582 PyObject *exc = NULL;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3583 /* the following variable is used for caching string comparisons
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3584 * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3585 int known_errorHandler = -1;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3586
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3587 /* allocate enough for a simple encoding without
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3588 replacements, if we need more, we'll resize */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
3589 res = PyString_FromStringAndSize(NULL, size);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3590 if (res == NULL)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3591 goto onError;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3592 if (size == 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3593 return res;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
3594 str = PyString_AS_STRING(res);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3595 ressize = size;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3596
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3597 while (p<endp) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3598 Py_UNICODE c = *p;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3599
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3600 /* can we encode this? */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3601 if (c<limit) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3602 /* no overflow check, because we know that the space is enough */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3603 *str++ = (char)c;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3604 ++p;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3605 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3606 else {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3607 Py_ssize_t unicodepos = p-startp;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3608 Py_ssize_t requiredsize;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3609 PyObject *repunicode;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3610 Py_ssize_t repsize;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3611 Py_ssize_t newpos;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3612 Py_ssize_t respos;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3613 Py_UNICODE *uni2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3614 /* startpos for collecting unencodable chars */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3615 const Py_UNICODE *collstart = p;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3616 const Py_UNICODE *collend = p;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3617 /* find all unecodable characters */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3618 while ((collend < endp) && ((*collend)>=limit))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3619 ++collend;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3620 /* cache callback name lookup (if not done yet, i.e. it's the first error) */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3621 if (known_errorHandler==-1) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3622 if ((errors==NULL) || (!strcmp(errors, "strict")))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3623 known_errorHandler = 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3624 else if (!strcmp(errors, "replace"))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3625 known_errorHandler = 2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3626 else if (!strcmp(errors, "ignore"))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3627 known_errorHandler = 3;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3628 else if (!strcmp(errors, "xmlcharrefreplace"))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3629 known_errorHandler = 4;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3630 else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3631 known_errorHandler = 0;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3632 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3633 switch (known_errorHandler) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3634 case 1: /* strict */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3635 raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3636 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3637 case 2: /* replace */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3638 while (collstart++<collend)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3639 *str++ = '?'; /* fall through */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3640 case 3: /* ignore */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3641 p = collend;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3642 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3643 case 4: /* xmlcharrefreplace */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3644 respos = str-PyString_AS_STRING(res);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3645 /* determine replacement size (temporarily (mis)uses p) */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3646 for (p = collstart, repsize = 0; p < collend; ++p) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3647 if (*p<10)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3648 repsize += 2+1+1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3649 else if (*p<100)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3650 repsize += 2+2+1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3651 else if (*p<1000)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3652 repsize += 2+3+1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3653 else if (*p<10000)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3654 repsize += 2+4+1;
31238
4edf200a8118 SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31199
diff changeset
3655 #ifndef Py_UNICODE_WIDE
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3656 else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3657 repsize += 2+5+1;
31238
4edf200a8118 SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31199
diff changeset
3658 #else
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3659 else if (*p<100000)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3660 repsize += 2+5+1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3661 else if (*p<1000000)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3662 repsize += 2+6+1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3663 else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3664 repsize += 2+7+1;
31238
4edf200a8118 SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31199
diff changeset
3665 #endif
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3666 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3667 requiredsize = respos+repsize+(endp-collend);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3668 if (requiredsize > ressize) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3669 if (requiredsize<2*ressize)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3670 requiredsize = 2*ressize;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3671 if (_PyString_Resize(&res, requiredsize))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3672 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3673 str = PyString_AS_STRING(res) + respos;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3674 ressize = requiredsize;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3675 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3676 /* generate replacement (temporarily (mis)uses p) */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3677 for (p = collstart; p < collend; ++p) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3678 str += sprintf(str, "&#%d;", (int)*p);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3679 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3680 p = collend;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3681 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3682 default:
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3683 repunicode = unicode_encode_call_errorhandler(errors, &errorHandler,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3684 encoding, reason, startp, size, &exc,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3685 collstart-startp, collend-startp, &newpos);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3686 if (repunicode == NULL)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3687 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3688 /* need more space? (at least enough for what we have+the
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3689 replacement+the rest of the string, so we won't have to
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3690 check space for encodable characters) */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3691 respos = str-PyString_AS_STRING(res);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3692 repsize = PyUnicode_GET_SIZE(repunicode);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3693 requiredsize = respos+repsize+(endp-collend);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3694 if (requiredsize > ressize) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3695 if (requiredsize<2*ressize)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3696 requiredsize = 2*ressize;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3697 if (_PyString_Resize(&res, requiredsize)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3698 Py_DECREF(repunicode);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3699 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3700 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3701 str = PyString_AS_STRING(res) + respos;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3702 ressize = requiredsize;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3703 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3704 /* check if there is anything unencodable in the replacement
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3705 and copy it to the output */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3706 for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3707 c = *uni2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3708 if (c >= limit) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3709 raise_encode_exception(&exc, encoding, startp, size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3710 unicodepos, unicodepos+1, reason);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3711 Py_DECREF(repunicode);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3712 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3713 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3714 *str = (char)c;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3715 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3716 p = startp + newpos;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3717 Py_DECREF(repunicode);
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
3718 }
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3719 }
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3720 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3721 /* Resize if we allocated to much */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
3722 respos = str-PyString_AS_STRING(res);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3723 if (respos<ressize)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3724 /* If this falls res will be NULL */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3725 _PyString_Resize(&res, respos);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3726 Py_XDECREF(errorHandler);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3727 Py_XDECREF(exc);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3728 return res;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3729
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3730 onError:
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3731 Py_XDECREF(res);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3732 Py_XDECREF(errorHandler);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3733 Py_XDECREF(exc);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3734 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3735 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3736
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3737 PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3738 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3739 const char *errors)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3740 {
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3741 return unicode_encode_ucs1(p, size, errors, 256);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3742 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3743
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3744 PyObject *PyUnicode_AsLatin1String(PyObject *unicode)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3745 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3746 if (!PyUnicode_Check(unicode)) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3747 PyErr_BadArgument();
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3748 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3749 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3750 return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode),
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3751 PyUnicode_GET_SIZE(unicode),
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3752 NULL);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3753 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3754
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3755 /* --- 7-bit ASCII Codec -------------------------------------------------- */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3756
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3757 PyObject *PyUnicode_DecodeASCII(const char *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3758 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3759 const char *errors)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3760 {
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3761 const char *starts = s;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3762 PyUnicodeObject *v;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3763 Py_UNICODE *p;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
3764 Py_ssize_t startinpos;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
3765 Py_ssize_t endinpos;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
3766 Py_ssize_t outpos;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3767 const char *e;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3768 PyObject *errorHandler = NULL;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3769 PyObject *exc = NULL;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
3770
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3771 /* ASCII is equivalent to the first 128 ordinals in Unicode. */
17618
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
3772 if (size == 1 && *(unsigned char*)s < 128) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3773 Py_UNICODE r = *(unsigned char*)s;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3774 return PyUnicode_FromUnicode(&r, 1);
17618
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
3775 }
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
3776
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3777 v = _PyUnicode_New(size);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3778 if (v == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3779 goto onError;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3780 if (size == 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3781 return (PyObject *)v;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3782 p = PyUnicode_AS_UNICODE(v);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3783 e = s + size;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3784 while (s < e) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3785 register unsigned char c = (unsigned char)*s;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3786 if (c < 128) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3787 *p++ = c;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3788 ++s;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3789 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3790 else {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3791 startinpos = s-starts;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3792 endinpos = startinpos + 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3793 outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3794 if (unicode_decode_call_errorhandler(
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3795 errors, &errorHandler,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3796 "ascii", "ordinal not in range(128)",
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3797 starts, size, &startinpos, &endinpos, &exc, &s,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3798 &v, &outpos, &p))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3799 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3800 }
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3801 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
3802 if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v))
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3803 if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3804 goto onError;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3805 Py_XDECREF(errorHandler);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3806 Py_XDECREF(exc);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3807 return (PyObject *)v;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
3808
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3809 onError:
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3810 Py_XDECREF(v);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3811 Py_XDECREF(errorHandler);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3812 Py_XDECREF(exc);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3813 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3814 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3815
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3816 PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3817 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3818 const char *errors)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3819 {
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
3820 return unicode_encode_ucs1(p, size, errors, 128);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3821 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3822
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3823 PyObject *PyUnicode_AsASCIIString(PyObject *unicode)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3824 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3825 if (!PyUnicode_Check(unicode)) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3826 PyErr_BadArgument();
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3827 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3828 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3829 return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode),
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3830 PyUnicode_GET_SIZE(unicode),
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3831 NULL);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3832 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3833
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 23837
diff changeset
3834 #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
12193
c3fc2b4617fc Some blank lines.
Guido van Rossum <guido@python.org>
parents: 12067
diff changeset
3835
12067
bcab189ddf86 MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
3836 /* --- MBCS codecs for Windows -------------------------------------------- */
12193
c3fc2b4617fc Some blank lines.
Guido van Rossum <guido@python.org>
parents: 12067
diff changeset
3837
52251
b846d709cb76 There is no macro named SIZEOF_SSIZE_T. Should use SIZEOF_SIZE_T instead.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52210
diff changeset
3838 #if SIZEOF_INT < SIZEOF_SIZE_T
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3839 #define NEED_RETRY
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3840 #endif
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3841
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3842 /* XXX This code is limited to "true" double-byte encodings, as
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3843 a) it assumes an incomplete character consists of a single byte, and
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3844 b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3845 encodings, see IsDBCSLeadByteEx documentation. */
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3846
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3847 static int is_dbcs_lead_byte(const char *s, int offset)
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3848 {
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3849 const char *curr = s + offset;
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3850
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3851 if (IsDBCSLeadByte(*curr)) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3852 const char *prev = CharPrev(s, curr);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3853 return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2);
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3854 }
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3855 return 0;
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3856 }
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3857
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3858 /*
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3859 * Decode MBCS string into unicode object. If 'final' is set, converts
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3860 * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise.
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3861 */
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3862 static int decode_mbcs(PyUnicodeObject **v,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3863 const char *s, /* MBCS string */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3864 int size, /* sizeof MBCS string */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3865 int final)
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3866 {
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3867 Py_UNICODE *p;
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3868 Py_ssize_t n = 0;
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3869 int usize = 0;
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3870
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3871 assert(size >= 0);
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3872
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3873 /* Skip trailing lead-byte unless 'final' is set */
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3874 if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1))
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3875 --size;
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3876
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3877 /* First get the size of the result */
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3878 if (size > 0) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3879 usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3880 if (usize == 0) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3881 PyErr_SetFromWindowsErrWithFilename(0, NULL);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3882 return -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3883 }
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3884 }
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3885
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3886 if (*v == NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3887 /* Create unicode object */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3888 *v = _PyUnicode_New(usize);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3889 if (*v == NULL)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3890 return -1;
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3891 }
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3892 else {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3893 /* Extend unicode object */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3894 n = PyUnicode_GET_SIZE(*v);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3895 if (_PyUnicode_Resize(v, n + usize) < 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3896 return -1;
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3897 }
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3898
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3899 /* Do the conversion */
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3900 if (size > 0) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3901 p = PyUnicode_AS_UNICODE(*v) + n;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3902 if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3903 PyErr_SetFromWindowsErrWithFilename(0, NULL);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3904 return -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3905 }
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3906 }
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3907
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3908 return size;
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3909 }
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3910
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3911 PyObject *PyUnicode_DecodeMBCSStateful(const char *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3912 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3913 const char *errors,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3914 Py_ssize_t *consumed)
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3915 {
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3916 PyUnicodeObject *v = NULL;
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3917 int done;
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3918
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3919 if (consumed)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3920 *consumed = 0;
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3921
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3922 #ifdef NEED_RETRY
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3923 retry:
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3924 if (size > INT_MAX)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3925 done = decode_mbcs(&v, s, INT_MAX, 0);
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3926 else
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3927 #endif
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3928 done = decode_mbcs(&v, s, (int)size, !consumed);
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3929
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3930 if (done < 0) {
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3931 Py_XDECREF(v);
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3932 return NULL;
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3933 }
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3934
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3935 if (consumed)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3936 *consumed += done;
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3937
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3938 #ifdef NEED_RETRY
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3939 if (size > INT_MAX) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3940 s += done;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3941 size -= done;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3942 goto retry;
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3943 }
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3944 #endif
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3945
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3946 return (PyObject *)v;
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3947 }
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3948
12067
bcab189ddf86 MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
3949 PyObject *PyUnicode_DecodeMBCS(const char *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3950 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3951 const char *errors)
12067
bcab189ddf86 MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
3952 {
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3953 return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL);
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3954 }
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3955
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3956 /*
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3957 * Convert unicode into string object (MBCS).
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3958 * Returns 0 if succeed, -1 otherwise.
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3959 */
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3960 static int encode_mbcs(PyObject **repr,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3961 const Py_UNICODE *p, /* unicode */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3962 int size) /* size of unicode */
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3963 {
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3964 int mbcssize = 0;
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3965 Py_ssize_t n = 0;
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3966
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3967 assert(size >= 0);
12067
bcab189ddf86 MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
3968
bcab189ddf86 MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
3969 /* First get the size of the result */
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3970 if (size > 0) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3971 mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3972 if (mbcssize == 0) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3973 PyErr_SetFromWindowsErrWithFilename(0, NULL);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3974 return -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3975 }
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3976 }
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3977
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3978 if (*repr == NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3979 /* Create string object */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3980 *repr = PyString_FromStringAndSize(NULL, mbcssize);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3981 if (*repr == NULL)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3982 return -1;
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3983 }
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3984 else {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3985 /* Extend string object */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3986 n = PyString_Size(*repr);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3987 if (_PyString_Resize(repr, n + mbcssize) < 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3988 return -1;
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3989 }
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3990
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3991 /* Do the conversion */
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3992 if (size > 0) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3993 char *s = PyString_AS_STRING(*repr) + n;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3994 if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3995 PyErr_SetFromWindowsErrWithFilename(0, NULL);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3996 return -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
3997 }
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3998 }
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
3999
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
4000 return 0;
12067
bcab189ddf86 MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4001 }
bcab189ddf86 MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4002
bcab189ddf86 MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4003 PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4004 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4005 const char *errors)
12067
bcab189ddf86 MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4006 {
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
4007 PyObject *repr = NULL;
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
4008 int ret;
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
4009
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
4010 #ifdef NEED_RETRY
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4011 retry:
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
4012 if (size > INT_MAX)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4013 ret = encode_mbcs(&repr, p, INT_MAX);
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
4014 else
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
4015 #endif
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4016 ret = encode_mbcs(&repr, p, (int)size);
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
4017
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
4018 if (ret < 0) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4019 Py_XDECREF(repr);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4020 return NULL;
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
4021 }
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
4022
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
4023 #ifdef NEED_RETRY
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
4024 if (size > INT_MAX) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4025 p += INT_MAX;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4026 size -= INT_MAX;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4027 goto retry;
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
4028 }
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
4029 #endif
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
4030
12067
bcab189ddf86 MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4031 return repr;
bcab189ddf86 MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4032 }
12193
c3fc2b4617fc Some blank lines.
Guido van Rossum <guido@python.org>
parents: 12067
diff changeset
4033
29706
a4fead0e24f8 Support 'mbcs' as a 'built-in' encoding, so the C API can use it without
Mark Hammond <mhammond@skippinet.com.au>
parents: 29641
diff changeset
4034 PyObject *PyUnicode_AsMBCSString(PyObject *unicode)
a4fead0e24f8 Support 'mbcs' as a 'built-in' encoding, so the C API can use it without
Mark Hammond <mhammond@skippinet.com.au>
parents: 29641
diff changeset
4035 {
a4fead0e24f8 Support 'mbcs' as a 'built-in' encoding, so the C API can use it without
Mark Hammond <mhammond@skippinet.com.au>
parents: 29641
diff changeset
4036 if (!PyUnicode_Check(unicode)) {
a4fead0e24f8 Support 'mbcs' as a 'built-in' encoding, so the C API can use it without
Mark Hammond <mhammond@skippinet.com.au>
parents: 29641
diff changeset
4037 PyErr_BadArgument();
a4fead0e24f8 Support 'mbcs' as a 'built-in' encoding, so the C API can use it without
Mark Hammond <mhammond@skippinet.com.au>
parents: 29641
diff changeset
4038 return NULL;
a4fead0e24f8 Support 'mbcs' as a 'built-in' encoding, so the C API can use it without
Mark Hammond <mhammond@skippinet.com.au>
parents: 29641
diff changeset
4039 }
a4fead0e24f8 Support 'mbcs' as a 'built-in' encoding, so the C API can use it without
Mark Hammond <mhammond@skippinet.com.au>
parents: 29641
diff changeset
4040 return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode),
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4041 PyUnicode_GET_SIZE(unicode),
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4042 NULL);
29706
a4fead0e24f8 Support 'mbcs' as a 'built-in' encoding, so the C API can use it without
Mark Hammond <mhammond@skippinet.com.au>
parents: 29641
diff changeset
4043 }
a4fead0e24f8 Support 'mbcs' as a 'built-in' encoding, so the C API can use it without
Mark Hammond <mhammond@skippinet.com.au>
parents: 29641
diff changeset
4044
38817
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
4045 #undef NEED_RETRY
549c547700af Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents: 38786
diff changeset
4046
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 23837
diff changeset
4047 #endif /* MS_WINDOWS */
12067
bcab189ddf86 MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4048
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4049 /* --- Character Mapping Codec -------------------------------------------- */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4050
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4051 PyObject *PyUnicode_DecodeCharmap(const char *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4052 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4053 PyObject *mapping,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4054 const char *errors)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4055 {
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4056 const char *starts = s;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
4057 Py_ssize_t startinpos;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
4058 Py_ssize_t endinpos;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
4059 Py_ssize_t outpos;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4060 const char *e;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4061 PyUnicodeObject *v;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4062 Py_UNICODE *p;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
4063 Py_ssize_t extrachars = 0;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4064 PyObject *errorHandler = NULL;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4065 PyObject *exc = NULL;
35702
22a720b35314 Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents: 35485
diff changeset
4066 Py_UNICODE *mapstring = NULL;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
4067 Py_ssize_t maplen = 0;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
4068
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4069 /* Default to Latin-1 */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4070 if (mapping == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4071 return PyUnicode_DecodeLatin1(s, size, errors);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4072
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4073 v = _PyUnicode_New(size);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4074 if (v == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4075 goto onError;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4076 if (size == 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4077 return (PyObject *)v;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4078 p = PyUnicode_AS_UNICODE(v);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4079 e = s + size;
35702
22a720b35314 Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents: 35485
diff changeset
4080 if (PyUnicode_CheckExact(mapping)) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4081 mapstring = PyUnicode_AS_UNICODE(mapping);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4082 maplen = PyUnicode_GET_SIZE(mapping);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4083 while (s < e) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4084 unsigned char ch = *s;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4085 Py_UNICODE x = 0xfffe; /* illegal value */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4086
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4087 if (ch < maplen)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4088 x = mapstring[ch];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4089
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4090 if (x == 0xfffe) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4091 /* undefined mapping */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4092 outpos = p-PyUnicode_AS_UNICODE(v);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4093 startinpos = s-starts;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4094 endinpos = startinpos+1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4095 if (unicode_decode_call_errorhandler(
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4096 errors, &errorHandler,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4097 "charmap", "character maps to <undefined>",
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4098 starts, size, &startinpos, &endinpos, &exc, &s,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4099 &v, &outpos, &p)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4100 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4101 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4102 continue;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4103 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4104 *p++ = x;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4105 ++s;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4106 }
35702
22a720b35314 Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents: 35485
diff changeset
4107 }
22a720b35314 Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents: 35485
diff changeset
4108 else {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4109 while (s < e) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4110 unsigned char ch = *s;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4111 PyObject *w, *x;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4112
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4113 /* Get mapping (char ordinal -> integer, Unicode char or None) */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4114 w = PyInt_FromLong((long)ch);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4115 if (w == NULL)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4116 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4117 x = PyObject_GetItem(mapping, w);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4118 Py_DECREF(w);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4119 if (x == NULL) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4120 if (PyErr_ExceptionMatches(PyExc_LookupError)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4121 /* No mapping found means: mapping is undefined. */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4122 PyErr_Clear();
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4123 x = Py_None;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4124 Py_INCREF(x);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4125 } else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4126 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4127 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4128
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4129 /* Apply mapping */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4130 if (PyInt_Check(x)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4131 long value = PyInt_AS_LONG(x);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4132 if (value < 0 || value > 65535) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4133 PyErr_SetString(PyExc_TypeError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4134 "character mapping must be in range(65536)");
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4135 Py_DECREF(x);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4136 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4137 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4138 *p++ = (Py_UNICODE)value;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4139 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4140 else if (x == Py_None) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4141 /* undefined mapping */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4142 outpos = p-PyUnicode_AS_UNICODE(v);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4143 startinpos = s-starts;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4144 endinpos = startinpos+1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4145 if (unicode_decode_call_errorhandler(
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4146 errors, &errorHandler,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4147 "charmap", "character maps to <undefined>",
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4148 starts, size, &startinpos, &endinpos, &exc, &s,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4149 &v, &outpos, &p)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4150 Py_DECREF(x);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4151 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4152 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4153 Py_DECREF(x);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4154 continue;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4155 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4156 else if (PyUnicode_Check(x)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4157 Py_ssize_t targetsize = PyUnicode_GET_SIZE(x);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4158
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4159 if (targetsize == 1)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4160 /* 1-1 mapping */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4161 *p++ = *PyUnicode_AS_UNICODE(x);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4162
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4163 else if (targetsize > 1) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4164 /* 1-n mapping */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4165 if (targetsize > extrachars) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4166 /* resize first */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4167 Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4168 Py_ssize_t needed = (targetsize - extrachars) + \
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4169 (targetsize << 2);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4170 extrachars += needed;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4171 /* XXX overflow detection missing */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4172 if (_PyUnicode_Resize(&v,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4173 PyUnicode_GET_SIZE(v) + needed) < 0) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4174 Py_DECREF(x);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4175 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4176 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4177 p = PyUnicode_AS_UNICODE(v) + oldpos;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4178 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4179 Py_UNICODE_COPY(p,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4180 PyUnicode_AS_UNICODE(x),
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4181 targetsize);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4182 p += targetsize;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4183 extrachars -= targetsize;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4184 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4185 /* 1-0 mapping: skip the character */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4186 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4187 else {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4188 /* wrong return value */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4189 PyErr_SetString(PyExc_TypeError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4190 "character mapping must return integer, None or unicode");
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4191 Py_DECREF(x);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4192 goto onError;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4193 }
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4194 Py_DECREF(x);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4195 ++s;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4196 }
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4197 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4198 if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v))
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4199 if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4200 goto onError;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4201 Py_XDECREF(errorHandler);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4202 Py_XDECREF(exc);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4203 return (PyObject *)v;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
4204
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4205 onError:
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4206 Py_XDECREF(errorHandler);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4207 Py_XDECREF(exc);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4208 Py_XDECREF(v);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4209 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4210 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4211
38586
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4212 /* Charmap encoding: the lookup table */
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4213
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4214 struct encoding_map{
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4215 PyObject_HEAD
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4216 unsigned char level1[32];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4217 int count2, count3;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4218 unsigned char level23[1];
38586
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4219 };
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4220
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4221 static PyObject*
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4222 encoding_map_size(PyObject *obj, PyObject* args)
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4223 {
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4224 struct encoding_map *map = (struct encoding_map*)obj;
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4225 return PyInt_FromLong(sizeof(*map) - 1 + 16*map->count2 +
38586
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4226 128*map->count3);
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4227 }
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4228
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4229 static PyMethodDef encoding_map_methods[] = {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4230 {"size", encoding_map_size, METH_NOARGS,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4231 PyDoc_STR("Return the size (in bytes) of this object") },
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4232 { 0 }
38586
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4233 };
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4234
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4235 static void
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4236 encoding_map_dealloc(PyObject* o)
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4237 {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4238 PyObject_FREE(o);
38586
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4239 }
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4240
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4241 static PyTypeObject EncodingMapType = {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4242 PyVarObject_HEAD_INIT(NULL, 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4243 "EncodingMap", /*tp_name*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4244 sizeof(struct encoding_map), /*tp_basicsize*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4245 0, /*tp_itemsize*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4246 /* methods */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4247 encoding_map_dealloc, /*tp_dealloc*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4248 0, /*tp_print*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4249 0, /*tp_getattr*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4250 0, /*tp_setattr*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4251 0, /*tp_compare*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4252 0, /*tp_repr*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4253 0, /*tp_as_number*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4254 0, /*tp_as_sequence*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4255 0, /*tp_as_mapping*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4256 0, /*tp_hash*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4257 0, /*tp_call*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4258 0, /*tp_str*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4259 0, /*tp_getattro*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4260 0, /*tp_setattro*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4261 0, /*tp_as_buffer*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4262 Py_TPFLAGS_DEFAULT, /*tp_flags*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4263 0, /*tp_doc*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4264 0, /*tp_traverse*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4265 0, /*tp_clear*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4266 0, /*tp_richcompare*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4267 0, /*tp_weaklistoffset*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4268 0, /*tp_iter*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4269 0, /*tp_iternext*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4270 encoding_map_methods, /*tp_methods*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4271 0, /*tp_members*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4272 0, /*tp_getset*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4273 0, /*tp_base*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4274 0, /*tp_dict*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4275 0, /*tp_descr_get*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4276 0, /*tp_descr_set*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4277 0, /*tp_dictoffset*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4278 0, /*tp_init*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4279 0, /*tp_alloc*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4280 0, /*tp_new*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4281 0, /*tp_free*/
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4282 0, /*tp_is_gc*/
38586
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4283 };
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4284
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4285 PyObject*
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4286 PyUnicode_BuildEncodingMap(PyObject* string)
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4287 {
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4288 Py_UNICODE *decode;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4289 PyObject *result;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4290 struct encoding_map *mresult;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4291 int i;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4292 int need_dict = 0;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4293 unsigned char level1[32];
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4294 unsigned char level2[512];
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4295 unsigned char *mlevel1, *mlevel2, *mlevel3;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4296 int count2 = 0, count3 = 0;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4297
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4298 if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) {
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4299 PyErr_BadArgument();
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4300 return NULL;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4301 }
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4302 decode = PyUnicode_AS_UNICODE(string);
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4303 memset(level1, 0xFF, sizeof level1);
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4304 memset(level2, 0xFF, sizeof level2);
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4305
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4306 /* If there isn't a one-to-one mapping of NULL to \0,
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4307 or if there are non-BMP characters, we need to use
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4308 a mapping dictionary. */
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4309 if (decode[0] != 0)
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4310 need_dict = 1;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4311 for (i = 1; i < 256; i++) {
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4312 int l1, l2;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4313 if (decode[i] == 0
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4314 #ifdef Py_UNICODE_WIDE
38586
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4315 || decode[i] > 0xFFFF
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4316 #endif
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4317 ) {
38586
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4318 need_dict = 1;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4319 break;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4320 }
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4321 if (decode[i] == 0xFFFE)
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4322 /* unmapped character */
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4323 continue;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4324 l1 = decode[i] >> 11;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4325 l2 = decode[i] >> 7;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4326 if (level1[l1] == 0xFF)
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4327 level1[l1] = count2++;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4328 if (level2[l2] == 0xFF)
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4329 level2[l2] = count3++;
38586
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4330 }
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4331
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4332 if (count2 >= 0xFF || count3 >= 0xFF)
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4333 need_dict = 1;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4334
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4335 if (need_dict) {
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4336 PyObject *result = PyDict_New();
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4337 PyObject *key, *value;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4338 if (!result)
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4339 return NULL;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4340 for (i = 0; i < 256; i++) {
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4341 key = value = NULL;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4342 key = PyInt_FromLong(decode[i]);
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4343 value = PyInt_FromLong(i);
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4344 if (!key || !value)
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4345 goto failed1;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4346 if (PyDict_SetItem(result, key, value) == -1)
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4347 goto failed1;
38587
21969b71c36d Repair refleaks in unicodeobject.
Georg Brandl <georg@python.org>
parents: 38586
diff changeset
4348 Py_DECREF(key);
21969b71c36d Repair refleaks in unicodeobject.
Georg Brandl <georg@python.org>
parents: 38586
diff changeset
4349 Py_DECREF(value);
38586
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4350 }
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4351 return result;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4352 failed1:
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4353 Py_XDECREF(key);
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4354 Py_XDECREF(value);
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4355 Py_DECREF(result);
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4356 return NULL;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4357 }
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4358
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4359 /* Create a three-level trie */
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4360 result = PyObject_MALLOC(sizeof(struct encoding_map) +
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4361 16*count2 + 128*count3 - 1);
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4362 if (!result)
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4363 return PyErr_NoMemory();
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4364 PyObject_Init(result, &EncodingMapType);
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4365 mresult = (struct encoding_map*)result;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4366 mresult->count2 = count2;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4367 mresult->count3 = count3;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4368 mlevel1 = mresult->level1;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4369 mlevel2 = mresult->level23;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4370 mlevel3 = mresult->level23 + 16*count2;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4371 memcpy(mlevel1, level1, 32);
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4372 memset(mlevel2, 0xFF, 16*count2);
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4373 memset(mlevel3, 0, 128*count3);
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4374 count3 = 0;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4375 for (i = 1; i < 256; i++) {
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4376 int o1, o2, o3, i2, i3;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4377 if (decode[i] == 0xFFFE)
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4378 /* unmapped character */
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4379 continue;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4380 o1 = decode[i]>>11;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4381 o2 = (decode[i]>>7) & 0xF;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4382 i2 = 16*mlevel1[o1] + o2;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4383 if (mlevel2[i2] == 0xFF)
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4384 mlevel2[i2] = count3++;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4385 o3 = decode[i] & 0x7F;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4386 i3 = 128*mlevel2[i2] + o3;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4387 mlevel3[i3] = i;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4388 }
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4389 return result;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4390 }
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4391
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4392 static int
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4393 encoding_map_lookup(Py_UNICODE c, PyObject *mapping)
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4394 {
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4395 struct encoding_map *map = (struct encoding_map*)mapping;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4396 int l1 = c>>11;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4397 int l2 = (c>>7) & 0xF;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4398 int l3 = c & 0x7F;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4399 int i;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4400
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4401 #ifdef Py_UNICODE_WIDE
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4402 if (c > 0xFFFF) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4403 return -1;
38586
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4404 }
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4405 #endif
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4406 if (c == 0)
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4407 return 0;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4408 /* level 1*/
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4409 i = map->level1[l1];
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4410 if (i == 0xFF) {
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4411 return -1;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4412 }
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4413 /* level 2*/
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4414 i = map->level23[16*i+l2];
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4415 if (i == 0xFF) {
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4416 return -1;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4417 }
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4418 /* level 3 */
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4419 i = map->level23[16*map->count2 + 128*i + l3];
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4420 if (i == 0) {
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4421 return -1;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4422 }
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4423 return i;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4424 }
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4425
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4426 /* Lookup the character ch in the mapping. If the character
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4427 can't be found, Py_None is returned (or NULL, if another
35806
44fe157255bc fix typos, mostly in comments
Fred Drake <fdrake@acm.org>
parents: 35758
diff changeset
4428 error occurred). */
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4429 static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4430 {
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4431 PyObject *w = PyInt_FromLong((long)c);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4432 PyObject *x;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4433
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4434 if (w == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4435 return NULL;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4436 x = PyObject_GetItem(mapping, w);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4437 Py_DECREF(w);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4438 if (x == NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4439 if (PyErr_ExceptionMatches(PyExc_LookupError)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4440 /* No mapping found means: mapping is undefined. */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4441 PyErr_Clear();
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4442 x = Py_None;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4443 Py_INCREF(x);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4444 return x;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4445 } else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4446 return NULL;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4447 }
27175
47b134bb51c3 Fix charmapencode_lookup(), so that a None value in the mapping
Walter Dörwald <walter@livinglogic.de>
parents: 27170
diff changeset
4448 else if (x == Py_None)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4449 return x;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4450 else if (PyInt_Check(x)) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4451 long value = PyInt_AS_LONG(x);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4452 if (value < 0 || value > 255) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4453 PyErr_SetString(PyExc_TypeError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4454 "character mapping must be in range(256)");
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4455 Py_DECREF(x);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4456 return NULL;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4457 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4458 return x;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4459 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4460 else if (PyString_Check(x))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4461 return x;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4462 else {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4463 /* wrong return value */
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4464 PyErr_SetString(PyExc_TypeError,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4465 "character mapping must return integer, None or str");
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4466 Py_DECREF(x);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4467 return NULL;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4468 }
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4469 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4470
38586
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4471 static int
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4472 charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize)
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4473 {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4474 Py_ssize_t outsize = PyString_GET_SIZE(*outobj);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4475 /* exponentially overallocate to minimize reallocations */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4476 if (requiredsize < 2*outsize)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4477 requiredsize = 2*outsize;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4478 if (_PyString_Resize(outobj, requiredsize)) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4479 return 0;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4480 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4481 return 1;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4482 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4483
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4484 typedef enum charmapencode_result {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4485 enc_SUCCESS, enc_FAILED, enc_EXCEPTION
38586
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4486 }charmapencode_result;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4487 /* lookup the character, put the result in the output string and adjust
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4488 various state variables. Reallocate the output string if not enough
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4489 space is available. Return a new reference to the object that
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4490 was put in the output buffer, or Py_None, if the mapping was undefined
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4491 (in which case no character was written) or NULL, if a
35827
ab3d1e6a0237 Another comment typo fix
Andrew M. Kuchling <amk@amk.ca>
parents: 35826
diff changeset
4492 reallocation error occurred. The caller must decref the result */
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4493 static
38586
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4494 charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4495 PyObject **outobj, Py_ssize_t *outpos)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4496 {
38586
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4497 PyObject *rep;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4498 char *outstart;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
4499 Py_ssize_t outsize = PyString_GET_SIZE(*outobj);
38586
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4500
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43932
diff changeset
4501 if (Py_TYPE(mapping) == &EncodingMapType) {
38586
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4502 int res = encoding_map_lookup(c, mapping);
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4503 Py_ssize_t requiredsize = *outpos+1;
38586
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4504 if (res == -1)
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4505 return enc_FAILED;
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4506 if (outsize<requiredsize)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4507 if (!charmapencode_resize(outobj, outpos, requiredsize))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4508 return enc_EXCEPTION;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
4509 outstart = PyString_AS_STRING(*outobj);
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4510 outstart[(*outpos)++] = (char)res;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4511 return enc_SUCCESS;
38586
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4512 }
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4513
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4514 rep = charmapencode_lookup(c, mapping);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4515 if (rep==NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4516 return enc_EXCEPTION;
38586
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4517 else if (rep==Py_None) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4518 Py_DECREF(rep);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4519 return enc_FAILED;
38586
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4520 } else {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4521 if (PyInt_Check(rep)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4522 Py_ssize_t requiredsize = *outpos+1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4523 if (outsize<requiredsize)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4524 if (!charmapencode_resize(outobj, outpos, requiredsize)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4525 Py_DECREF(rep);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4526 return enc_EXCEPTION;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4527 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
4528 outstart = PyString_AS_STRING(*outobj);
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4529 outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4530 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4531 else {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4532 const char *repchars = PyString_AS_STRING(rep);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4533 Py_ssize_t repsize = PyString_GET_SIZE(rep);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4534 Py_ssize_t requiredsize = *outpos+repsize;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4535 if (outsize<requiredsize)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4536 if (!charmapencode_resize(outobj, outpos, requiredsize)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4537 Py_DECREF(rep);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4538 return enc_EXCEPTION;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4539 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
4540 outstart = PyString_AS_STRING(*outobj);
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4541 memcpy(outstart + *outpos, repchars, repsize);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4542 *outpos += repsize;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4543 }
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4544 }
38587
21969b71c36d Repair refleaks in unicodeobject.
Georg Brandl <georg@python.org>
parents: 38586
diff changeset
4545 Py_DECREF(rep);
38586
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4546 return enc_SUCCESS;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4547 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4548
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4549 /* handle an error in PyUnicode_EncodeCharmap
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4550 Return 0 on success, -1 on error */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4551 static
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4552 int charmap_encoding_error(
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
4553 const Py_UNICODE *p, Py_ssize_t size, Py_ssize_t *inpos, PyObject *mapping,
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4554 PyObject **exceptionObject,
30149
59bad7efff02 Fix refcount leak in PyUnicode_EncodeCharmap(). The bug surfaces
Walter Dörwald <walter@livinglogic.de>
parents: 29706
diff changeset
4555 int *known_errorHandler, PyObject **errorHandler, const char *errors,
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
4556 PyObject **res, Py_ssize_t *respos)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4557 {
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4558 PyObject *repunicode = NULL; /* initialize to prevent gcc warning */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
4559 Py_ssize_t repsize;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
4560 Py_ssize_t newpos;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4561 Py_UNICODE *uni2;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4562 /* startpos for collecting unencodable chars */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
4563 Py_ssize_t collstartpos = *inpos;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
4564 Py_ssize_t collendpos = *inpos+1;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
4565 Py_ssize_t collpos;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4566 char *encoding = "charmap";
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4567 char *reason = "character maps to <undefined>";
38586
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4568 charmapencode_result x;
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4569
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4570 /* find all unencodable characters */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4571 while (collendpos < size) {
38586
9e0b050ce46f Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents: 38441
diff changeset
4572 PyObject *rep;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43932
diff changeset
4573 if (Py_TYPE(mapping) == &EncodingMapType) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4574 int res = encoding_map_lookup(p[collendpos], mapping);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4575 if (res != -1)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4576 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4577 ++collendpos;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4578 continue;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4579 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4580
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4581 rep = charmapencode_lookup(p[collendpos], mapping);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4582 if (rep==NULL)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4583 return -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4584 else if (rep!=Py_None) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4585 Py_DECREF(rep);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4586 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4587 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4588 Py_DECREF(rep);
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4589 ++collendpos;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4590 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4591 /* cache callback name lookup
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4592 * (if not done yet, i.e. it's the first error) */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4593 if (*known_errorHandler==-1) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4594 if ((errors==NULL) || (!strcmp(errors, "strict")))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4595 *known_errorHandler = 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4596 else if (!strcmp(errors, "replace"))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4597 *known_errorHandler = 2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4598 else if (!strcmp(errors, "ignore"))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4599 *known_errorHandler = 3;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4600 else if (!strcmp(errors, "xmlcharrefreplace"))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4601 *known_errorHandler = 4;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4602 else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4603 *known_errorHandler = 0;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4604 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4605 switch (*known_errorHandler) {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4606 case 1: /* strict */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4607 raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4608 return -1;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4609 case 2: /* replace */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4610 for (collpos = collstartpos; collpos<collendpos; ++collpos) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4611 x = charmapencode_output('?', mapping, res, respos);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4612 if (x==enc_EXCEPTION) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4613 return -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4614 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4615 else if (x==enc_FAILED) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4616 raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4617 return -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4618 }
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4619 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4620 /* fall through */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4621 case 3: /* ignore */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4622 *inpos = collendpos;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4623 break;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4624 case 4: /* xmlcharrefreplace */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4625 /* generate replacement (temporarily (mis)uses p) */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4626 for (collpos = collstartpos; collpos < collendpos; ++collpos) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4627 char buffer[2+29+1+1];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4628 char *cp;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4629 sprintf(buffer, "&#%d;", (int)p[collpos]);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4630 for (cp = buffer; *cp; ++cp) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4631 x = charmapencode_output(*cp, mapping, res, respos);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4632 if (x==enc_EXCEPTION)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4633 return -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4634 else if (x==enc_FAILED) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4635 raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4636 return -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4637 }
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4638 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4639 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4640 *inpos = collendpos;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4641 break;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4642 default:
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4643 repunicode = unicode_encode_call_errorhandler(errors, errorHandler,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4644 encoding, reason, p, size, exceptionObject,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4645 collstartpos, collendpos, &newpos);
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4646 if (repunicode == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4647 return -1;
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4648 /* generate replacement */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4649 repsize = PyUnicode_GET_SIZE(repunicode);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4650 for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4651 x = charmapencode_output(*uni2, mapping, res, respos);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4652 if (x==enc_EXCEPTION) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4653 return -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4654 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4655 else if (x==enc_FAILED) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4656 Py_DECREF(repunicode);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4657 raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4658 return -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4659 }
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4660 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4661 *inpos = newpos;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4662 Py_DECREF(repunicode);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4663 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4664 return 0;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4665 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4666
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4667 PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4668 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4669 PyObject *mapping,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4670 const char *errors)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4671 {
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4672 /* output object */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4673 PyObject *res = NULL;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4674 /* current input position */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
4675 Py_ssize_t inpos = 0;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4676 /* current output position */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
4677 Py_ssize_t respos = 0;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4678 PyObject *errorHandler = NULL;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4679 PyObject *exc = NULL;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4680 /* the following variable is used for caching string comparisons
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4681 * -1=not initialized, 0=unknown, 1=strict, 2=replace,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4682 * 3=ignore, 4=xmlcharrefreplace */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4683 int known_errorHandler = -1;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4684
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4685 /* Default to Latin-1 */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4686 if (mapping == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4687 return PyUnicode_EncodeLatin1(p, size, errors);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4688
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4689 /* allocate enough for a simple encoding without
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4690 replacements, if we need more, we'll resize */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
4691 res = PyString_FromStringAndSize(NULL, size);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4692 if (res == NULL)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4693 goto onError;
13966
91ddeed12fa7 Fixed a couple of instances where a 0-length string was being
Marc-André Lemburg <mal@egenix.com>
parents: 13946
diff changeset
4694 if (size == 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4695 return res;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4696
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4697 while (inpos<size) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4698 /* try to encode it */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4699 charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4700 if (x==enc_EXCEPTION) /* error */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4701 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4702 if (x==enc_FAILED) { /* unencodable character */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4703 if (charmap_encoding_error(p, size, &inpos, mapping,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4704 &exc,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4705 &known_errorHandler, &errorHandler, errors,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4706 &res, &respos)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4707 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4708 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4709 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4710 else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4711 /* done with this character => adjust input position */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4712 ++inpos;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4713 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4714
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4715 /* Resize if we allocated to much */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
4716 if (respos<PyString_GET_SIZE(res)) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4717 if (_PyString_Resize(&res, respos))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4718 goto onError;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4719 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4720 Py_XDECREF(exc);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4721 Py_XDECREF(errorHandler);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4722 return res;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4723
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4724 onError:
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4725 Py_XDECREF(res);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4726 Py_XDECREF(exc);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4727 Py_XDECREF(errorHandler);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4728 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4729 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4730
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4731 PyObject *PyUnicode_AsCharmapString(PyObject *unicode,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4732 PyObject *mapping)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4733 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4734 if (!PyUnicode_Check(unicode) || mapping == NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4735 PyErr_BadArgument();
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4736 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4737 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4738 return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode),
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4739 PyUnicode_GET_SIZE(unicode),
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4740 mapping,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4741 NULL);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4742 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4743
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4744 /* create or adjust a UnicodeTranslateError */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4745 static void make_translate_exception(PyObject **exceptionObject,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4746 const Py_UNICODE *unicode, Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4747 Py_ssize_t startpos, Py_ssize_t endpos,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4748 const char *reason)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4749 {
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4750 if (*exceptionObject == NULL) {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4751 *exceptionObject = PyUnicodeTranslateError_Create(
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4752 unicode, size, startpos, endpos, reason);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4753 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4754 else {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4755 if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4756 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4757 if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4758 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4759 if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4760 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4761 return;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4762 onError:
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4763 Py_DECREF(*exceptionObject);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4764 *exceptionObject = NULL;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4765 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4766 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4767
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4768 /* raises a UnicodeTranslateError */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4769 static void raise_translate_exception(PyObject **exceptionObject,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4770 const Py_UNICODE *unicode, Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4771 Py_ssize_t startpos, Py_ssize_t endpos,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4772 const char *reason)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4773 {
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4774 make_translate_exception(exceptionObject,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4775 unicode, size, startpos, endpos, reason);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4776 if (*exceptionObject != NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4777 PyCodec_StrictErrors(*exceptionObject);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4778 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4779
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4780 /* error handling callback helper:
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4781 build arguments, call the callback and check the arguments,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4782 put the result into newpos and return the replacement string, which
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4783 has to be freed by the caller */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4784 static PyObject *unicode_translate_call_errorhandler(const char *errors,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4785 PyObject **errorHandler,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4786 const char *reason,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4787 const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4788 Py_ssize_t startpos, Py_ssize_t endpos,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4789 Py_ssize_t *newpos)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4790 {
37662
0678ce9fb6b2 Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 37661
diff changeset
4791 static char *argparse = "O!n;translating error handler must return (unicode, int) tuple";
0678ce9fb6b2 Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 37661
diff changeset
4792
0678ce9fb6b2 Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 37661
diff changeset
4793 Py_ssize_t i_newpos;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4794 PyObject *restuple;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4795 PyObject *resunicode;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4796
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4797 if (*errorHandler == NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4798 *errorHandler = PyCodec_LookupError(errors);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4799 if (*errorHandler == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4800 return NULL;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4801 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4802
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4803 make_translate_exception(exceptionObject,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4804 unicode, size, startpos, endpos, reason);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4805 if (*exceptionObject == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4806 return NULL;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4807
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4808 restuple = PyObject_CallFunctionObjArgs(
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4809 *errorHandler, *exceptionObject, NULL);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4810 if (restuple == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4811 return NULL;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4812 if (!PyTuple_Check(restuple)) {
51598
512c5a5de00b Fix warnings GCC emits where the argument of PyErr_Format is a single variable.
Georg Brandl <georg@python.org>
parents: 51218
diff changeset
4813 PyErr_SetString(PyExc_TypeError, &argparse[4]);
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4814 Py_DECREF(restuple);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4815 return NULL;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4816 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4817 if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4818 &resunicode, &i_newpos)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4819 Py_DECREF(restuple);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4820 return NULL;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4821 }
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
4822 if (i_newpos<0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4823 *newpos = size+i_newpos;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
4824 else
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
4825 *newpos = i_newpos;
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27521
diff changeset
4826 if (*newpos<0 || *newpos>size) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4827 PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4828 Py_DECREF(restuple);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4829 return NULL;
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27521
diff changeset
4830 }
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4831 Py_INCREF(resunicode);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4832 Py_DECREF(restuple);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4833 return resunicode;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4834 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4835
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4836 /* Lookup the character ch in the mapping and put the result in result,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4837 which must be decrefed by the caller.
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4838 Return 0 on success, -1 on error */
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4839 static
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4840 int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4841 {
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4842 PyObject *w = PyInt_FromLong((long)c);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4843 PyObject *x;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4844
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4845 if (w == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4846 return -1;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4847 x = PyObject_GetItem(mapping, w);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4848 Py_DECREF(w);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4849 if (x == NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4850 if (PyErr_ExceptionMatches(PyExc_LookupError)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4851 /* No mapping found means: use 1:1 mapping. */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4852 PyErr_Clear();
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4853 *result = NULL;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4854 return 0;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4855 } else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4856 return -1;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4857 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4858 else if (x == Py_None) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4859 *result = x;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4860 return 0;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4861 }
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4862 else if (PyInt_Check(x)) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4863 long value = PyInt_AS_LONG(x);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4864 long max = PyUnicode_GetMax();
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4865 if (value < 0 || value > max) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4866 PyErr_Format(PyExc_TypeError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4867 "character mapping must be in range(0x%lx)", max+1);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4868 Py_DECREF(x);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4869 return -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4870 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4871 *result = x;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4872 return 0;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4873 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4874 else if (PyUnicode_Check(x)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4875 *result = x;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4876 return 0;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4877 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4878 else {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4879 /* wrong return value */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4880 PyErr_SetString(PyExc_TypeError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4881 "character mapping must return integer, None or unicode");
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4882 Py_DECREF(x);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4883 return -1;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4884 }
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4885 }
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4886 /* ensure that *outobj is at least requiredsize characters long,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4887 if not reallocate and adjust various state variables.
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4888 Return 0 on success, -1 on error */
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4889 static
30765
fe3f5592305d Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents: 30707
diff changeset
4890 int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4891 Py_ssize_t requiredsize)
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
4892 {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
4893 Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj);
30765
fe3f5592305d Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents: 30707
diff changeset
4894 if (requiredsize > oldsize) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4895 /* remember old output position */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4896 Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4897 /* exponentially overallocate to minimize reallocations */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4898 if (requiredsize < 2 * oldsize)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4899 requiredsize = 2 * oldsize;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4900 if (PyUnicode_Resize(outobj, requiredsize) < 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4901 return -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4902 *outp = PyUnicode_AS_UNICODE(*outobj) + outpos;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4903 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4904 return 0;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4905 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4906 /* lookup the character, put the result in the output string and adjust
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4907 various state variables. Return a new reference to the object that
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4908 was put in the output buffer in *result, or Py_None, if the mapping was
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4909 undefined (in which case no character was written).
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4910 The called must decref result.
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4911 Return 0 on success, -1 on error. */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4912 static
30765
fe3f5592305d Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents: 30707
diff changeset
4913 int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4914 Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4915 PyObject **res)
30765
fe3f5592305d Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents: 30707
diff changeset
4916 {
fe3f5592305d Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents: 30707
diff changeset
4917 if (charmaptranslate_lookup(*curinp, mapping, res))
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4918 return -1;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4919 if (*res==NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4920 /* not found => default to 1:1 mapping */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4921 *(*outp)++ = *curinp;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4922 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4923 else if (*res==Py_None)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4924 ;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4925 else if (PyInt_Check(*res)) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4926 /* no overflow check, because we know that the space is enough */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4927 *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res);
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4928 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4929 else if (PyUnicode_Check(*res)) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4930 Py_ssize_t repsize = PyUnicode_GET_SIZE(*res);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4931 if (repsize==1) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4932 /* no overflow check, because we know that the space is enough */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4933 *(*outp)++ = *PyUnicode_AS_UNICODE(*res);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4934 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4935 else if (repsize!=0) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4936 /* more than one character */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4937 Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) +
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4938 (insize - (curinp-startinp)) +
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4939 repsize - 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4940 if (charmaptranslate_makespace(outobj, outp, requiredsize))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4941 return -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4942 memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4943 *outp += repsize;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4944 }
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4945 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4946 else
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4947 return -1;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4948 return 0;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4949 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4950
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4951 PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4952 Py_ssize_t size,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4953 PyObject *mapping,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4954 const char *errors)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4955 {
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4956 /* output object */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4957 PyObject *res = NULL;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4958 /* pointers to the beginning and end+1 of input */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4959 const Py_UNICODE *startp = p;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4960 const Py_UNICODE *endp = p + size;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4961 /* pointer into the output */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4962 Py_UNICODE *str;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4963 /* current output position */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
4964 Py_ssize_t respos = 0;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4965 char *reason = "character maps to <undefined>";
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4966 PyObject *errorHandler = NULL;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4967 PyObject *exc = NULL;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4968 /* the following variable is used for caching string comparisons
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4969 * -1=not initialized, 0=unknown, 1=strict, 2=replace,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4970 * 3=ignore, 4=xmlcharrefreplace */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4971 int known_errorHandler = -1;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4972
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4973 if (mapping == NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4974 PyErr_BadArgument();
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4975 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4976 }
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4977
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4978 /* allocate enough for a simple 1:1 translation without
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4979 replacements, if we need more, we'll resize */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4980 res = PyUnicode_FromUnicode(NULL, size);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4981 if (res == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4982 goto onError;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4983 if (size == 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4984 return res;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4985 str = PyUnicode_AS_UNICODE(res);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4986
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
4987 while (p<endp) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4988 /* try to encode it */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4989 PyObject *x = NULL;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4990 if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4991 Py_XDECREF(x);
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4992 goto onError;
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4993 }
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
4994 Py_XDECREF(x);
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4995 if (x!=Py_None) /* it worked => adjust input pointer */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4996 ++p;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4997 else { /* untranslatable character */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4998 PyObject *repunicode = NULL; /* initialize to prevent gcc warning */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
4999 Py_ssize_t repsize;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5000 Py_ssize_t newpos;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5001 Py_UNICODE *uni2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5002 /* startpos for collecting untranslatable chars */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5003 const Py_UNICODE *collstart = p;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5004 const Py_UNICODE *collend = p+1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5005 const Py_UNICODE *coll;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5006
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5007 /* find all untranslatable characters */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5008 while (collend < endp) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5009 if (charmaptranslate_lookup(*collend, mapping, &x))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5010 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5011 Py_XDECREF(x);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5012 if (x!=Py_None)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5013 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5014 ++collend;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5015 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5016 /* cache callback name lookup
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5017 * (if not done yet, i.e. it's the first error) */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5018 if (known_errorHandler==-1) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5019 if ((errors==NULL) || (!strcmp(errors, "strict")))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5020 known_errorHandler = 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5021 else if (!strcmp(errors, "replace"))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5022 known_errorHandler = 2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5023 else if (!strcmp(errors, "ignore"))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5024 known_errorHandler = 3;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5025 else if (!strcmp(errors, "xmlcharrefreplace"))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5026 known_errorHandler = 4;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5027 else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5028 known_errorHandler = 0;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5029 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5030 switch (known_errorHandler) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5031 case 1: /* strict */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5032 raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason);
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
5033 goto onError;
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5034 case 2: /* replace */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5035 /* No need to check for space, this is a 1:1 replacement */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5036 for (coll = collstart; coll<collend; ++coll)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5037 *str++ = '?';
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5038 /* fall through */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5039 case 3: /* ignore */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5040 p = collend;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5041 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5042 case 4: /* xmlcharrefreplace */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5043 /* generate replacement (temporarily (mis)uses p) */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5044 for (p = collstart; p < collend; ++p) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5045 char buffer[2+29+1+1];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5046 char *cp;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5047 sprintf(buffer, "&#%d;", (int)*p);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5048 if (charmaptranslate_makespace(&res, &str,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5049 (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend)))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5050 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5051 for (cp = buffer; *cp; ++cp)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5052 *str++ = *cp;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5053 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5054 p = collend;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5055 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5056 default:
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5057 repunicode = unicode_translate_call_errorhandler(errors, &errorHandler,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5058 reason, startp, size, &exc,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5059 collstart-startp, collend-startp, &newpos);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5060 if (repunicode == NULL)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5061 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5062 /* generate replacement */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5063 repsize = PyUnicode_GET_SIZE(repunicode);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5064 if (charmaptranslate_makespace(&res, &str,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5065 (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5066 Py_DECREF(repunicode);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5067 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5068 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5069 for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5070 *str++ = *uni2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5071 p = startp + newpos;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5072 Py_DECREF(repunicode);
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
5073 }
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5074 }
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
5075 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
5076 /* Resize if we allocated to much */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
5077 respos = str-PyUnicode_AS_UNICODE(res);
30765
fe3f5592305d Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents: 30707
diff changeset
5078 if (respos<PyUnicode_GET_SIZE(res)) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5079 if (PyUnicode_Resize(&res, respos) < 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5080 goto onError;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
5081 }
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
5082 Py_XDECREF(exc);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
5083 Py_XDECREF(errorHandler);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
5084 return res;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
5085
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5086 onError:
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
5087 Py_XDECREF(res);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
5088 Py_XDECREF(exc);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
5089 Py_XDECREF(errorHandler);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5090 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5091 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5092
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5093 PyObject *PyUnicode_Translate(PyObject *str,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5094 PyObject *mapping,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5095 const char *errors)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5096 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5097 PyObject *result;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
5098
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5099 str = PyUnicode_FromObject(str);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5100 if (str == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5101 goto onError;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5102 result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str),
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5103 PyUnicode_GET_SIZE(str),
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5104 mapping,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5105 errors);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5106 Py_DECREF(str);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5107 return result;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
5108
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5109 onError:
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5110 Py_XDECREF(str);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5111 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5112 }
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
5113
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12193
diff changeset
5114 /* --- Decimal Encoder ---------------------------------------------------- */
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12193
diff changeset
5115
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12193
diff changeset
5116 int PyUnicode_EncodeDecimal(Py_UNICODE *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5117 Py_ssize_t length,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5118 char *output,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5119 const char *errors)
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12193
diff changeset
5120 {
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12193
diff changeset
5121 Py_UNICODE *p, *end;
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
5122 PyObject *errorHandler = NULL;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
5123 PyObject *exc = NULL;
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
5124 const char *encoding = "decimal";
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
5125 const char *reason = "invalid decimal Unicode string";
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
5126 /* the following variable is used for caching string comparisons
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
5127 * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
5128 int known_errorHandler = -1;
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12193
diff changeset
5129
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12193
diff changeset
5130 if (output == NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5131 PyErr_BadArgument();
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5132 return -1;
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12193
diff changeset
5133 }
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12193
diff changeset
5134
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12193
diff changeset
5135 p = s;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12193
diff changeset
5136 end = s + length;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12193
diff changeset
5137 while (p < end) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5138 register Py_UNICODE ch = *p;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5139 int decimal;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5140 PyObject *repunicode;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5141 Py_ssize_t repsize;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5142 Py_ssize_t newpos;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5143 Py_UNICODE *uni2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5144 Py_UNICODE *collstart;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5145 Py_UNICODE *collend;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5146
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5147 if (Py_UNICODE_ISSPACE(ch)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5148 *output++ = ' ';
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5149 ++p;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5150 continue;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5151 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5152 decimal = Py_UNICODE_TODECIMAL(ch);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5153 if (decimal >= 0) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5154 *output++ = '0' + decimal;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5155 ++p;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5156 continue;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5157 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5158 if (0 < ch && ch < 256) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5159 *output++ = (char)ch;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5160 ++p;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5161 continue;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5162 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5163 /* All other characters are considered unencodable */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5164 collstart = p;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5165 collend = p+1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5166 while (collend < end) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5167 if ((0 < *collend && *collend < 256) ||
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5168 !Py_UNICODE_ISSPACE(*collend) ||
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5169 Py_UNICODE_TODECIMAL(*collend))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5170 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5171 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5172 /* cache callback name lookup
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5173 * (if not done yet, i.e. it's the first error) */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5174 if (known_errorHandler==-1) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5175 if ((errors==NULL) || (!strcmp(errors, "strict")))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5176 known_errorHandler = 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5177 else if (!strcmp(errors, "replace"))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5178 known_errorHandler = 2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5179 else if (!strcmp(errors, "ignore"))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5180 known_errorHandler = 3;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5181 else if (!strcmp(errors, "xmlcharrefreplace"))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5182 known_errorHandler = 4;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5183 else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5184 known_errorHandler = 0;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5185 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5186 switch (known_errorHandler) {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
5187 case 1: /* strict */
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5188 raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason);
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
5189 goto onError;
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5190 case 2: /* replace */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5191 for (p = collstart; p < collend; ++p)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5192 *output++ = '?';
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5193 /* fall through */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5194 case 3: /* ignore */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5195 p = collend;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5196 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5197 case 4: /* xmlcharrefreplace */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5198 /* generate replacement (temporarily (mis)uses p) */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5199 for (p = collstart; p < collend; ++p)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5200 output += sprintf(output, "&#%d;", (int)*p);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5201 p = collend;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5202 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5203 default:
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5204 repunicode = unicode_encode_call_errorhandler(errors, &errorHandler,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5205 encoding, reason, s, length, &exc,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5206 collstart-s, collend-s, &newpos);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5207 if (repunicode == NULL)
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
5208 goto onError;
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5209 /* generate replacement */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5210 repsize = PyUnicode_GET_SIZE(repunicode);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5211 for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5212 Py_UNICODE ch = *uni2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5213 if (Py_UNICODE_ISSPACE(ch))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5214 *output++ = ' ';
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5215 else {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5216 decimal = Py_UNICODE_TODECIMAL(ch);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5217 if (decimal >= 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5218 *output++ = '0' + decimal;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5219 else if (0 < ch && ch < 256)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5220 *output++ = (char)ch;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5221 else {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5222 Py_DECREF(repunicode);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5223 raise_encode_exception(&exc, encoding,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5224 s, length, collstart-s, collend-s, reason);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5225 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5226 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5227 }
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
5228 }
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5229 p = s + newpos;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5230 Py_DECREF(repunicode);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5231 }
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12193
diff changeset
5232 }
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12193
diff changeset
5233 /* 0-terminate the output string */
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12193
diff changeset
5234 *output++ = '\0';
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
5235 Py_XDECREF(exc);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
5236 Py_XDECREF(errorHandler);
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12193
diff changeset
5237 return 0;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12193
diff changeset
5238
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5239 onError:
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
5240 Py_XDECREF(exc);
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
5241 Py_XDECREF(errorHandler);
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12193
diff changeset
5242 return -1;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12193
diff changeset
5243 }
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 12193
diff changeset
5244
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5245 /* --- Helpers ------------------------------------------------------------ */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5246
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45341
diff changeset
5247 #include "stringlib/unicodedefs.h"
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45341
diff changeset
5248
43888
1fb353776dfb Made _ParseTupleFinds only defined to unicodeobject.c
Facundo Batista <facundobatista@gmail.com>
parents: 43886
diff changeset
5249 #define FROM_UNICODE
38383
05744917b7c3 needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38381
diff changeset
5250
38381
b32f9bb2e077 needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38347
diff changeset
5251 #include "stringlib/fastsearch.h"
38400
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
5252
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
5253 #include "stringlib/count.h"
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
5254 #include "stringlib/find.h"
38383
05744917b7c3 needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38381
diff changeset
5255 #include "stringlib/partition.h"
05744917b7c3 needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38381
diff changeset
5256
38402
6b03a47eb54d needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents: 38401
diff changeset
5257 /* helper macro to fixup start/end slice values */
6b03a47eb54d needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents: 38401
diff changeset
5258 #define FIX_START_END(obj) \
6b03a47eb54d needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents: 38401
diff changeset
5259 if (start < 0) \
6b03a47eb54d needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents: 38401
diff changeset
5260 start += (obj)->length; \
6b03a47eb54d needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents: 38401
diff changeset
5261 if (start < 0) \
6b03a47eb54d needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents: 38401
diff changeset
5262 start = 0; \
6b03a47eb54d needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents: 38401
diff changeset
5263 if (end > (obj)->length) \
6b03a47eb54d needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents: 38401
diff changeset
5264 end = (obj)->length; \
6b03a47eb54d needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents: 38401
diff changeset
5265 if (end < 0) \
6b03a47eb54d needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents: 38401
diff changeset
5266 end += (obj)->length; \
6b03a47eb54d needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents: 38401
diff changeset
5267 if (end < 0) \
6b03a47eb54d needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents: 38401
diff changeset
5268 end = 0;
6b03a47eb54d needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents: 38401
diff changeset
5269
38400
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
5270 Py_ssize_t PyUnicode_Count(PyObject *str,
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
5271 PyObject *substr,
38347
6d8615f9f1cc use Py_LOCAL also for string and unicode objects
Fredrik Lundh <fredrik@pythonware.com>
parents: 38339
diff changeset
5272 Py_ssize_t start,
38400
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
5273 Py_ssize_t end)
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
5274 {
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
5275 Py_ssize_t result;
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
5276 PyUnicodeObject* str_obj;
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
5277 PyUnicodeObject* sub_obj;
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
5278
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
5279 str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str);
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
5280 if (!str_obj)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5281 return -1;
38400
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
5282 sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr);
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
5283 if (!sub_obj) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5284 Py_DECREF(str_obj);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5285 return -1;
38400
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
5286 }
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5287
38402
6b03a47eb54d needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents: 38401
diff changeset
5288 FIX_START_END(str_obj);
15910
f7f3be3d00b9 Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents: 15749
diff changeset
5289
38400
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
5290 result = stringlib_count(
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
5291 str_obj->str + start, end - start, sub_obj->str, sub_obj->length
38275
43b1474b512b needforspeed: use "fastsearch" for count and findstring helpers. this
Fredrik Lundh <fredrik@pythonware.com>
parents: 38274
diff changeset
5292 );
38335
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
5293
38400
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
5294 Py_DECREF(sub_obj);
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
5295 Py_DECREF(str_obj);
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
5296
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5297 return result;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5298 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5299
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5300 Py_ssize_t PyUnicode_Find(PyObject *str,
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
5301 PyObject *sub,
38401
adc214fc1e1c needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 38400
diff changeset
5302 Py_ssize_t start,
adc214fc1e1c needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 38400
diff changeset
5303 Py_ssize_t end,
adc214fc1e1c needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 38400
diff changeset
5304 int direction)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5305 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5306 Py_ssize_t result;
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
5307
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
5308 str = PyUnicode_FromObject(str);
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
5309 if (!str)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5310 return -2;
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
5311 sub = PyUnicode_FromObject(sub);
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
5312 if (!sub) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5313 Py_DECREF(str);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5314 return -2;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5315 }
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
5316
38401
adc214fc1e1c needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 38400
diff changeset
5317 if (direction > 0)
38441
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
5318 result = stringlib_find_slice(
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
5319 PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str),
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
5320 PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub),
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
5321 start, end
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
5322 );
38401
adc214fc1e1c needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 38400
diff changeset
5323 else
38441
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
5324 result = stringlib_rfind_slice(
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
5325 PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str),
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
5326 PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub),
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
5327 start, end
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
5328 );
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
5329
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
5330 Py_DECREF(str);
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
5331 Py_DECREF(sub);
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
5332
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5333 return result;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5334 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5335
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
5336 static
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5337 int tailmatch(PyUnicodeObject *self,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5338 PyUnicodeObject *substring,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5339 Py_ssize_t start,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5340 Py_ssize_t end,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5341 int direction)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5342 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5343 if (substring->length == 0)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5344 return 1;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5345
38402
6b03a47eb54d needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents: 38401
diff changeset
5346 FIX_START_END(self);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5347
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5348 end -= substring->length;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5349 if (end < start)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5350 return 0;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5351
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5352 if (direction > 0) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5353 if (Py_UNICODE_MATCH(self, end, substring))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5354 return 1;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5355 } else {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5356 if (Py_UNICODE_MATCH(self, start, substring))
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5357 return 1;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5358 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5359
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5360 return 0;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5361 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5362
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5363 Py_ssize_t PyUnicode_Tailmatch(PyObject *str,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5364 PyObject *substr,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5365 Py_ssize_t start,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5366 Py_ssize_t end,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5367 int direction)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5368 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5369 Py_ssize_t result;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
5370
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5371 str = PyUnicode_FromObject(str);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5372 if (str == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5373 return -1;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5374 substr = PyUnicode_FromObject(substr);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5375 if (substr == NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5376 Py_DECREF(str);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5377 return -1;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5378 }
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
5379
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5380 result = tailmatch((PyUnicodeObject *)str,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5381 (PyUnicodeObject *)substr,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5382 start, end, direction);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5383 Py_DECREF(str);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5384 Py_DECREF(substr);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5385 return result;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5386 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5387
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5388 /* Apply fixfct filter to the Unicode object self and return a
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5389 reference to the modified object */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5390
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
5391 static
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5392 PyObject *fixup(PyUnicodeObject *self,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5393 int (*fixfct)(PyUnicodeObject *s))
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5394 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5395
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5396 PyUnicodeObject *u;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5397
17618
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
5398 u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5399 if (u == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5400 return NULL;
17618
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
5401
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
5402 Py_UNICODE_COPY(u->str, self->str, self->length);
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
5403
19885
72e6eb653312 More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19849
diff changeset
5404 if (!fixfct(u) && PyUnicode_CheckExact(self)) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5405 /* fixfct should return TRUE if it modified the buffer. If
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5406 FALSE, return a reference to the original buffer instead
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5407 (to save space, not time) */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5408 Py_INCREF(self);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5409 Py_DECREF(u);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5410 return (PyObject*) self;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5411 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5412 return (PyObject*) u;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5413 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5414
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
5415 static
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5416 int fixupper(PyUnicodeObject *self)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5417 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5418 Py_ssize_t len = self->length;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5419 Py_UNICODE *s = self->str;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5420 int status = 0;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
5421
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5422 while (len-- > 0) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5423 register Py_UNICODE ch;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5424
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5425 ch = Py_UNICODE_TOUPPER(*s);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5426 if (ch != *s) {
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5427 status = 1;
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5428 *s = ch;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5429 }
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5430 s++;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5431 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5432
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5433 return status;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5434 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5435
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
5436 static
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5437 int fixlower(PyUnicodeObject *self)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5438 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5439 Py_ssize_t len = self->length;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5440 Py_UNICODE *s = self->str;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5441 int status = 0;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
5442
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5443 while (len-- > 0) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5444 register Py_UNICODE ch;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5445
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5446 ch = Py_UNICODE_TOLOWER(*s);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5447 if (ch != *s) {
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5448 status = 1;
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5449 *s = ch;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5450 }
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5451 s++;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5452 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5453
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5454 return status;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5455 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5456
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
5457 static
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5458 int fixswapcase(PyUnicodeObject *self)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5459 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5460 Py_ssize_t len = self->length;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5461 Py_UNICODE *s = self->str;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5462 int status = 0;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
5463
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5464 while (len-- > 0) {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5465 if (Py_UNICODE_ISUPPER(*s)) {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5466 *s = Py_UNICODE_TOLOWER(*s);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5467 status = 1;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5468 } else if (Py_UNICODE_ISLOWER(*s)) {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5469 *s = Py_UNICODE_TOUPPER(*s);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5470 status = 1;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5471 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5472 s++;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5473 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5474
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5475 return status;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5476 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5477
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
5478 static
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5479 int fixcapitalize(PyUnicodeObject *self)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5480 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5481 Py_ssize_t len = self->length;
16332
bacbb45fe844 Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents: 16226
diff changeset
5482 Py_UNICODE *s = self->str;
bacbb45fe844 Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents: 16226
diff changeset
5483 int status = 0;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
5484
16332
bacbb45fe844 Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents: 16226
diff changeset
5485 if (len == 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5486 return 0;
16332
bacbb45fe844 Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents: 16226
diff changeset
5487 if (Py_UNICODE_ISLOWER(*s)) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5488 *s = Py_UNICODE_TOUPPER(*s);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5489 status = 1;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5490 }
16332
bacbb45fe844 Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents: 16226
diff changeset
5491 s++;
bacbb45fe844 Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents: 16226
diff changeset
5492 while (--len > 0) {
bacbb45fe844 Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents: 16226
diff changeset
5493 if (Py_UNICODE_ISUPPER(*s)) {
bacbb45fe844 Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents: 16226
diff changeset
5494 *s = Py_UNICODE_TOLOWER(*s);
bacbb45fe844 Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents: 16226
diff changeset
5495 status = 1;
bacbb45fe844 Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents: 16226
diff changeset
5496 }
bacbb45fe844 Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents: 16226
diff changeset
5497 s++;
bacbb45fe844 Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents: 16226
diff changeset
5498 }
bacbb45fe844 Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents: 16226
diff changeset
5499 return status;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5500 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5501
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5502 static
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5503 int fixtitle(PyUnicodeObject *self)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5504 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5505 register Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5506 register Py_UNICODE *e;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5507 int previous_is_cased;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5508
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5509 /* Shortcut for single character strings */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5510 if (PyUnicode_GET_SIZE(self) == 1) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5511 Py_UNICODE ch = Py_UNICODE_TOTITLE(*p);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5512 if (*p != ch) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5513 *p = ch;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5514 return 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5515 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5516 else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5517 return 0;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5518 }
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
5519
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5520 e = p + PyUnicode_GET_SIZE(self);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5521 previous_is_cased = 0;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5522 for (; p < e; p++) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5523 register const Py_UNICODE ch = *p;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5524
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5525 if (previous_is_cased)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5526 *p = Py_UNICODE_TOLOWER(ch);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5527 else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5528 *p = Py_UNICODE_TOTITLE(ch);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5529
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5530 if (Py_UNICODE_ISLOWER(ch) ||
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5531 Py_UNICODE_ISUPPER(ch) ||
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5532 Py_UNICODE_ISTITLE(ch))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5533 previous_is_cased = 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5534 else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5535 previous_is_cased = 0;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5536 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5537 return 1;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5538 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5539
33300
77de38534d26 PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents: 32951
diff changeset
5540 PyObject *
77de38534d26 PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents: 32951
diff changeset
5541 PyUnicode_Join(PyObject *separator, PyObject *seq)
77de38534d26 PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents: 32951
diff changeset
5542 {
77de38534d26 PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents: 32951
diff changeset
5543 PyObject *internal_separator = NULL;
33499
a4d3673bcd66 Initialize sep and seplen to suppress warning from gcc.
Skip Montanaro <skip@pobox.com>
parents: 33495
diff changeset
5544 const Py_UNICODE blank = ' ';
a4d3673bcd66 Initialize sep and seplen to suppress warning from gcc.
Skip Montanaro <skip@pobox.com>
parents: 33495
diff changeset
5545 const Py_UNICODE *sep = &blank;
38240
0373a074004c PyUnicode_Join(): Recent code changes introduced new
Tim Peters <tim.peters@gmail.com>
parents: 38239
diff changeset
5546 Py_ssize_t seplen = 1;
33310
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5547 PyUnicodeObject *res = NULL; /* the result */
38240
0373a074004c PyUnicode_Join(): Recent code changes introduced new
Tim Peters <tim.peters@gmail.com>
parents: 38239
diff changeset
5548 Py_ssize_t res_alloc = 100; /* # allocated bytes for string in res */
0373a074004c PyUnicode_Join(): Recent code changes introduced new
Tim Peters <tim.peters@gmail.com>
parents: 38239
diff changeset
5549 Py_ssize_t res_used; /* # used bytes */
33310
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5550 Py_UNICODE *res_p; /* pointer to free byte in res's string area */
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5551 PyObject *fseq; /* PySequence_Fast(seq) */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5552 Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */
33300
77de38534d26 PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents: 32951
diff changeset
5553 PyObject *item;
37662
0678ce9fb6b2 Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 37661
diff changeset
5554 Py_ssize_t i;
33310
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5555
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5556 fseq = PySequence_Fast(seq, "");
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5557 if (fseq == NULL) {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
5558 return NULL;
33310
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5559 }
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5560
33311
59bd121eeeb7 PyUnicode_Join(): Bozo Alert. While this is chugging along, it may
Tim Peters <tim.peters@gmail.com>
parents: 33310
diff changeset
5561 /* Grrrr. A codec may be invoked to convert str objects to
59bd121eeeb7 PyUnicode_Join(): Bozo Alert. While this is chugging along, it may
Tim Peters <tim.peters@gmail.com>
parents: 33310
diff changeset
5562 * Unicode, and so it's possible to call back into Python code
59bd121eeeb7 PyUnicode_Join(): Bozo Alert. While this is chugging along, it may
Tim Peters <tim.peters@gmail.com>
parents: 33310
diff changeset
5563 * during PyUnicode_FromObject(), and so it's possible for a sick
59bd121eeeb7 PyUnicode_Join(): Bozo Alert. While this is chugging along, it may
Tim Peters <tim.peters@gmail.com>
parents: 33310
diff changeset
5564 * codec to change the size of fseq (if seq is a list). Therefore
59bd121eeeb7 PyUnicode_Join(): Bozo Alert. While this is chugging along, it may
Tim Peters <tim.peters@gmail.com>
parents: 33310
diff changeset
5565 * we have to keep refetching the size -- can't assume seqlen
59bd121eeeb7 PyUnicode_Join(): Bozo Alert. While this is chugging along, it may
Tim Peters <tim.peters@gmail.com>
parents: 33310
diff changeset
5566 * is invariant.
59bd121eeeb7 PyUnicode_Join(): Bozo Alert. While this is chugging along, it may
Tim Peters <tim.peters@gmail.com>
parents: 33310
diff changeset
5567 */
33310
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5568 seqlen = PySequence_Fast_GET_SIZE(fseq);
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5569 /* If empty sequence, return u"". */
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5570 if (seqlen == 0) {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
5571 res = _PyUnicode_New(0); /* empty sequence; return u"" */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
5572 goto Done;
33310
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5573 }
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5574 /* If singleton sequence with an exact Unicode, return that. */
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5575 if (seqlen == 1) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5576 item = PySequence_Fast_GET_ITEM(fseq, 0);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5577 if (PyUnicode_CheckExact(item)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5578 Py_INCREF(item);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5579 res = (PyUnicodeObject *)item;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5580 goto Done;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5581 }
33310
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5582 }
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5583
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5584 /* At least two items to join, or one that isn't exact Unicode. */
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5585 if (seqlen > 1) {
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5586 /* Set up sep and seplen -- they're needed. */
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
5587 if (separator == NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5588 sep = &blank;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5589 seplen = 1;
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
5590 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
5591 else {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5592 internal_separator = PyUnicode_FromObject(separator);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5593 if (internal_separator == NULL)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5594 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5595 sep = PyUnicode_AS_UNICODE(internal_separator);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5596 seplen = PyUnicode_GET_SIZE(internal_separator);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5597 /* In case PyUnicode_FromObject() mutated seq. */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5598 seqlen = PySequence_Fast_GET_SIZE(fseq);
33310
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5599 }
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5600 }
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5601
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5602 /* Get space. */
37662
0678ce9fb6b2 Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 37661
diff changeset
5603 res = _PyUnicode_New(res_alloc);
33310
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5604 if (res == NULL)
33300
77de38534d26 PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents: 32951
diff changeset
5605 goto onError;
33310
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5606 res_p = PyUnicode_AS_UNICODE(res);
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5607 res_used = 0;
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5608
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5609 for (i = 0; i < seqlen; ++i) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5610 Py_ssize_t itemlen;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5611 Py_ssize_t new_res_used;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5612
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5613 item = PySequence_Fast_GET_ITEM(fseq, i);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5614 /* Convert item to Unicode. */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5615 if (! PyUnicode_Check(item) && ! PyString_Check(item)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5616 PyErr_Format(PyExc_TypeError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5617 "sequence item %zd: expected string or Unicode,"
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5618 " %.80s found",
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5619 i, Py_TYPE(item)->tp_name);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5620 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5621 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5622 item = PyUnicode_FromObject(item);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5623 if (item == NULL)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5624 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5625 /* We own a reference to item from here on. */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5626
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5627 /* In case PyUnicode_FromObject() mutated seq. */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5628 seqlen = PySequence_Fast_GET_SIZE(fseq);
33311
59bd121eeeb7 PyUnicode_Join(): Bozo Alert. While this is chugging along, it may
Tim Peters <tim.peters@gmail.com>
parents: 33310
diff changeset
5629
33310
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5630 /* Make sure we have enough space for the separator and the item. */
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5631 itemlen = PyUnicode_GET_SIZE(item);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5632 new_res_used = res_used + itemlen;
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
5633 if (new_res_used < 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5634 goto Overflow;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5635 if (i < seqlen - 1) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5636 new_res_used += seplen;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5637 if (new_res_used < 0)
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
5638 goto Overflow;
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5639 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5640 if (new_res_used > res_alloc) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5641 /* double allocated size until it's big enough */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5642 do {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5643 res_alloc += res_alloc;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5644 if (res_alloc <= 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5645 goto Overflow;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5646 } while (new_res_used > res_alloc);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5647 if (_PyUnicode_Resize(&res, res_alloc) < 0) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5648 Py_DECREF(item);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5649 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5650 }
33310
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5651 res_p = PyUnicode_AS_UNICODE(res) + res_used;
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5652 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5653
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5654 /* Copy item, and maybe the separator. */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5655 Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5656 res_p += itemlen;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5657 if (i < seqlen - 1) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5658 Py_UNICODE_COPY(res_p, sep, seplen);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5659 res_p += seplen;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5660 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5661 Py_DECREF(item);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5662 res_used = new_res_used;
33310
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5663 }
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5664
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5665 /* Shrink res to match the used area; this probably can't fail,
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5666 * but it's cheap to check.
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5667 */
37662
0678ce9fb6b2 Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 37661
diff changeset
5668 if (_PyUnicode_Resize(&res, res_used) < 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5669 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5670
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5671 Done:
33300
77de38534d26 PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents: 32951
diff changeset
5672 Py_XDECREF(internal_separator);
33310
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5673 Py_DECREF(fseq);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5674 return (PyObject *)res;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5675
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5676 Overflow:
33300
77de38534d26 PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents: 32951
diff changeset
5677 PyErr_SetString(PyExc_OverflowError,
38709
67450db26f81 Apply perky's fix for #1503157: "/".join([u"", u""]) raising OverflowError.
Georg Brandl <georg@python.org>
parents: 38697
diff changeset
5678 "join() result is too long for a Python string");
33300
77de38534d26 PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents: 32951
diff changeset
5679 Py_DECREF(item);
77de38534d26 PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents: 32951
diff changeset
5680 /* fall through */
77de38534d26 PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents: 32951
diff changeset
5681
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5682 onError:
33300
77de38534d26 PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents: 32951
diff changeset
5683 Py_XDECREF(internal_separator);
33310
8579859f198c PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents: 33304
diff changeset
5684 Py_DECREF(fseq);
17687
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17640
diff changeset
5685 Py_XDECREF(res);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5686 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5687 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5688
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
5689 static
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
5690 PyUnicodeObject *pad(PyUnicodeObject *self,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5691 Py_ssize_t left,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5692 Py_ssize_t right,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5693 Py_UNICODE fill)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5694 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5695 PyUnicodeObject *u;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5696
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5697 if (left < 0)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5698 left = 0;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5699 if (right < 0)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5700 right = 0;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5701
19885
72e6eb653312 More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19849
diff changeset
5702 if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) {
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5703 Py_INCREF(self);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5704 return self;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5705 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5706
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
5707 if (left > PY_SSIZE_T_MAX - self->length ||
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
5708 right > PY_SSIZE_T_MAX - (left + self->length)) {
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
5709 PyErr_SetString(PyExc_OverflowError, "padded string is too long");
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
5710 return NULL;
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48070
diff changeset
5711 }
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5712 u = _PyUnicode_New(left + self->length + right);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5713 if (u) {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5714 if (left)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5715 Py_UNICODE_FILL(u->str, fill, left);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5716 Py_UNICODE_COPY(u->str + left, self->str, self->length);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5717 if (right)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5718 Py_UNICODE_FILL(u->str + left + self->length, fill, right);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5719 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5720
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5721 return u;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5722 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5723
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5724 #define SPLIT_APPEND(data, left, right) \
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5725 str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5726 if (!str) \
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5727 goto onError; \
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5728 if (PyList_Append(list, str)) { \
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5729 Py_DECREF(str); \
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5730 goto onError; \
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5731 } \
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5732 else \
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5733 Py_DECREF(str);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5734
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5735 static
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5736 PyObject *split_whitespace(PyUnicodeObject *self,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5737 PyObject *list,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5738 Py_ssize_t maxcount)
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5739 {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5740 register Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5741 register Py_ssize_t j;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5742 Py_ssize_t len = self->length;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5743 PyObject *str;
45053
26278796d4da Patch #1970 by Antoine Pitrou: Speedup unicode whitespace and linebreak detection. The speedup is about 25% for split() (571 / 457 usec) and 35% (175 / 127 usec )for splitlines()
Christian Heimes <christian@cheimes.de>
parents: 44945
diff changeset
5744 register const Py_UNICODE *buf = self->str;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5745
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5746 for (i = j = 0; i < len; ) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5747 /* find a token */
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
5748 while (i < len && Py_UNICODE_ISSPACE(buf[i]))
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5749 i++;
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
5750 j = i;
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5751 while (i < len && !Py_UNICODE_ISSPACE(buf[i]))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5752 i++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5753 if (j < i) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5754 if (maxcount-- <= 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5755 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5756 SPLIT_APPEND(buf, j, i);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5757 while (i < len && Py_UNICODE_ISSPACE(buf[i]))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5758 i++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5759 j = i;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5760 }
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5761 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5762 if (j < len) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5763 SPLIT_APPEND(buf, j, len);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5764 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5765 return list;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5766
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5767 onError:
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5768 Py_DECREF(list);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5769 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5770 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5771
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5772 PyObject *PyUnicode_Splitlines(PyObject *string,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5773 int keepends)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5774 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5775 register Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5776 register Py_ssize_t j;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5777 Py_ssize_t len;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5778 PyObject *list;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5779 PyObject *str;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5780 Py_UNICODE *data;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5781
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5782 string = PyUnicode_FromObject(string);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5783 if (string == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5784 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5785 data = PyUnicode_AS_UNICODE(string);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5786 len = PyUnicode_GET_SIZE(string);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5787
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5788 list = PyList_New(0);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5789 if (!list)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5790 goto onError;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5791
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5792 for (i = j = 0; i < len; ) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5793 Py_ssize_t eol;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5794
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5795 /* Find a line and append it */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5796 while (i < len && !BLOOM_LINEBREAK(data[i]))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5797 i++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5798
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5799 /* Skip the line break reading CRLF as one line break */
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
5800 eol = i;
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5801 if (i < len) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5802 if (data[i] == '\r' && i + 1 < len &&
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5803 data[i+1] == '\n')
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5804 i += 2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5805 else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5806 i++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5807 if (keepends)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5808 eol = i;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5809 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5810 SPLIT_APPEND(data, j, eol);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5811 j = i;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5812 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5813 if (j < len) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5814 SPLIT_APPEND(data, j, len);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5815 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5816
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5817 Py_DECREF(string);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5818 return list;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5819
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5820 onError:
36877
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36675
diff changeset
5821 Py_XDECREF(list);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5822 Py_DECREF(string);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5823 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5824 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5825
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
5826 static
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5827 PyObject *split_char(PyUnicodeObject *self,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5828 PyObject *list,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5829 Py_UNICODE ch,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5830 Py_ssize_t maxcount)
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5831 {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5832 register Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5833 register Py_ssize_t j;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5834 Py_ssize_t len = self->length;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5835 PyObject *str;
45053
26278796d4da Patch #1970 by Antoine Pitrou: Speedup unicode whitespace and linebreak detection. The speedup is about 25% for split() (571 / 457 usec) and 35% (175 / 127 usec )for splitlines()
Christian Heimes <christian@cheimes.de>
parents: 44945
diff changeset
5836 register const Py_UNICODE *buf = self->str;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5837
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5838 for (i = j = 0; i < len; ) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5839 if (buf[i] == ch) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5840 if (maxcount-- <= 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5841 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5842 SPLIT_APPEND(buf, j, i);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5843 i = j = i + 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5844 } else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5845 i++;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5846 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5847 if (j <= len) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5848 SPLIT_APPEND(buf, j, len);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5849 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5850 return list;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5851
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5852 onError:
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5853 Py_DECREF(list);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5854 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5855 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5856
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
5857 static
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5858 PyObject *split_substring(PyUnicodeObject *self,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5859 PyObject *list,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5860 PyUnicodeObject *substring,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5861 Py_ssize_t maxcount)
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5862 {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5863 register Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5864 register Py_ssize_t j;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5865 Py_ssize_t len = self->length;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5866 Py_ssize_t sublen = substring->length;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5867 PyObject *str;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5868
15609
0906c8e2c961 Fix off-by-one error in split_substring(). Fixes SF bug #122162.
Guido van Rossum <guido@python.org>
parents: 15579
diff changeset
5869 for (i = j = 0; i <= len - sublen; ) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5870 if (Py_UNICODE_MATCH(self, i, substring)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5871 if (maxcount-- <= 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5872 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5873 SPLIT_APPEND(self->str, j, i);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5874 i = j = i + sublen;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5875 } else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5876 i++;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5877 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5878 if (j <= len) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5879 SPLIT_APPEND(self->str, j, len);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5880 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5881 return list;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5882
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5883 onError:
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5884 Py_DECREF(list);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5885 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5886 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5887
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5888 static
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5889 PyObject *rsplit_whitespace(PyUnicodeObject *self,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5890 PyObject *list,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5891 Py_ssize_t maxcount)
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5892 {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5893 register Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5894 register Py_ssize_t j;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5895 Py_ssize_t len = self->length;
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5896 PyObject *str;
45053
26278796d4da Patch #1970 by Antoine Pitrou: Speedup unicode whitespace and linebreak detection. The speedup is about 25% for split() (571 / 457 usec) and 35% (175 / 127 usec )for splitlines()
Christian Heimes <christian@cheimes.de>
parents: 44945
diff changeset
5897 register const Py_UNICODE *buf = self->str;
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5898
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5899 for (i = j = len - 1; i >= 0; ) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5900 /* find a token */
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
5901 while (i >= 0 && Py_UNICODE_ISSPACE(buf[i]))
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5902 i--;
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
5903 j = i;
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5904 while (i >= 0 && !Py_UNICODE_ISSPACE(buf[i]))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5905 i--;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5906 if (j > i) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5907 if (maxcount-- <= 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5908 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5909 SPLIT_APPEND(buf, i + 1, j + 1);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5910 while (i >= 0 && Py_UNICODE_ISSPACE(buf[i]))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5911 i--;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5912 j = i;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5913 }
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5914 }
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5915 if (j >= 0) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5916 SPLIT_APPEND(buf, 0, j + 1);
38257
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
5917 }
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
5918 if (PyList_Reverse(list) < 0)
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
5919 goto onError;
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5920 return list;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5921
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5922 onError:
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5923 Py_DECREF(list);
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5924 return NULL;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5925 }
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5926
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
5927 static
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5928 PyObject *rsplit_char(PyUnicodeObject *self,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5929 PyObject *list,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5930 Py_UNICODE ch,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5931 Py_ssize_t maxcount)
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5932 {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5933 register Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5934 register Py_ssize_t j;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5935 Py_ssize_t len = self->length;
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5936 PyObject *str;
45053
26278796d4da Patch #1970 by Antoine Pitrou: Speedup unicode whitespace and linebreak detection. The speedup is about 25% for split() (571 / 457 usec) and 35% (175 / 127 usec )for splitlines()
Christian Heimes <christian@cheimes.de>
parents: 44945
diff changeset
5937 register const Py_UNICODE *buf = self->str;
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5938
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5939 for (i = j = len - 1; i >= 0; ) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5940 if (buf[i] == ch) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5941 if (maxcount-- <= 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5942 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5943 SPLIT_APPEND(buf, i + 1, j + 1);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5944 j = i = i - 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5945 } else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5946 i--;
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5947 }
31252
892116a886e0 Fix unicode.rsplit()'s bug that ignores separater on the end of string when
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31248
diff changeset
5948 if (j >= -1) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5949 SPLIT_APPEND(buf, 0, j + 1);
38257
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
5950 }
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
5951 if (PyList_Reverse(list) < 0)
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
5952 goto onError;
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5953 return list;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5954
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5955 onError:
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5956 Py_DECREF(list);
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5957 return NULL;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5958 }
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5959
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
5960 static
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5961 PyObject *rsplit_substring(PyUnicodeObject *self,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5962 PyObject *list,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5963 PyUnicodeObject *substring,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5964 Py_ssize_t maxcount)
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5965 {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5966 register Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5967 register Py_ssize_t j;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5968 Py_ssize_t len = self->length;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
5969 Py_ssize_t sublen = substring->length;
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5970 PyObject *str;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5971
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5972 for (i = len - sublen, j = len; i >= 0; ) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5973 if (Py_UNICODE_MATCH(self, i, substring)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5974 if (maxcount-- <= 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5975 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5976 SPLIT_APPEND(self->str, i + sublen, j);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5977 j = i;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5978 i -= sublen;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5979 } else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5980 i--;
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5981 }
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5982 if (j >= 0) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5983 SPLIT_APPEND(self->str, 0, j);
38257
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
5984 }
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
5985 if (PyList_Reverse(list) < 0)
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
5986 goto onError;
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5987 return list;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5988
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5989 onError:
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5990 Py_DECREF(list);
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5991 return NULL;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5992 }
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
5993
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5994 #undef SPLIT_APPEND
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5995
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5996 static
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5997 PyObject *split(PyUnicodeObject *self,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5998 PyUnicodeObject *substring,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
5999 Py_ssize_t maxcount)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6000 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6001 PyObject *list;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6002
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6003 if (maxcount < 0)
37662
0678ce9fb6b2 Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 37661
diff changeset
6004 maxcount = PY_SSIZE_T_MAX;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6005
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6006 list = PyList_New(0);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6007 if (!list)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6008 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6009
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6010 if (substring == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6011 return split_whitespace(self,list,maxcount);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6012
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6013 else if (substring->length == 1)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6014 return split_char(self,list,substring->str[0],maxcount);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6015
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6016 else if (substring->length == 0) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6017 Py_DECREF(list);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6018 PyErr_SetString(PyExc_ValueError, "empty separator");
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6019 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6020 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6021 else
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6022 return split_substring(self,list,substring,maxcount);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6023 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6024
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
6025 static
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
6026 PyObject *rsplit(PyUnicodeObject *self,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6027 PyUnicodeObject *substring,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6028 Py_ssize_t maxcount)
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
6029 {
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
6030 PyObject *list;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
6031
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
6032 if (maxcount < 0)
37662
0678ce9fb6b2 Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 37661
diff changeset
6033 maxcount = PY_SSIZE_T_MAX;
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
6034
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
6035 list = PyList_New(0);
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
6036 if (!list)
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
6037 return NULL;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
6038
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
6039 if (substring == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6040 return rsplit_whitespace(self,list,maxcount);
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
6041
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
6042 else if (substring->length == 1)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6043 return rsplit_char(self,list,substring->str[0],maxcount);
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
6044
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
6045 else if (substring->length == 0) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6046 Py_DECREF(list);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6047 PyErr_SetString(PyExc_ValueError, "empty separator");
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6048 return NULL;
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
6049 }
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
6050 else
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6051 return rsplit_substring(self,list,substring,maxcount);
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
6052 }
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
6053
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
6054 static
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6055 PyObject *replace(PyUnicodeObject *self,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6056 PyUnicodeObject *str1,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6057 PyUnicodeObject *str2,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6058 Py_ssize_t maxcount)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6059 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6060 PyUnicodeObject *u;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6061
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6062 if (maxcount < 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6063 maxcount = PY_SSIZE_T_MAX;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6064
38278
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6065 if (str1->length == str2->length) {
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6066 /* same length */
37662
0678ce9fb6b2 Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 37661
diff changeset
6067 Py_ssize_t i;
38278
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6068 if (str1->length == 1) {
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6069 /* replace characters */
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6070 Py_UNICODE u1, u2;
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6071 if (!findchar(self->str, self->length, str1->str[0]))
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6072 goto nothing;
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6073 u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length);
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6074 if (!u)
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6075 return NULL;
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6076 Py_UNICODE_COPY(u->str, self->str, self->length);
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6077 u1 = str1->str[0];
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6078 u2 = str2->str[0];
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6079 for (i = 0; i < u->length; i++)
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6080 if (u->str[i] == u1) {
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6081 if (--maxcount < 0)
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6082 break;
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6083 u->str[i] = u2;
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6084 }
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6085 } else {
38278
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6086 i = fastsearch(
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6087 self->str, self->length, str1->str, str1->length, FAST_SEARCH
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6088 );
38278
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6089 if (i < 0)
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6090 goto nothing;
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6091 u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length);
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6092 if (!u)
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6093 return NULL;
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6094 Py_UNICODE_COPY(u->str, self->str, self->length);
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6095 while (i <= self->length - str1->length)
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6096 if (Py_UNICODE_MATCH(self, i, str1)) {
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6097 if (--maxcount < 0)
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6098 break;
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6099 Py_UNICODE_COPY(u->str+i, str2->str, str2->length);
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6100 i += str1->length;
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6101 } else
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6102 i++;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6103 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6104 } else {
38278
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6105
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6106 Py_ssize_t n, i, j, e;
38299
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38278
diff changeset
6107 Py_ssize_t product, new_size, delta;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6108 Py_UNICODE *p;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6109
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6110 /* replace strings */
38400
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
6111 n = stringlib_count(self->str, self->length, str1->str, str1->length);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6112 if (n > maxcount)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6113 n = maxcount;
38278
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6114 if (n == 0)
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6115 goto nothing;
38299
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38278
diff changeset
6116 /* new_size = self->length + n * (str2->length - str1->length)); */
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38278
diff changeset
6117 delta = (str2->length - str1->length);
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38278
diff changeset
6118 if (delta == 0) {
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38278
diff changeset
6119 new_size = self->length;
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38278
diff changeset
6120 } else {
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38278
diff changeset
6121 product = n * (str2->length - str1->length);
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38278
diff changeset
6122 if ((product / (str2->length - str1->length)) != n) {
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38278
diff changeset
6123 PyErr_SetString(PyExc_OverflowError,
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38278
diff changeset
6124 "replace string is too long");
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38278
diff changeset
6125 return NULL;
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38278
diff changeset
6126 }
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38278
diff changeset
6127 new_size = self->length + product;
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38278
diff changeset
6128 if (new_size < 0) {
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38278
diff changeset
6129 PyErr_SetString(PyExc_OverflowError,
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38278
diff changeset
6130 "replace string is too long");
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38278
diff changeset
6131 return NULL;
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38278
diff changeset
6132 }
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38278
diff changeset
6133 }
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38278
diff changeset
6134 u = _PyUnicode_New(new_size);
38278
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6135 if (!u)
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6136 return NULL;
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6137 i = 0;
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6138 p = u->str;
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6139 e = self->length - str1->length;
38278
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6140 if (str1->length > 0) {
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6141 while (n-- > 0) {
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6142 /* look for next match */
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6143 j = i;
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6144 while (j <= e) {
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6145 if (Py_UNICODE_MATCH(self, j, str1))
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6146 break;
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6147 j++;
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6148 }
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6149 if (j > i) {
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6150 if (j > e)
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6151 break;
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6152 /* copy unchanged part [i:j] */
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6153 Py_UNICODE_COPY(p, self->str+i, j-i);
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6154 p += j - i;
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6155 }
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6156 /* copy substitution string */
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6157 if (str2->length > 0) {
38278
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6158 Py_UNICODE_COPY(p, str2->str, str2->length);
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6159 p += str2->length;
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6160 }
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6161 i = j + str1->length;
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6162 }
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6163 if (i < self->length)
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6164 /* copy tail [i:] */
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6165 Py_UNICODE_COPY(p, self->str+i, self->length-i);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6166 } else {
38440
5b0753b9d16a needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents: 38427
diff changeset
6167 /* interleave */
38278
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6168 while (n > 0) {
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6169 Py_UNICODE_COPY(p, str2->str, str2->length);
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6170 p += str2->length;
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6171 if (--n <= 0)
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6172 break;
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6173 *p++ = self->str[i++];
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6174 }
38278
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6175 Py_UNICODE_COPY(p, self->str+i, self->length-i);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6176 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6177 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6178 return (PyObject *) u;
38278
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6179
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6180 nothing:
38278
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6181 /* nothing to replace; return original string (when possible) */
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6182 if (PyUnicode_CheckExact(self)) {
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6183 Py_INCREF(self);
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6184 return (PyObject *) self;
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6185 }
260d5a6ce30f needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents: 38276
diff changeset
6186 return PyUnicode_FromUnicode(self->str, self->length);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6187 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6188
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6189 /* --- Unicode Object Methods --------------------------------------------- */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6190
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
6191 PyDoc_STRVAR(title__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6192 "S.title() -> unicode\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6193 \n\
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6194 Return a titlecased version of S, i.e. words start with title case\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
6195 characters, all remaining cased characters have lower case.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6196
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6197 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
6198 unicode_title(PyUnicodeObject *self)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6199 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6200 return fixup(self, fixtitle);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6201 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6202
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
6203 PyDoc_STRVAR(capitalize__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6204 "S.capitalize() -> unicode\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6205 \n\
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6206 Return a capitalized version of S, i.e. make the first character\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
6207 have upper case.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6208
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6209 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
6210 unicode_capitalize(PyUnicodeObject *self)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6211 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6212 return fixup(self, fixcapitalize);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6213 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6214
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6215 #if 0
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
6216 PyDoc_STRVAR(capwords__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6217 "S.capwords() -> unicode\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6218 \n\
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6219 Apply .capitalize() to all words in S and return the result with\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
6220 normalized whitespace (all whitespace strings are replaced by ' ').");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6221
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6222 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
6223 unicode_capwords(PyUnicodeObject *self)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6224 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6225 PyObject *list;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6226 PyObject *item;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
6227 Py_ssize_t i;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6228
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6229 /* Split into words */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6230 list = split(self, NULL, -1);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6231 if (!list)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6232 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6233
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6234 /* Capitalize each word */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6235 for (i = 0; i < PyList_GET_SIZE(list); i++) {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6236 item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i),
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6237 fixcapitalize);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6238 if (item == NULL)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6239 goto onError;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6240 Py_DECREF(PyList_GET_ITEM(list, i));
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6241 PyList_SET_ITEM(list, i, item);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6242 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6243
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6244 /* Join the words to form a new string */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6245 item = PyUnicode_Join(NULL, list);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6246
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6247 onError:
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6248 Py_DECREF(list);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6249 return (PyObject *)item;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6250 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6251 #endif
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6252
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30765
diff changeset
6253 /* Argument converter. Coerces to a single unicode character */
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30765
diff changeset
6254
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30765
diff changeset
6255 static int
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30765
diff changeset
6256 convert_uc(PyObject *obj, void *addr)
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30765
diff changeset
6257 {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
6258 Py_UNICODE *fillcharloc = (Py_UNICODE *)addr;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
6259 PyObject *uniobj;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
6260 Py_UNICODE *unistr;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
6261
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
6262 uniobj = PyUnicode_FromObject(obj);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
6263 if (uniobj == NULL) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
6264 PyErr_SetString(PyExc_TypeError,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6265 "The fill character cannot be converted to Unicode");
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
6266 return 0;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
6267 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
6268 if (PyUnicode_GET_SIZE(uniobj) != 1) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
6269 PyErr_SetString(PyExc_TypeError,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6270 "The fill character must be exactly one character long");
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
6271 Py_DECREF(uniobj);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
6272 return 0;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
6273 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
6274 unistr = PyUnicode_AS_UNICODE(uniobj);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
6275 *fillcharloc = unistr[0];
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
6276 Py_DECREF(uniobj);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
6277 return 1;
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30765
diff changeset
6278 }
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30765
diff changeset
6279
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
6280 PyDoc_STRVAR(center__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6281 "S.center(width[, fillchar]) -> unicode\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6282 \n\
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30765
diff changeset
6283 Return S centered in a Unicode string of length width. Padding is\n\
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30765
diff changeset
6284 done using the specified fill character (default is a space)");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6285
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6286 static PyObject *
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6287 unicode_center(PyUnicodeObject *self, PyObject *args)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6288 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
6289 Py_ssize_t marg, left;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
6290 Py_ssize_t width;
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30765
diff changeset
6291 Py_UNICODE fillchar = ' ';
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30765
diff changeset
6292
36532
0c096be27000 Use correct PyArg_Parse format char for Py_ssize_t in unicode.center().
Thomas Wouters <thomas@python.org>
parents: 36519
diff changeset
6293 if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar))
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6294 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6295
19885
72e6eb653312 More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19849
diff changeset
6296 if (self->length >= width && PyUnicode_CheckExact(self)) {
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6297 Py_INCREF(self);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6298 return (PyObject*) self;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6299 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6300
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6301 marg = width - self->length;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6302 left = marg / 2 + (marg & width & 1);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6303
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30765
diff changeset
6304 return (PyObject*) pad(self, left, marg - left, fillchar);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6305 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6306
13890
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6307 #if 0
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6308
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6309 /* This code should go into some future Unicode collation support
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6310 module. The basic comparison should compare ordinals on a naive
54600
8888a6fe9dd7 #6224: s/JPython/Jython/, and remove one link to a module nine years old.
Georg Brandl <georg@python.org>
parents: 53780
diff changeset
6311 basis (this is what Java does and thus Jython too). */
13890
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6312
13337
e101600adee4 Bill Tutt:
Marc-André Lemburg <mal@egenix.com>
parents: 13209
diff changeset
6313 /* speedy UTF-16 code point order comparison */
e101600adee4 Bill Tutt:
Marc-André Lemburg <mal@egenix.com>
parents: 13209
diff changeset
6314 /* gleaned from: */
e101600adee4 Bill Tutt:
Marc-André Lemburg <mal@egenix.com>
parents: 13209
diff changeset
6315 /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */
e101600adee4 Bill Tutt:
Marc-André Lemburg <mal@egenix.com>
parents: 13209
diff changeset
6316
13411
9a8391054840 New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13401
diff changeset
6317 static short utf16Fixup[32] =
13337
e101600adee4 Bill Tutt:
Marc-André Lemburg <mal@egenix.com>
parents: 13209
diff changeset
6318 {
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
6319 0, 0, 0, 0, 0, 0, 0, 0,
13337
e101600adee4 Bill Tutt:
Marc-André Lemburg <mal@egenix.com>
parents: 13209
diff changeset
6320 0, 0, 0, 0, 0, 0, 0, 0,
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
6321 0, 0, 0, 0, 0, 0, 0, 0,
13411
9a8391054840 New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents: 13401
diff changeset
6322 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800
13337
e101600adee4 Bill Tutt:
Marc-André Lemburg <mal@egenix.com>
parents: 13209
diff changeset
6323 };
e101600adee4 Bill Tutt:
Marc-André Lemburg <mal@egenix.com>
parents: 13209
diff changeset
6324
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6325 static int
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6326 unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6327 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
6328 Py_ssize_t len1, len2;
13337
e101600adee4 Bill Tutt:
Marc-André Lemburg <mal@egenix.com>
parents: 13209
diff changeset
6329
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6330 Py_UNICODE *s1 = str1->str;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6331 Py_UNICODE *s2 = str2->str;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6332
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6333 len1 = str1->length;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6334 len2 = str2->length;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
6335
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6336 while (len1 > 0 && len2 > 0) {
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
6337 Py_UNICODE c1, c2;
13337
e101600adee4 Bill Tutt:
Marc-André Lemburg <mal@egenix.com>
parents: 13209
diff changeset
6338
e101600adee4 Bill Tutt:
Marc-André Lemburg <mal@egenix.com>
parents: 13209
diff changeset
6339 c1 = *s1++;
e101600adee4 Bill Tutt:
Marc-André Lemburg <mal@egenix.com>
parents: 13209
diff changeset
6340 c2 = *s2++;
18279
8b7ff44b4669 experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents: 18278
diff changeset
6341
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6342 if (c1 > (1<<11) * 26)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6343 c1 += utf16Fixup[c1>>11];
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6344 if (c2 > (1<<11) * 26)
13337
e101600adee4 Bill Tutt:
Marc-André Lemburg <mal@egenix.com>
parents: 13209
diff changeset
6345 c2 += utf16Fixup[c2>>11];
18279
8b7ff44b4669 experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents: 18278
diff changeset
6346 /* now c1 and c2 are in UTF-32-compatible order */
8b7ff44b4669 experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents: 18278
diff changeset
6347
8b7ff44b4669 experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents: 18278
diff changeset
6348 if (c1 != c2)
8b7ff44b4669 experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents: 18278
diff changeset
6349 return (c1 < c2) ? -1 : 1;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
6350
13337
e101600adee4 Bill Tutt:
Marc-André Lemburg <mal@egenix.com>
parents: 13209
diff changeset
6351 len1--; len2--;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6352 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6353
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6354 return (len1 < len2) ? -1 : (len1 != len2);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6355 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6356
13890
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6357 #else
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6358
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6359 static int
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6360 unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2)
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6361 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
6362 register Py_ssize_t len1, len2;
13890
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6363
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6364 Py_UNICODE *s1 = str1->str;
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6365 Py_UNICODE *s2 = str2->str;
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6366
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6367 len1 = str1->length;
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6368 len2 = str2->length;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
6369
13890
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6370 while (len1 > 0 && len2 > 0) {
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
6371 Py_UNICODE c1, c2;
18279
8b7ff44b4669 experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents: 18278
diff changeset
6372
8b7ff44b4669 experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents: 18278
diff changeset
6373 c1 = *s1++;
8b7ff44b4669 experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents: 18278
diff changeset
6374 c2 = *s2++;
8b7ff44b4669 experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents: 18278
diff changeset
6375
8b7ff44b4669 experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents: 18278
diff changeset
6376 if (c1 != c2)
8b7ff44b4669 experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents: 18278
diff changeset
6377 return (c1 < c2) ? -1 : 1;
8b7ff44b4669 experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents: 18278
diff changeset
6378
13890
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6379 len1--; len2--;
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6380 }
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6381
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6382 return (len1 < len2) ? -1 : (len1 != len2);
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6383 }
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6384
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6385 #endif
8f0fc0b5f784 Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents: 13837
diff changeset
6386
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6387 int PyUnicode_Compare(PyObject *left,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6388 PyObject *right)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6389 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6390 PyUnicodeObject *u = NULL, *v = NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6391 int result;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6392
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6393 /* Coerce the two arguments */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6394 u = (PyUnicodeObject *)PyUnicode_FromObject(left);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6395 if (u == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6396 goto onError;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6397 v = (PyUnicodeObject *)PyUnicode_FromObject(right);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6398 if (v == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6399 goto onError;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6400
13620
5b727ab37092 Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
6401 /* Shortcut for empty or interned objects */
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6402 if (v == u) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6403 Py_DECREF(u);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6404 Py_DECREF(v);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6405 return 0;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6406 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6407
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6408 result = unicode_compare(u, v);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6409
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6410 Py_DECREF(u);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6411 Py_DECREF(v);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6412 return result;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6413
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6414 onError:
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6415 Py_XDECREF(u);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6416 Py_XDECREF(v);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6417 return -1;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6418 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6419
39487
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6420 PyObject *PyUnicode_RichCompare(PyObject *left,
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6421 PyObject *right,
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6422 int op)
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6423 {
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6424 int result;
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6425
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6426 result = PyUnicode_Compare(left, right);
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6427 if (result == -1 && PyErr_Occurred())
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6428 goto onError;
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6429
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6430 /* Convert the return value to a Boolean */
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6431 switch (op) {
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6432 case Py_EQ:
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6433 result = (result == 0);
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6434 break;
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6435 case Py_NE:
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6436 result = (result != 0);
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6437 break;
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6438 case Py_LE:
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6439 result = (result <= 0);
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6440 break;
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6441 case Py_GE:
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6442 result = (result >= 0);
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6443 break;
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6444 case Py_LT:
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6445 result = (result == -1);
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6446 break;
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6447 case Py_GT:
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6448 result = (result == 1);
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6449 break;
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6450 }
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6451 return PyBool_FromLong(result);
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6452
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6453 onError:
39487
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6454
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6455 /* Standard case
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6456
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6457 Type errors mean that PyUnicode_FromObject() could not convert
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6458 one of the arguments (usually the right hand side) to Unicode,
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6459 ie. we can't handle the comparison request. However, it is
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6460 possible that the other object knows a comparison method, which
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6461 is why we return Py_NotImplemented to give the other object a
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6462 chance.
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6463
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6464 */
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6465 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6466 PyErr_Clear();
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6467 Py_INCREF(Py_NotImplemented);
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6468 return Py_NotImplemented;
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6469 }
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6470 if (op != Py_EQ && op != Py_NE)
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6471 return NULL;
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6472
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6473 /* Equality comparison.
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6474
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6475 This is a special case: we silence any PyExc_UnicodeDecodeError
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6476 and instead turn it into a PyErr_UnicodeWarning.
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6477
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6478 */
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6479 if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError))
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6480 return NULL;
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6481 PyErr_Clear();
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
6482 if (PyErr_Warn(PyExc_UnicodeWarning,
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
6483 (op == Py_EQ) ?
39487
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6484 "Unicode equal comparison "
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6485 "failed to convert both arguments to Unicode - "
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6486 "interpreting them as being unequal" :
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6487 "Unicode unequal comparison "
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6488 "failed to convert both arguments to Unicode - "
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6489 "interpreting them as being unequal"
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6490 ) < 0)
39487
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6491 return NULL;
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6492 result = (op == Py_NE);
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6493 return PyBool_FromLong(result);
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6494 }
635b09b1f542 Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents: 39463
diff changeset
6495
11997
38310eb0659e Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents: 11968
diff changeset
6496 int PyUnicode_Contains(PyObject *container,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6497 PyObject *element)
11997
38310eb0659e Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents: 11968
diff changeset
6498 {
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
6499 PyObject *str, *sub;
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
6500 int result;
11997
38310eb0659e Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents: 11968
diff changeset
6501
38310eb0659e Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents: 11968
diff changeset
6502 /* Coerce the two arguments */
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
6503 sub = PyUnicode_FromObject(element);
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
6504 if (!sub) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6505 PyErr_SetString(PyExc_TypeError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6506 "'in <string>' requires string as left operand");
38243
0ba711a496e6 needforspeed: fixed unicode "in" operator to use same implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38241
diff changeset
6507 return -1;
0ba711a496e6 needforspeed: fixed unicode "in" operator to use same implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38241
diff changeset
6508 }
0ba711a496e6 needforspeed: fixed unicode "in" operator to use same implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38241
diff changeset
6509
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
6510 str = PyUnicode_FromObject(container);
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
6511 if (!str) {
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
6512 Py_DECREF(sub);
38243
0ba711a496e6 needforspeed: fixed unicode "in" operator to use same implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38241
diff changeset
6513 return -1;
0ba711a496e6 needforspeed: fixed unicode "in" operator to use same implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38241
diff changeset
6514 }
11997
38310eb0659e Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents: 11968
diff changeset
6515
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
6516 result = stringlib_contains_obj(str, sub);
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
6517
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
6518 Py_DECREF(str);
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
6519 Py_DECREF(sub);
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
6520
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
6521 return result;
11997
38310eb0659e Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents: 11968
diff changeset
6522 }
38310eb0659e Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents: 11968
diff changeset
6523
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6524 /* Concat to string or Unicode object giving a new Unicode object. */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6525
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6526 PyObject *PyUnicode_Concat(PyObject *left,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6527 PyObject *right)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6528 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6529 PyUnicodeObject *u = NULL, *v = NULL, *w;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6530
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6531 /* Coerce the two arguments */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6532 u = (PyUnicodeObject *)PyUnicode_FromObject(left);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6533 if (u == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6534 goto onError;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6535 v = (PyUnicodeObject *)PyUnicode_FromObject(right);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6536 if (v == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6537 goto onError;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6538
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6539 /* Shortcuts */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6540 if (v == unicode_empty) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6541 Py_DECREF(v);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6542 return (PyObject *)u;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6543 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6544 if (u == unicode_empty) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6545 Py_DECREF(u);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6546 return (PyObject *)v;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6547 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6548
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6549 /* Concat the two Unicode strings */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6550 w = _PyUnicode_New(u->length + v->length);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6551 if (w == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6552 goto onError;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6553 Py_UNICODE_COPY(w->str, u->str, u->length);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6554 Py_UNICODE_COPY(w->str + u->length, v->str, v->length);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6555
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6556 Py_DECREF(u);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6557 Py_DECREF(v);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6558 return (PyObject *)w;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6559
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6560 onError:
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6561 Py_XDECREF(u);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6562 Py_XDECREF(v);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6563 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6564 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6565
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
6566 PyDoc_STRVAR(count__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6567 "S.count(sub[, start[, end]]) -> int\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6568 \n\
38236
7e16fcce34a1 docstring tweaks: count counts non-overlapping substrings, not
Fredrik Lundh <fredrik@pythonware.com>
parents: 38182
diff changeset
6569 Return the number of non-overlapping occurrences of substring sub in\n\
7e16fcce34a1 docstring tweaks: count counts non-overlapping substrings, not
Fredrik Lundh <fredrik@pythonware.com>
parents: 38182
diff changeset
6570 Unicode string S[start:end]. Optional arguments start and end are\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
6571 interpreted as in slice notation.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6572
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6573 static PyObject *
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6574 unicode_count(PyUnicodeObject *self, PyObject *args)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6575 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6576 PyUnicodeObject *substring;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
6577 Py_ssize_t start = 0;
37662
0678ce9fb6b2 Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 37661
diff changeset
6578 Py_ssize_t end = PY_SSIZE_T_MAX;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6579 PyObject *result;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6580
12629
3efeaa7521e4 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12582
diff changeset
6581 if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6582 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6583 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6584
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6585 substring = (PyUnicodeObject *)PyUnicode_FromObject(
38400
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
6586 (PyObject *)substring);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6587 if (substring == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6588 return NULL;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
6589
38402
6b03a47eb54d needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents: 38401
diff changeset
6590 FIX_START_END(self);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6591
38400
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
6592 result = PyInt_FromSsize_t(
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
6593 stringlib_count(self->str + start, end - start,
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
6594 substring->str, substring->length)
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
6595 );
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6596
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6597 Py_DECREF(substring);
38400
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
6598
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6599 return result;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6600 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6601
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
6602 PyDoc_STRVAR(encode__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6603 "S.encode([encoding[,errors]]) -> string or unicode\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6604 \n\
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6605 Encodes S using the codec registered for encoding. encoding defaults\n\
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6606 to the default encoding. errors may be given to set a different error\n\
12645
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
6607 handling scheme. Default is 'strict' meaning that encoding errors raise\n\
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
6608 a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
6609 'xmlcharrefreplace' as well as any other name registered with\n\
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25005
diff changeset
6610 codecs.register_error that can handle UnicodeEncodeErrors.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6611
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6612 static PyObject *
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6613 unicode_encode(PyUnicodeObject *self, PyObject *args)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6614 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6615 char *encoding = NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6616 char *errors = NULL;
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6617 PyObject *v;
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
6618
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6619 if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors))
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6620 return NULL;
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6621 v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors);
32552
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
6622 if (v == NULL)
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
6623 goto onError;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
6624 if (!PyString_Check(v) && !PyUnicode_Check(v)) {
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6625 PyErr_Format(PyExc_TypeError,
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6626 "encoder did not return a string/unicode object "
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6627 "(type=%.400s)",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43932
diff changeset
6628 Py_TYPE(v)->tp_name);
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6629 Py_DECREF(v);
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6630 return NULL;
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6631 }
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6632 return v;
32552
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
6633
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6634 onError:
32552
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
6635 return NULL;
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6636 }
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6637
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6638 PyDoc_STRVAR(decode__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6639 "S.decode([encoding[,errors]]) -> string or unicode\n\
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6640 \n\
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6641 Decodes S using the codec registered for encoding. encoding defaults\n\
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6642 to the default encoding. errors may be given to set a different error\n\
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6643 handling scheme. Default is 'strict' meaning that encoding errors raise\n\
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6644 a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6645 as well as any other name registerd with codecs.register_error that is\n\
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6646 able to handle UnicodeDecodeErrors.");
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6647
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6648 static PyObject *
32571
bec0c7618625 Fix a copy&paste typo.
Marc-André Lemburg <mal@egenix.com>
parents: 32552
diff changeset
6649 unicode_decode(PyUnicodeObject *self, PyObject *args)
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6650 {
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6651 char *encoding = NULL;
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6652 char *errors = NULL;
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6653 PyObject *v;
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
6654
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6655 if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors))
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6656 return NULL;
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6657 v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors);
32552
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
6658 if (v == NULL)
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
6659 goto onError;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
6660 if (!PyString_Check(v) && !PyUnicode_Check(v)) {
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6661 PyErr_Format(PyExc_TypeError,
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6662 "decoder did not return a string/unicode object "
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6663 "(type=%.400s)",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43932
diff changeset
6664 Py_TYPE(v)->tp_name);
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6665 Py_DECREF(v);
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6666 return NULL;
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6667 }
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
6668 return v;
32552
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
6669
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6670 onError:
32552
3706a3704aea .encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents: 32551
diff changeset
6671 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6672 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6673
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
6674 PyDoc_STRVAR(expandtabs__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6675 "S.expandtabs([tabsize]) -> unicode\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6676 \n\
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6677 Return a copy of S where all tab characters are expanded using spaces.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
6678 If tabsize is not given, a tab size of 8 characters is assumed.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6679
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6680 static PyObject*
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6681 unicode_expandtabs(PyUnicodeObject *self, PyObject *args)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6682 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6683 Py_UNICODE *e;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6684 Py_UNICODE *p;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6685 Py_UNICODE *q;
45731
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
6686 Py_UNICODE *qe;
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
6687 Py_ssize_t i, j, incr;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6688 PyUnicodeObject *u;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6689 int tabsize = 8;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6690
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6691 if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize))
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6692 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6693
13620
5b727ab37092 Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
6694 /* First pass: determine size of output string */
45731
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
6695 i = 0; /* chars up to and including most recent \n or \r */
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
6696 j = 0; /* chars since most recent \n or \r (use in tab calculations) */
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
6697 e = self->str + self->length; /* end of input */
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6698 for (p = self->str; p < e; p++)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6699 if (*p == '\t') {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6700 if (tabsize > 0) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6701 incr = tabsize - (j % tabsize); /* cannot overflow */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6702 if (j > PY_SSIZE_T_MAX - incr)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6703 goto overflow1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6704 j += incr;
45731
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
6705 }
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6706 }
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6707 else {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6708 if (j > PY_SSIZE_T_MAX - 1)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6709 goto overflow1;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6710 j++;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6711 if (*p == '\n' || *p == '\r') {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6712 if (i > PY_SSIZE_T_MAX - j)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6713 goto overflow1;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6714 i += j;
45731
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
6715 j = 0;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6716 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6717 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6718
45731
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
6719 if (i > PY_SSIZE_T_MAX - j)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6720 goto overflow1;
42004
e62b606eff0b Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents: 40908
diff changeset
6721
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6722 /* Second pass: create output string and fill it */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6723 u = _PyUnicode_New(i + j);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6724 if (!u)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6725 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6726
45731
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
6727 j = 0; /* same as in first pass */
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
6728 q = u->str; /* next output char */
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
6729 qe = u->str + u->length; /* end of output */
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6730
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6731 for (p = self->str; p < e; p++)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6732 if (*p == '\t') {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6733 if (tabsize > 0) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6734 i = tabsize - (j % tabsize);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6735 j += i;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6736 while (i--) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6737 if (q >= qe)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6738 goto overflow2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6739 *q++ = ' ';
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6740 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6741 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6742 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6743 else {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
6744 if (q >= qe)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6745 goto overflow2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6746 *q++ = *p;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6747 j++;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6748 if (*p == '\n' || *p == '\r')
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6749 j = 0;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6750 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6751
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6752 return (PyObject*) u;
45731
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
6753
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
6754 overflow2:
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
6755 Py_DECREF(u);
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
6756 overflow1:
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
6757 PyErr_SetString(PyExc_OverflowError, "new string is too long");
162c6952ea34 Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents: 45500
diff changeset
6758 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6759 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6760
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
6761 PyDoc_STRVAR(find__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6762 "S.find(sub [,start [,end]]) -> int\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6763 \n\
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6764 Return the lowest index in S where substring sub is found,\n\
42402
251033a1c2e3 Bug #1763149: use proper slice syntax in docstring.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
6765 such that sub is contained within s[start:end]. Optional\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6766 arguments start and end are interpreted as in slice notation.\n\
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6767 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
6768 Return -1 on failure.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6769
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6770 static PyObject *
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6771 unicode_find(PyUnicodeObject *self, PyObject *args)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6772 {
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
6773 PyObject *substring;
43886
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 43710
diff changeset
6774 Py_ssize_t start;
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 43710
diff changeset
6775 Py_ssize_t end;
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
6776 Py_ssize_t result;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6777
43886
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 43710
diff changeset
6778 if (!_ParseTupleFinds(args, &substring, &start, &end))
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6779 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6780
38441
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
6781 result = stringlib_find_slice(
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
6782 PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self),
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
6783 PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring),
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
6784 start, end
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
6785 );
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6786
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6787 Py_DECREF(substring);
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
6788
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
6789 return PyInt_FromSsize_t(result);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6790 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6791
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6792 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
6793 unicode_getitem(PyUnicodeObject *self, Py_ssize_t index)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6794 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6795 if (index < 0 || index >= self->length) {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6796 PyErr_SetString(PyExc_IndexError, "string index out of range");
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6797 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6798 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6799
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6800 return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6801 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6802
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6803 static long
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6804 unicode_hash(PyUnicodeObject *self)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6805 {
13522
acc4638f37d5 - changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents: 13411
diff changeset
6806 /* Since Unicode objects compare equal to their ASCII string
acc4638f37d5 - changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents: 13411
diff changeset
6807 counterparts, they should use the individual character values
acc4638f37d5 - changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents: 13411
diff changeset
6808 as basis for their hash value. This is needed to assure that
acc4638f37d5 - changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents: 13411
diff changeset
6809 strings and Unicode objects behave in the same way as
acc4638f37d5 - changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents: 13411
diff changeset
6810 dictionary keys. */
acc4638f37d5 - changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents: 13411
diff changeset
6811
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
6812 register Py_ssize_t len;
13522
acc4638f37d5 - changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents: 13411
diff changeset
6813 register Py_UNICODE *p;
acc4638f37d5 - changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents: 13411
diff changeset
6814 register long x;
acc4638f37d5 - changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents: 13411
diff changeset
6815
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6816 if (self->hash != -1)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6817 return self->hash;
13522
acc4638f37d5 - changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents: 13411
diff changeset
6818 len = PyUnicode_GET_SIZE(self);
acc4638f37d5 - changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents: 13411
diff changeset
6819 p = PyUnicode_AS_UNICODE(self);
acc4638f37d5 - changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents: 13411
diff changeset
6820 x = *p << 7;
acc4638f37d5 - changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents: 13411
diff changeset
6821 while (--len >= 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6822 x = (1000003*x) ^ *p++;
13522
acc4638f37d5 - changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents: 13411
diff changeset
6823 x ^= PyUnicode_GET_SIZE(self);
acc4638f37d5 - changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents: 13411
diff changeset
6824 if (x == -1)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6825 x = -2;
13522
acc4638f37d5 - changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents: 13411
diff changeset
6826 self->hash = x;
acc4638f37d5 - changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents: 13411
diff changeset
6827 return x;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6828 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6829
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
6830 PyDoc_STRVAR(index__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6831 "S.index(sub [,start [,end]]) -> int\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6832 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
6833 Like S.find() but raise ValueError when the substring is not found.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6834
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6835 static PyObject *
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6836 unicode_index(PyUnicodeObject *self, PyObject *args)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6837 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
6838 Py_ssize_t result;
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
6839 PyObject *substring;
43886
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 43710
diff changeset
6840 Py_ssize_t start;
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 43710
diff changeset
6841 Py_ssize_t end;
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 43710
diff changeset
6842
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 43710
diff changeset
6843 if (!_ParseTupleFinds(args, &substring, &start, &end))
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6844 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6845
38441
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
6846 result = stringlib_find_slice(
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
6847 PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self),
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
6848 PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring),
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
6849 start, end
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
6850 );
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6851
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6852 Py_DECREF(substring);
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
6853
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6854 if (result < 0) {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6855 PyErr_SetString(PyExc_ValueError, "substring not found");
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6856 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6857 }
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
6858
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
6859 return PyInt_FromSsize_t(result);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6860 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6861
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
6862 PyDoc_STRVAR(islower__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6863 "S.islower() -> bool\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6864 \n\
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22521
diff changeset
6865 Return True if all cased characters in S are lowercase and there is\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
6866 at least one cased character in S, False otherwise.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6867
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6868 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
6869 unicode_islower(PyUnicodeObject *self)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6870 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6871 register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6872 register const Py_UNICODE *e;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6873 int cased;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6874
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6875 /* Shortcut for single character strings */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6876 if (PyUnicode_GET_SIZE(self) == 1)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6877 return PyBool_FromLong(Py_UNICODE_ISLOWER(*p));
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6878
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12909
diff changeset
6879 /* Special case for empty strings */
36203
44cfca6136c6 Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents: 36056
diff changeset
6880 if (PyUnicode_GET_SIZE(self) == 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6881 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12909
diff changeset
6882
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6883 e = p + PyUnicode_GET_SIZE(self);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6884 cased = 0;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6885 for (; p < e; p++) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6886 register const Py_UNICODE ch = *p;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6887
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6888 if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6889 return PyBool_FromLong(0);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6890 else if (!cased && Py_UNICODE_ISLOWER(ch))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6891 cased = 1;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6892 }
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22521
diff changeset
6893 return PyBool_FromLong(cased);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6894 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6895
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
6896 PyDoc_STRVAR(isupper__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6897 "S.isupper() -> bool\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6898 \n\
30707
9e21e56bf5f3 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30653
diff changeset
6899 Return True if all cased characters in S are uppercase and there is\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
6900 at least one cased character in S, False otherwise.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6901
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6902 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
6903 unicode_isupper(PyUnicodeObject *self)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6904 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6905 register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6906 register const Py_UNICODE *e;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6907 int cased;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6908
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6909 /* Shortcut for single character strings */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6910 if (PyUnicode_GET_SIZE(self) == 1)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6911 return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6912
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12909
diff changeset
6913 /* Special case for empty strings */
36203
44cfca6136c6 Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents: 36056
diff changeset
6914 if (PyUnicode_GET_SIZE(self) == 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6915 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12909
diff changeset
6916
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6917 e = p + PyUnicode_GET_SIZE(self);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6918 cased = 0;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6919 for (; p < e; p++) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6920 register const Py_UNICODE ch = *p;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6921
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6922 if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6923 return PyBool_FromLong(0);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6924 else if (!cased && Py_UNICODE_ISUPPER(ch))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6925 cased = 1;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6926 }
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22521
diff changeset
6927 return PyBool_FromLong(cased);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6928 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6929
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
6930 PyDoc_STRVAR(istitle__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6931 "S.istitle() -> bool\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6932 \n\
30707
9e21e56bf5f3 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30653
diff changeset
6933 Return True if S is a titlecased string and there is at least one\n\
9e21e56bf5f3 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30653
diff changeset
6934 character in S, i.e. upper- and titlecase characters may only\n\
9e21e56bf5f3 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30653
diff changeset
6935 follow uncased characters and lowercase characters only cased ones.\n\
9e21e56bf5f3 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30653
diff changeset
6936 Return False otherwise.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6937
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6938 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
6939 unicode_istitle(PyUnicodeObject *self)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6940 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6941 register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6942 register const Py_UNICODE *e;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6943 int cased, previous_is_cased;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6944
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6945 /* Shortcut for single character strings */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6946 if (PyUnicode_GET_SIZE(self) == 1)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6947 return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) ||
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6948 (Py_UNICODE_ISUPPER(*p) != 0));
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6949
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12909
diff changeset
6950 /* Special case for empty strings */
36203
44cfca6136c6 Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents: 36056
diff changeset
6951 if (PyUnicode_GET_SIZE(self) == 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6952 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12909
diff changeset
6953
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6954 e = p + PyUnicode_GET_SIZE(self);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6955 cased = 0;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6956 previous_is_cased = 0;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6957 for (; p < e; p++) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6958 register const Py_UNICODE ch = *p;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6959
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6960 if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6961 if (previous_is_cased)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6962 return PyBool_FromLong(0);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6963 previous_is_cased = 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6964 cased = 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6965 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6966 else if (Py_UNICODE_ISLOWER(ch)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6967 if (!previous_is_cased)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6968 return PyBool_FromLong(0);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6969 previous_is_cased = 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6970 cased = 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6971 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6972 else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6973 previous_is_cased = 0;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6974 }
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22521
diff changeset
6975 return PyBool_FromLong(cased);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6976 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6977
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
6978 PyDoc_STRVAR(isspace__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6979 "S.isspace() -> bool\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6980 \n\
30707
9e21e56bf5f3 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30653
diff changeset
6981 Return True if all characters in S are whitespace\n\
9e21e56bf5f3 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30653
diff changeset
6982 and there is at least one character in S, False otherwise.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6983
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6984 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
6985 unicode_isspace(PyUnicodeObject *self)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6986 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6987 register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6988 register const Py_UNICODE *e;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6989
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6990 /* Shortcut for single character strings */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6991 if (PyUnicode_GET_SIZE(self) == 1 &&
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6992 Py_UNICODE_ISSPACE(*p))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6993 return PyBool_FromLong(1);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6994
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12909
diff changeset
6995 /* Special case for empty strings */
36203
44cfca6136c6 Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents: 36056
diff changeset
6996 if (PyUnicode_GET_SIZE(self) == 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
6997 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12909
diff changeset
6998
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6999 e = p + PyUnicode_GET_SIZE(self);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7000 for (; p < e; p++) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7001 if (!Py_UNICODE_ISSPACE(*p))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7002 return PyBool_FromLong(0);
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22521
diff changeset
7003 }
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22521
diff changeset
7004 return PyBool_FromLong(1);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7005 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7006
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7007 PyDoc_STRVAR(isalpha__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7008 "S.isalpha() -> bool\n\
13356
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7009 \n\
30707
9e21e56bf5f3 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30653
diff changeset
7010 Return True if all characters in S are alphabetic\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7011 and there is at least one character in S, False otherwise.");
13356
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7012
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7013 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
7014 unicode_isalpha(PyUnicodeObject *self)
13356
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7015 {
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7016 register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7017 register const Py_UNICODE *e;
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7018
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7019 /* Shortcut for single character strings */
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7020 if (PyUnicode_GET_SIZE(self) == 1 &&
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7021 Py_UNICODE_ISALPHA(*p))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7022 return PyBool_FromLong(1);
13356
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7023
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7024 /* Special case for empty strings */
36203
44cfca6136c6 Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents: 36056
diff changeset
7025 if (PyUnicode_GET_SIZE(self) == 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7026 return PyBool_FromLong(0);
13356
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7027
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7028 e = p + PyUnicode_GET_SIZE(self);
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7029 for (; p < e; p++) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7030 if (!Py_UNICODE_ISALPHA(*p))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7031 return PyBool_FromLong(0);
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22521
diff changeset
7032 }
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22521
diff changeset
7033 return PyBool_FromLong(1);
13356
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7034 }
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7035
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7036 PyDoc_STRVAR(isalnum__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7037 "S.isalnum() -> bool\n\
13356
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7038 \n\
30707
9e21e56bf5f3 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30653
diff changeset
7039 Return True if all characters in S are alphanumeric\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7040 and there is at least one character in S, False otherwise.");
13356
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7041
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7042 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
7043 unicode_isalnum(PyUnicodeObject *self)
13356
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7044 {
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7045 register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7046 register const Py_UNICODE *e;
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7047
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7048 /* Shortcut for single character strings */
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7049 if (PyUnicode_GET_SIZE(self) == 1 &&
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7050 Py_UNICODE_ISALNUM(*p))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7051 return PyBool_FromLong(1);
13356
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7052
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7053 /* Special case for empty strings */
36203
44cfca6136c6 Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents: 36056
diff changeset
7054 if (PyUnicode_GET_SIZE(self) == 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7055 return PyBool_FromLong(0);
13356
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7056
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7057 e = p + PyUnicode_GET_SIZE(self);
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7058 for (; p < e; p++) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7059 if (!Py_UNICODE_ISALNUM(*p))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7060 return PyBool_FromLong(0);
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22521
diff changeset
7061 }
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22521
diff changeset
7062 return PyBool_FromLong(1);
13356
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7063 }
9a1b961e0c3a Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents: 13337
diff changeset
7064
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7065 PyDoc_STRVAR(isdecimal__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7066 "S.isdecimal() -> bool\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7067 \n\
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22521
diff changeset
7068 Return True if there are only decimal characters in S,\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7069 False otherwise.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7070
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7071 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
7072 unicode_isdecimal(PyUnicodeObject *self)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7073 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7074 register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7075 register const Py_UNICODE *e;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7076
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7077 /* Shortcut for single character strings */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7078 if (PyUnicode_GET_SIZE(self) == 1 &&
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7079 Py_UNICODE_ISDECIMAL(*p))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7080 return PyBool_FromLong(1);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7081
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12909
diff changeset
7082 /* Special case for empty strings */
36203
44cfca6136c6 Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents: 36056
diff changeset
7083 if (PyUnicode_GET_SIZE(self) == 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7084 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12909
diff changeset
7085
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7086 e = p + PyUnicode_GET_SIZE(self);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7087 for (; p < e; p++) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7088 if (!Py_UNICODE_ISDECIMAL(*p))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7089 return PyBool_FromLong(0);
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22521
diff changeset
7090 }
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22521
diff changeset
7091 return PyBool_FromLong(1);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7092 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7093
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7094 PyDoc_STRVAR(isdigit__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7095 "S.isdigit() -> bool\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7096 \n\
30707
9e21e56bf5f3 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30653
diff changeset
7097 Return True if all characters in S are digits\n\
9e21e56bf5f3 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30653
diff changeset
7098 and there is at least one character in S, False otherwise.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7099
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7100 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
7101 unicode_isdigit(PyUnicodeObject *self)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7102 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7103 register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7104 register const Py_UNICODE *e;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7105
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7106 /* Shortcut for single character strings */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7107 if (PyUnicode_GET_SIZE(self) == 1 &&
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7108 Py_UNICODE_ISDIGIT(*p))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7109 return PyBool_FromLong(1);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7110
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12909
diff changeset
7111 /* Special case for empty strings */
36203
44cfca6136c6 Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents: 36056
diff changeset
7112 if (PyUnicode_GET_SIZE(self) == 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7113 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12909
diff changeset
7114
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7115 e = p + PyUnicode_GET_SIZE(self);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7116 for (; p < e; p++) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7117 if (!Py_UNICODE_ISDIGIT(*p))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7118 return PyBool_FromLong(0);
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22521
diff changeset
7119 }
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22521
diff changeset
7120 return PyBool_FromLong(1);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7121 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7122
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7123 PyDoc_STRVAR(isnumeric__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7124 "S.isnumeric() -> bool\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7125 \n\
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22521
diff changeset
7126 Return True if there are only numeric characters in S,\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7127 False otherwise.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7128
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7129 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
7130 unicode_isnumeric(PyUnicodeObject *self)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7131 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7132 register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7133 register const Py_UNICODE *e;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7134
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7135 /* Shortcut for single character strings */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7136 if (PyUnicode_GET_SIZE(self) == 1 &&
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7137 Py_UNICODE_ISNUMERIC(*p))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7138 return PyBool_FromLong(1);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7139
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12909
diff changeset
7140 /* Special case for empty strings */
36203
44cfca6136c6 Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents: 36056
diff changeset
7141 if (PyUnicode_GET_SIZE(self) == 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7142 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12909
diff changeset
7143
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7144 e = p + PyUnicode_GET_SIZE(self);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7145 for (; p < e; p++) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7146 if (!Py_UNICODE_ISNUMERIC(*p))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7147 return PyBool_FromLong(0);
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22521
diff changeset
7148 }
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22521
diff changeset
7149 return PyBool_FromLong(1);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7150 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7151
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7152 PyDoc_STRVAR(join__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7153 "S.join(sequence) -> unicode\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7154 \n\
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7155 Return a string which is the concatenation of the strings in the\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7156 sequence. The separator between elements is S.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7157
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7158 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
7159 unicode_join(PyObject *self, PyObject *data)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7160 {
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
7161 return PyUnicode_Join(self, data);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7162 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7163
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
7164 static Py_ssize_t
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7165 unicode_length(PyUnicodeObject *self)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7166 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7167 return self->length;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7168 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7169
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7170 PyDoc_STRVAR(ljust__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7171 "S.ljust(width[, fillchar]) -> int\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7172 \n\
49431
2e794757bdc8 Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49386
diff changeset
7173 Return S left-justified in a Unicode string of length width. Padding is\n\
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30765
diff changeset
7174 done using the specified fill character (default is a space).");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7175
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7176 static PyObject *
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7177 unicode_ljust(PyUnicodeObject *self, PyObject *args)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7178 {
37662
0678ce9fb6b2 Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 37661
diff changeset
7179 Py_ssize_t width;
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30765
diff changeset
7180 Py_UNICODE fillchar = ' ';
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30765
diff changeset
7181
37662
0678ce9fb6b2 Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 37661
diff changeset
7182 if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar))
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7183 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7184
19885
72e6eb653312 More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19849
diff changeset
7185 if (self->length >= width && PyUnicode_CheckExact(self)) {
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7186 Py_INCREF(self);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7187 return (PyObject*) self;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7188 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7189
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30765
diff changeset
7190 return (PyObject*) pad(self, 0, width - self->length, fillchar);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7191 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7192
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7193 PyDoc_STRVAR(lower__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7194 "S.lower() -> unicode\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7195 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7196 Return a copy of the string S converted to lowercase.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7197
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7198 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
7199 unicode_lower(PyUnicodeObject *self)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7200 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7201 return fixup(self, fixlower);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7202 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7203
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7204 #define LEFTSTRIP 0
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7205 #define RIGHTSTRIP 1
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7206 #define BOTHSTRIP 2
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7207
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7208 /* Arrays indexed by above */
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7209 static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"};
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7210
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7211 #define STRIPNAME(i) (stripformat[i]+3)
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7212
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7213 /* externally visible for str.strip(unicode) */
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7214 PyObject *
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7215 _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj)
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7216 {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7217 Py_UNICODE *s = PyUnicode_AS_UNICODE(self);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7218 Py_ssize_t len = PyUnicode_GET_SIZE(self);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7219 Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7220 Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7221 Py_ssize_t i, j;
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7222
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7223 BLOOM_MASK sepmask = make_bloom_mask(sep, seplen);
38257
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
7224
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7225 i = 0;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7226 if (striptype != RIGHTSTRIP) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7227 while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7228 i++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7229 }
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7230 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7231
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7232 j = len;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7233 if (striptype != LEFTSTRIP) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7234 do {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7235 j--;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7236 } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen));
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7237 j++;
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7238 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7239
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7240 if (i == 0 && j == len && PyUnicode_CheckExact(self)) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7241 Py_INCREF(self);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7242 return (PyObject*)self;
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7243 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7244 else
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7245 return PyUnicode_FromUnicode(s+i, j-i);
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7246 }
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7247
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7248
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7249 static PyObject *
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7250 do_strip(PyUnicodeObject *self, int striptype)
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7251 {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7252 Py_UNICODE *s = PyUnicode_AS_UNICODE(self);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7253 Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7254
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7255 i = 0;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7256 if (striptype != RIGHTSTRIP) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7257 while (i < len && Py_UNICODE_ISSPACE(s[i])) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7258 i++;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7259 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7260 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7261
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7262 j = len;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7263 if (striptype != LEFTSTRIP) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7264 do {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7265 j--;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7266 } while (j >= i && Py_UNICODE_ISSPACE(s[j]));
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7267 j++;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7268 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7269
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7270 if (i == 0 && j == len && PyUnicode_CheckExact(self)) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7271 Py_INCREF(self);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7272 return (PyObject*)self;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7273 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7274 else
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7275 return PyUnicode_FromUnicode(s+i, j-i);
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7276 }
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7277
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7278
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7279 static PyObject *
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7280 do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7281 {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7282 PyObject *sep = NULL;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7283
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7284 if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep))
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7285 return NULL;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7286
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7287 if (sep != NULL && sep != Py_None) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7288 if (PyUnicode_Check(sep))
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7289 return _PyUnicode_XStrip(self, striptype, sep);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7290 else if (PyString_Check(sep)) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7291 PyObject *res;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7292 sep = PyUnicode_FromObject(sep);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7293 if (sep==NULL)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7294 return NULL;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7295 res = _PyUnicode_XStrip(self, striptype, sep);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7296 Py_DECREF(sep);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7297 return res;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7298 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7299 else {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7300 PyErr_Format(PyExc_TypeError,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7301 "%s arg must be None, unicode or str",
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7302 STRIPNAME(striptype));
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7303 return NULL;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7304 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7305 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7306
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7307 return do_strip(self, striptype);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7308 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7309
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7310
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7311 PyDoc_STRVAR(strip__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7312 "S.strip([chars]) -> unicode\n\
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7313 \n\
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7314 Return a copy of the string S with leading and trailing\n\
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7315 whitespace removed.\n\
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
7316 If chars is given and not None, remove characters in chars instead.\n\
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
7317 If chars is a str, it will be converted to unicode before stripping");
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7318
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7319 static PyObject *
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7320 unicode_strip(PyUnicodeObject *self, PyObject *args)
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7321 {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7322 if (PyTuple_GET_SIZE(args) == 0)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7323 return do_strip(self, BOTHSTRIP); /* Common case */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7324 else
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7325 return do_argstrip(self, BOTHSTRIP, args);
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7326 }
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7327
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7328
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7329 PyDoc_STRVAR(lstrip__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7330 "S.lstrip([chars]) -> unicode\n\
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7331 \n\
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7332 Return a copy of the string S with leading whitespace removed.\n\
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
7333 If chars is given and not None, remove characters in chars instead.\n\
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
7334 If chars is a str, it will be converted to unicode before stripping");
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7335
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7336 static PyObject *
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7337 unicode_lstrip(PyUnicodeObject *self, PyObject *args)
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7338 {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7339 if (PyTuple_GET_SIZE(args) == 0)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7340 return do_strip(self, LEFTSTRIP); /* Common case */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7341 else
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7342 return do_argstrip(self, LEFTSTRIP, args);
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7343 }
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7344
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7345
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7346 PyDoc_STRVAR(rstrip__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7347 "S.rstrip([chars]) -> unicode\n\
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7348 \n\
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7349 Return a copy of the string S with trailing whitespace removed.\n\
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
7350 If chars is given and not None, remove characters in chars instead.\n\
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
7351 If chars is a str, it will be converted to unicode before stripping");
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7352
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7353 static PyObject *
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7354 unicode_rstrip(PyUnicodeObject *self, PyObject *args)
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7355 {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7356 if (PyTuple_GET_SIZE(args) == 0)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7357 return do_strip(self, RIGHTSTRIP); /* Common case */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7358 else
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7359 return do_argstrip(self, RIGHTSTRIP, args);
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7360 }
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7361
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
7362
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7363 static PyObject*
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
7364 unicode_repeat(PyUnicodeObject *str, Py_ssize_t len)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7365 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7366 PyUnicodeObject *u;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7367 Py_UNICODE *p;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
7368 Py_ssize_t nchars;
14492
7728b0ea9344 Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents: 14388
diff changeset
7369 size_t nbytes;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7370
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7371 if (len < 0)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7372 len = 0;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7373
19885
72e6eb653312 More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19849
diff changeset
7374 if (len == 1 && PyUnicode_CheckExact(str)) {
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7375 /* no repeat, return original string */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7376 Py_INCREF(str);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7377 return (PyObject*) str;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7378 }
14492
7728b0ea9344 Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents: 14388
diff changeset
7379
7728b0ea9344 Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents: 14388
diff changeset
7380 /* ensure # of chars needed doesn't overflow int and # of bytes
7728b0ea9344 Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents: 14388
diff changeset
7381 * needed doesn't overflow size_t
7728b0ea9344 Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents: 14388
diff changeset
7382 */
7728b0ea9344 Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents: 14388
diff changeset
7383 nchars = len * str->length;
7728b0ea9344 Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents: 14388
diff changeset
7384 if (len && nchars / len != str->length) {
7728b0ea9344 Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents: 14388
diff changeset
7385 PyErr_SetString(PyExc_OverflowError,
7728b0ea9344 Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents: 14388
diff changeset
7386 "repeated string is too long");
7728b0ea9344 Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents: 14388
diff changeset
7387 return NULL;
7728b0ea9344 Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents: 14388
diff changeset
7388 }
7728b0ea9344 Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents: 14388
diff changeset
7389 nbytes = (nchars + 1) * sizeof(Py_UNICODE);
7728b0ea9344 Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents: 14388
diff changeset
7390 if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) {
7728b0ea9344 Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents: 14388
diff changeset
7391 PyErr_SetString(PyExc_OverflowError,
7728b0ea9344 Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents: 14388
diff changeset
7392 "repeated string is too long");
7728b0ea9344 Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents: 14388
diff changeset
7393 return NULL;
7728b0ea9344 Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents: 14388
diff changeset
7394 }
7728b0ea9344 Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents: 14388
diff changeset
7395 u = _PyUnicode_New(nchars);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7396 if (!u)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7397 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7398
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7399 p = u->str;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7400
38238
feb00df91922 needforspeed: speed up unicode repeat, unicode string copy
Fredrik Lundh <fredrik@pythonware.com>
parents: 38236
diff changeset
7401 if (str->length == 1 && len > 0) {
feb00df91922 needforspeed: speed up unicode repeat, unicode string copy
Fredrik Lundh <fredrik@pythonware.com>
parents: 38236
diff changeset
7402 Py_UNICODE_FILL(p, str->str[0], len);
38239
604af867c65f needforspeed: use memcpy for "long" strings; use a better algorithm
Fredrik Lundh <fredrik@pythonware.com>
parents: 38238
diff changeset
7403 } else {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7404 Py_ssize_t done = 0; /* number of characters copied this far */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7405 if (done < nchars) {
38238
feb00df91922 needforspeed: speed up unicode repeat, unicode string copy
Fredrik Lundh <fredrik@pythonware.com>
parents: 38236
diff changeset
7406 Py_UNICODE_COPY(p, str->str, str->length);
38239
604af867c65f needforspeed: use memcpy for "long" strings; use a better algorithm
Fredrik Lundh <fredrik@pythonware.com>
parents: 38238
diff changeset
7407 done = str->length;
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7408 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7409 while (done < nchars) {
46062
09adff3d6691 Try to fix a bunch of compiler warnings on Win64.
Neal Norwitz <nnorwitz@gmail.com>
parents: 46059
diff changeset
7410 Py_ssize_t n = (done <= nchars-done) ? done : nchars-done;
38239
604af867c65f needforspeed: use memcpy for "long" strings; use a better algorithm
Fredrik Lundh <fredrik@pythonware.com>
parents: 38238
diff changeset
7411 Py_UNICODE_COPY(p+done, p, n);
604af867c65f needforspeed: use memcpy for "long" strings; use a better algorithm
Fredrik Lundh <fredrik@pythonware.com>
parents: 38238
diff changeset
7412 done += n;
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7413 }
38239
604af867c65f needforspeed: use memcpy for "long" strings; use a better algorithm
Fredrik Lundh <fredrik@pythonware.com>
parents: 38238
diff changeset
7414 }
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7415
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7416 return (PyObject*) u;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7417 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7418
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7419 PyObject *PyUnicode_Replace(PyObject *obj,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7420 PyObject *subobj,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7421 PyObject *replobj,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7422 Py_ssize_t maxcount)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7423 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7424 PyObject *self;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7425 PyObject *str1;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7426 PyObject *str2;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7427 PyObject *result;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7428
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7429 self = PyUnicode_FromObject(obj);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7430 if (self == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7431 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7432 str1 = PyUnicode_FromObject(subobj);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7433 if (str1 == NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7434 Py_DECREF(self);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7435 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7436 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7437 str2 = PyUnicode_FromObject(replobj);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7438 if (str2 == NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7439 Py_DECREF(self);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7440 Py_DECREF(str1);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7441 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7442 }
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
7443 result = replace((PyUnicodeObject *)self,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7444 (PyUnicodeObject *)str1,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7445 (PyUnicodeObject *)str2,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7446 maxcount);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7447 Py_DECREF(self);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7448 Py_DECREF(str1);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7449 Py_DECREF(str2);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7450 return result;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7451 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7452
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7453 PyDoc_STRVAR(replace__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7454 "S.replace (old, new[, count]) -> unicode\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7455 \n\
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7456 Return a copy of S with all occurrences of substring\n\
47388
d6639cd9575f #2999: fix name of third parameter in unicode.replace()'s docstring.
Georg Brandl <georg@python.org>
parents: 47294
diff changeset
7457 old replaced by new. If the optional argument count is\n\
d6639cd9575f #2999: fix name of third parameter in unicode.replace()'s docstring.
Georg Brandl <georg@python.org>
parents: 47294
diff changeset
7458 given, only the first count occurrences are replaced.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7459
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7460 static PyObject*
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7461 unicode_replace(PyUnicodeObject *self, PyObject *args)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7462 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7463 PyUnicodeObject *str1;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7464 PyUnicodeObject *str2;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
7465 Py_ssize_t maxcount = -1;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7466 PyObject *result;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7467
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
7468 if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount))
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7469 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7470 str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7471 if (str1 == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7472 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7473 str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2);
27774
bfc524379307 Fix two refcounting bugs
Walter Dörwald <walter@livinglogic.de>
parents: 27555
diff changeset
7474 if (str2 == NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7475 Py_DECREF(str1);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7476 return NULL;
27774
bfc524379307 Fix two refcounting bugs
Walter Dörwald <walter@livinglogic.de>
parents: 27555
diff changeset
7477 }
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7478
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7479 result = replace(self, str1, str2, maxcount);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7480
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7481 Py_DECREF(str1);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7482 Py_DECREF(str2);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7483 return result;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7484 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7485
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7486 static
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7487 PyObject *unicode_repr(PyObject *unicode)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7488 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7489 return unicodeescape_string(PyUnicode_AS_UNICODE(unicode),
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7490 PyUnicode_GET_SIZE(unicode),
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7491 1);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7492 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7493
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7494 PyDoc_STRVAR(rfind__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7495 "S.rfind(sub [,start [,end]]) -> int\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7496 \n\
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7497 Return the highest index in S where substring sub is found,\n\
42402
251033a1c2e3 Bug #1763149: use proper slice syntax in docstring.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
7498 such that sub is contained within s[start:end]. Optional\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7499 arguments start and end are interpreted as in slice notation.\n\
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7500 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7501 Return -1 on failure.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7502
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7503 static PyObject *
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7504 unicode_rfind(PyUnicodeObject *self, PyObject *args)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7505 {
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
7506 PyObject *substring;
43886
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 43710
diff changeset
7507 Py_ssize_t start;
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 43710
diff changeset
7508 Py_ssize_t end;
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
7509 Py_ssize_t result;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7510
43886
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 43710
diff changeset
7511 if (!_ParseTupleFinds(args, &substring, &start, &end))
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7512 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7513
38441
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
7514 result = stringlib_rfind_slice(
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
7515 PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self),
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
7516 PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring),
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
7517 start, end
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
7518 );
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7519
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7520 Py_DECREF(substring);
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
7521
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
7522 return PyInt_FromSsize_t(result);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7523 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7524
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7525 PyDoc_STRVAR(rindex__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7526 "S.rindex(sub [,start [,end]]) -> int\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7527 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7528 Like S.rfind() but raise ValueError when the substring is not found.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7529
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7530 static PyObject *
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7531 unicode_rindex(PyUnicodeObject *self, PyObject *args)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7532 {
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
7533 PyObject *substring;
43886
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 43710
diff changeset
7534 Py_ssize_t start;
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 43710
diff changeset
7535 Py_ssize_t end;
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
7536 Py_ssize_t result;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7537
43886
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 43710
diff changeset
7538 if (!_ParseTupleFinds(args, &substring, &start, &end))
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7539 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7540
38441
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
7541 result = stringlib_rfind_slice(
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
7542 PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self),
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
7543 PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring),
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
7544 start, end
72327f8f4a79 needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents: 38440
diff changeset
7545 );
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7546
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7547 Py_DECREF(substring);
38424
3d81faf47af7 needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents: 38422
diff changeset
7548
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7549 if (result < 0) {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7550 PyErr_SetString(PyExc_ValueError, "substring not found");
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7551 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7552 }
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
7553 return PyInt_FromSsize_t(result);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7554 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7555
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7556 PyDoc_STRVAR(rjust__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7557 "S.rjust(width[, fillchar]) -> unicode\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7558 \n\
49431
2e794757bdc8 Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49386
diff changeset
7559 Return S right-justified in a Unicode string of length width. Padding is\n\
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30765
diff changeset
7560 done using the specified fill character (default is a space).");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7561
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7562 static PyObject *
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7563 unicode_rjust(PyUnicodeObject *self, PyObject *args)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7564 {
37662
0678ce9fb6b2 Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 37661
diff changeset
7565 Py_ssize_t width;
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30765
diff changeset
7566 Py_UNICODE fillchar = ' ';
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30765
diff changeset
7567
37662
0678ce9fb6b2 Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 37661
diff changeset
7568 if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar))
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7569 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7570
19885
72e6eb653312 More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19849
diff changeset
7571 if (self->length >= width && PyUnicode_CheckExact(self)) {
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7572 Py_INCREF(self);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7573 return (PyObject*) self;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7574 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7575
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30765
diff changeset
7576 return (PyObject*) pad(self, width - self->length, 0, fillchar);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7577 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7578
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7579 static PyObject*
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
7580 unicode_slice(PyUnicodeObject *self, Py_ssize_t start, Py_ssize_t end)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7581 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7582 /* standard clamping */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7583 if (start < 0)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7584 start = 0;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7585 if (end < 0)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7586 end = 0;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7587 if (end > self->length)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7588 end = self->length;
19885
72e6eb653312 More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19849
diff changeset
7589 if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) {
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7590 /* full slice, return original string */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7591 Py_INCREF(self);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7592 return (PyObject*) self;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7593 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7594 if (start > end)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7595 start = end;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7596 /* copy slice */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7597 return (PyObject*) PyUnicode_FromUnicode(self->str + start,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7598 end - start);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7599 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7600
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7601 PyObject *PyUnicode_Split(PyObject *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7602 PyObject *sep,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7603 Py_ssize_t maxsplit)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7604 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7605 PyObject *result;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
7606
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7607 s = PyUnicode_FromObject(s);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7608 if (s == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7609 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7610 if (sep != NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7611 sep = PyUnicode_FromObject(sep);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7612 if (sep == NULL) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7613 Py_DECREF(s);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7614 return NULL;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7615 }
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7616 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7617
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7618 result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7619
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7620 Py_DECREF(s);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7621 Py_XDECREF(sep);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7622 return result;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7623 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7624
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7625 PyDoc_STRVAR(split__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7626 "S.split([sep [,maxsplit]]) -> list of strings\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7627 \n\
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7628 Return a list of the words in S, using sep as the\n\
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7629 delimiter string. If maxsplit is given, at most maxsplit\n\
46871
877783333eae #2809: elaborate str.split docstring a bit.
Georg Brandl <georg@python.org>
parents: 46391
diff changeset
7630 splits are done. If sep is not specified or is None, any\n\
46908
ed2c1888e143 #2809 followup: even better split docstring.
Georg Brandl <georg@python.org>
parents: 46871
diff changeset
7631 whitespace string is a separator and empty strings are\n\
ed2c1888e143 #2809 followup: even better split docstring.
Georg Brandl <georg@python.org>
parents: 46871
diff changeset
7632 removed from the result.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7633
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7634 static PyObject*
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7635 unicode_split(PyUnicodeObject *self, PyObject *args)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7636 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7637 PyObject *substring = Py_None;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
7638 Py_ssize_t maxcount = -1;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
7639
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
7640 if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount))
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7641 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7642
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7643 if (substring == Py_None)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7644 return split(self, NULL, maxcount);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7645 else if (PyUnicode_Check(substring))
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7646 return split(self, (PyUnicodeObject *)substring, maxcount);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7647 else
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7648 return PyUnicode_Split((PyObject *)self, substring, maxcount);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7649 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7650
38335
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
7651 PyObject *
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
7652 PyUnicode_Partition(PyObject *str_in, PyObject *sep_in)
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
7653 {
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
7654 PyObject* str_obj;
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
7655 PyObject* sep_obj;
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
7656 PyObject* out;
38383
05744917b7c3 needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38381
diff changeset
7657
38335
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
7658 str_obj = PyUnicode_FromObject(str_in);
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
7659 if (!str_obj)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7660 return NULL;
38335
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
7661 sep_obj = PyUnicode_FromObject(sep_in);
38383
05744917b7c3 needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38381
diff changeset
7662 if (!sep_obj) {
05744917b7c3 needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38381
diff changeset
7663 Py_DECREF(str_obj);
05744917b7c3 needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38381
diff changeset
7664 return NULL;
05744917b7c3 needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38381
diff changeset
7665 }
05744917b7c3 needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38381
diff changeset
7666
38400
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
7667 out = stringlib_partition(
38383
05744917b7c3 needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38381
diff changeset
7668 str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj),
05744917b7c3 needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38381
diff changeset
7669 sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj)
05744917b7c3 needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38381
diff changeset
7670 );
05744917b7c3 needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38381
diff changeset
7671
05744917b7c3 needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38381
diff changeset
7672 Py_DECREF(sep_obj);
05744917b7c3 needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38381
diff changeset
7673 Py_DECREF(str_obj);
38335
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
7674
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
7675 return out;
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
7676 }
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
7677
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7678
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7679 PyObject *
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7680 PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in)
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7681 {
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7682 PyObject* str_obj;
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7683 PyObject* sep_obj;
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7684 PyObject* out;
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7685
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7686 str_obj = PyUnicode_FromObject(str_in);
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7687 if (!str_obj)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7688 return NULL;
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7689 sep_obj = PyUnicode_FromObject(sep_in);
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7690 if (!sep_obj) {
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7691 Py_DECREF(str_obj);
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7692 return NULL;
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7693 }
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7694
38400
33c328896f52 needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
7695 out = stringlib_rpartition(
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7696 str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj),
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7697 sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj)
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7698 );
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7699
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7700 Py_DECREF(sep_obj);
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7701 Py_DECREF(str_obj);
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7702
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7703 return out;
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7704 }
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7705
38335
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
7706 PyDoc_STRVAR(partition__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7707 "S.partition(sep) -> (head, sep, tail)\n\
38335
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
7708 \n\
49431
2e794757bdc8 Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49386
diff changeset
7709 Search for the separator sep in S, and return the part before it,\n\
38335
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
7710 the separator itself, and the part after it. If the separator is not\n\
49431
2e794757bdc8 Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49386
diff changeset
7711 found, return S and two empty strings.");
38335
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
7712
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
7713 static PyObject*
38338
958f91684209 needforspeed: use METH_O for argument handling, which made partition some
Fredrik Lundh <fredrik@pythonware.com>
parents: 38335
diff changeset
7714 unicode_partition(PyUnicodeObject *self, PyObject *separator)
958f91684209 needforspeed: use METH_O for argument handling, which made partition some
Fredrik Lundh <fredrik@pythonware.com>
parents: 38335
diff changeset
7715 {
38335
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
7716 return PyUnicode_Partition((PyObject *)self, separator);
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
7717 }
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38304
diff changeset
7718
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7719 PyDoc_STRVAR(rpartition__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7720 "S.rpartition(sep) -> (tail, sep, head)\n\
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7721 \n\
49431
2e794757bdc8 Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49386
diff changeset
7722 Search for the separator sep in S, starting at the end of S, and return\n\
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7723 the part before it, the separator itself, and the part after it. If the\n\
49431
2e794757bdc8 Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49386
diff changeset
7724 separator is not found, return two empty strings and S.");
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7725
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7726 static PyObject*
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7727 unicode_rpartition(PyUnicodeObject *self, PyObject *separator)
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7728 {
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7729 return PyUnicode_RPartition((PyObject *)self, separator);
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7730 }
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
7731
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7732 PyObject *PyUnicode_RSplit(PyObject *s,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7733 PyObject *sep,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7734 Py_ssize_t maxsplit)
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7735 {
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7736 PyObject *result;
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
7737
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7738 s = PyUnicode_FromObject(s);
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7739 if (s == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7740 return NULL;
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7741 if (sep != NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7742 sep = PyUnicode_FromObject(sep);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7743 if (sep == NULL) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7744 Py_DECREF(s);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7745 return NULL;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7746 }
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7747 }
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7748
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7749 result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit);
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7750
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7751 Py_DECREF(s);
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7752 Py_XDECREF(sep);
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7753 return result;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7754 }
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7755
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7756 PyDoc_STRVAR(rsplit__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7757 "S.rsplit([sep [,maxsplit]]) -> list of strings\n\
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7758 \n\
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7759 Return a list of the words in S, using sep as the\n\
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7760 delimiter string, starting at the end of the string and\n\
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7761 working to the front. If maxsplit is given, at most maxsplit\n\
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7762 splits are done. If sep is not specified, any whitespace string\n\
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7763 is a separator.");
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7764
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7765 static PyObject*
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7766 unicode_rsplit(PyUnicodeObject *self, PyObject *args)
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7767 {
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7768 PyObject *substring = Py_None;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
7769 Py_ssize_t maxcount = -1;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
7770
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
7771 if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount))
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7772 return NULL;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7773
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7774 if (substring == Py_None)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7775 return rsplit(self, NULL, maxcount);
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7776 else if (PyUnicode_Check(substring))
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7777 return rsplit(self, (PyUnicodeObject *)substring, maxcount);
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7778 else
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7779 return PyUnicode_RSplit((PyObject *)self, substring, maxcount);
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7780 }
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
7781
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7782 PyDoc_STRVAR(splitlines__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7783 "S.splitlines([keepends]) -> list of strings\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7784 \n\
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7785 Return a list of the lines in S, breaking at line boundaries.\n\
12369
68e48bb94071 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12329
diff changeset
7786 Line breaks are not included in the resulting list unless keepends\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7787 is given and true.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7788
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7789 static PyObject*
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7790 unicode_splitlines(PyUnicodeObject *self, PyObject *args)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7791 {
12369
68e48bb94071 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12329
diff changeset
7792 int keepends = 0;
68e48bb94071 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12329
diff changeset
7793
68e48bb94071 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12329
diff changeset
7794 if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends))
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7795 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7796
12369
68e48bb94071 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12329
diff changeset
7797 return PyUnicode_Splitlines((PyObject *)self, keepends);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7798 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7799
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7800 static
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7801 PyObject *unicode_str(PyUnicodeObject *self)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7802 {
12645
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
7803 return PyUnicode_AsEncodedString((PyObject *)self, NULL, NULL);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7804 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7805
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7806 PyDoc_STRVAR(swapcase__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7807 "S.swapcase() -> unicode\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7808 \n\
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7809 Return a copy of S with uppercase characters converted to lowercase\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7810 and vice versa.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7811
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7812 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
7813 unicode_swapcase(PyUnicodeObject *self)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7814 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7815 return fixup(self, fixswapcase);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7816 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7817
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7818 PyDoc_STRVAR(translate__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7819 "S.translate(table) -> unicode\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7820 \n\
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7821 Return a copy of the string S, where all characters have been mapped\n\
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7822 through the given translation table, which must be a mapping of\n\
25120
953ebe0c0c25 Change the unicode.translate docstring to document that
Walter Dörwald <walter@livinglogic.de>
parents: 25091
diff changeset
7823 Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\
953ebe0c0c25 Change the unicode.translate docstring to document that
Walter Dörwald <walter@livinglogic.de>
parents: 25091
diff changeset
7824 Unmapped characters are left untouched. Characters mapped to None\n\
953ebe0c0c25 Change the unicode.translate docstring to document that
Walter Dörwald <walter@livinglogic.de>
parents: 25091
diff changeset
7825 are deleted.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7826
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7827 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
7828 unicode_translate(PyUnicodeObject *self, PyObject *table)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7829 {
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
7830 return PyUnicode_TranslateCharmap(self->str,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7831 self->length,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7832 table,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7833 "ignore");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7834 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7835
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7836 PyDoc_STRVAR(upper__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7837 "S.upper() -> unicode\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7838 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7839 Return a copy of S converted to uppercase.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7840
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7841 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
7842 unicode_upper(PyUnicodeObject *self)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7843 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7844 return fixup(self, fixupper);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7845 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7846
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7847 PyDoc_STRVAR(zfill__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7848 "S.zfill(width) -> unicode\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7849 \n\
49115
6f08278bf8b1 Fix varname in docstring. #3822.
Georg Brandl <georg@python.org>
parents: 48117
diff changeset
7850 Pad a numeric string S with zeros on the left, to fill a field\n\
6f08278bf8b1 Fix varname in docstring. #3822.
Georg Brandl <georg@python.org>
parents: 48117
diff changeset
7851 of the specified width. The string S is never truncated.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7852
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7853 static PyObject *
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7854 unicode_zfill(PyUnicodeObject *self, PyObject *args)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7855 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
7856 Py_ssize_t fill;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7857 PyUnicodeObject *u;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7858
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
7859 Py_ssize_t width;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
7860 if (!PyArg_ParseTuple(args, "n:zfill", &width))
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7861 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7862
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7863 if (self->length >= width) {
23051
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23045
diff changeset
7864 if (PyUnicode_CheckExact(self)) {
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23045
diff changeset
7865 Py_INCREF(self);
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23045
diff changeset
7866 return (PyObject*) self;
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23045
diff changeset
7867 }
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23045
diff changeset
7868 else
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23045
diff changeset
7869 return PyUnicode_FromUnicode(
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23045
diff changeset
7870 PyUnicode_AS_UNICODE(self),
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23045
diff changeset
7871 PyUnicode_GET_SIZE(self)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7872 );
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7873 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7874
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7875 fill = width - self->length;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7876
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7877 u = pad(self, fill, 0, '0');
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7878
23045
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 22981
diff changeset
7879 if (u == NULL)
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 22981
diff changeset
7880 return NULL;
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 22981
diff changeset
7881
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7882 if (u->str[fill] == '+' || u->str[fill] == '-') {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7883 /* move sign to beginning of string */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7884 u->str[0] = u->str[fill];
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7885 u->str[fill] = '0';
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7886 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7887
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7888 return (PyObject*) u;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7889 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7890
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7891 #if 0
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7892 static PyObject*
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 45053
diff changeset
7893 free_listsize(PyUnicodeObject *self)
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 45053
diff changeset
7894 {
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 45053
diff changeset
7895 return PyInt_FromLong(numfree);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7896 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7897 #endif
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7898
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7899 PyDoc_STRVAR(startswith__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7900 "S.startswith(prefix[, start[, end]]) -> bool\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7901 \n\
28559
aff080f2efb4 Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents: 28494
diff changeset
7902 Return True if S starts with the specified prefix, False otherwise.\n\
aff080f2efb4 Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents: 28494
diff changeset
7903 With optional start, test S beginning at that position.\n\
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7904 With optional end, stop comparing S at that position.\n\
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7905 prefix can also be a tuple of strings to try.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7906
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7907 static PyObject *
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7908 unicode_startswith(PyUnicodeObject *self,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7909 PyObject *args)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7910 {
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7911 PyObject *subobj;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7912 PyUnicodeObject *substring;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
7913 Py_ssize_t start = 0;
37662
0678ce9fb6b2 Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 37661
diff changeset
7914 Py_ssize_t end = PY_SSIZE_T_MAX;
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7915 int result;
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7916
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7917 if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7918 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7919 return NULL;
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7920 if (PyTuple_Check(subobj)) {
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7921 Py_ssize_t i;
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7922 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7923 substring = (PyUnicodeObject *)PyUnicode_FromObject(
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7924 PyTuple_GET_ITEM(subobj, i));
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7925 if (substring == NULL)
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7926 return NULL;
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7927 result = tailmatch(self, substring, start, end, -1);
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7928 Py_DECREF(substring);
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7929 if (result) {
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7930 Py_RETURN_TRUE;
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7931 }
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7932 }
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7933 /* nothing matched */
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7934 Py_RETURN_FALSE;
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7935 }
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7936 substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7937 if (substring == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7938 return NULL;
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7939 result = tailmatch(self, substring, start, end, -1);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7940 Py_DECREF(substring);
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7941 return PyBool_FromLong(result);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7942 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7943
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7944
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
7945 PyDoc_STRVAR(endswith__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7946 "S.endswith(suffix[, start[, end]]) -> bool\n\
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7947 \n\
28559
aff080f2efb4 Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents: 28494
diff changeset
7948 Return True if S ends with the specified suffix, False otherwise.\n\
aff080f2efb4 Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents: 28494
diff changeset
7949 With optional start, test S beginning at that position.\n\
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7950 With optional end, stop comparing S at that position.\n\
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7951 suffix can also be a tuple of strings to try.");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7952
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7953 static PyObject *
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7954 unicode_endswith(PyUnicodeObject *self,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7955 PyObject *args)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7956 {
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7957 PyObject *subobj;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7958 PyUnicodeObject *substring;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
7959 Py_ssize_t start = 0;
37662
0678ce9fb6b2 Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 37661
diff changeset
7960 Py_ssize_t end = PY_SSIZE_T_MAX;
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7961 int result;
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7962
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7963 if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7964 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7965 return NULL;
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7966 if (PyTuple_Check(subobj)) {
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7967 Py_ssize_t i;
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7968 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7969 substring = (PyUnicodeObject *)PyUnicode_FromObject(
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7970 PyTuple_GET_ITEM(subobj, i));
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7971 if (substring == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7972 return NULL;
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7973 result = tailmatch(self, substring, start, end, +1);
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7974 Py_DECREF(substring);
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7975 if (result) {
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7976 Py_RETURN_TRUE;
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7977 }
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7978 }
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7979 Py_RETURN_FALSE;
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7980 }
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7981 substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7982 if (substring == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7983 return NULL;
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7984
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7985 result = tailmatch(self, substring, start, end, +1);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7986 Py_DECREF(substring);
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38587
diff changeset
7987 return PyBool_FromLong(result);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7988 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7989
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7990
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45341
diff changeset
7991 /* Implements do_string_format, which is unicode because of stringlib */
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45341
diff changeset
7992 #include "stringlib/string_format.h"
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45341
diff changeset
7993
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45341
diff changeset
7994 PyDoc_STRVAR(format__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
7995 "S.format(*args, **kwargs) -> unicode\n\
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45341
diff changeset
7996 \n\
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45341
diff changeset
7997 ");
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45341
diff changeset
7998
47392
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
7999 static PyObject *
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8000 unicode__format__(PyObject *self, PyObject *args)
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8001 {
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8002 PyObject *format_spec;
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8003 PyObject *result = NULL;
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8004 PyObject *tmp = NULL;
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8005
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8006 /* If 2.x, convert format_spec to the same type as value */
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8007 /* This is to allow things like u''.format('') */
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8008 if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8009 goto done;
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8010 if (!(PyBytes_Check(format_spec) || PyUnicode_Check(format_spec))) {
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8011 PyErr_Format(PyExc_TypeError, "__format__ arg must be str "
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8012 "or unicode, not %s", Py_TYPE(format_spec)->tp_name);
47392
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8013 goto done;
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8014 }
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8015 tmp = PyObject_Unicode(format_spec);
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8016 if (tmp == NULL)
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8017 goto done;
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8018 format_spec = tmp;
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8019
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8020 result = _PyUnicode_FormatAdvanced(self,
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8021 PyUnicode_AS_UNICODE(format_spec),
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8022 PyUnicode_GET_SIZE(format_spec));
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8023 done:
47392
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8024 Py_XDECREF(tmp);
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8025 return result;
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8026 }
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47388
diff changeset
8027
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45341
diff changeset
8028 PyDoc_STRVAR(p_format__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8029 "S.__format__(format_spec) -> unicode\n\
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45341
diff changeset
8030 \n\
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45341
diff changeset
8031 ");
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45341
diff changeset
8032
47543
296ab1038b7f Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47527
diff changeset
8033 static PyObject *
296ab1038b7f Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47527
diff changeset
8034 unicode__sizeof__(PyUnicodeObject *v)
296ab1038b7f Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47527
diff changeset
8035 {
47922
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47543
diff changeset
8036 return PyInt_FromSsize_t(sizeof(PyUnicodeObject) +
7cfc28ac0715 Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47543
diff changeset
8037 sizeof(Py_UNICODE) * (v->length + 1));
47543
296ab1038b7f Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47527
diff changeset
8038 }
296ab1038b7f Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47527
diff changeset
8039
296ab1038b7f Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47527
diff changeset
8040 PyDoc_STRVAR(sizeof__doc__,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8041 "S.__sizeof__() -> size of S in memory, in bytes\n\
47543
296ab1038b7f Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47527
diff changeset
8042 \n\
296ab1038b7f Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47527
diff changeset
8043 ");
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27175
diff changeset
8044
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27175
diff changeset
8045 static PyObject *
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27175
diff changeset
8046 unicode_getnewargs(PyUnicodeObject *v)
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27175
diff changeset
8047 {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8048 return Py_BuildValue("(u#)", v->str, v->length);
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27175
diff changeset
8049 }
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27175
diff changeset
8050
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27175
diff changeset
8051
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8052 static PyMethodDef unicode_methods[] = {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8053
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8054 /* Order is according to common usage: often used methods should
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8055 appear first, since lookup is done sequentially. */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8056
37306
ce56c5e3cb20 That one was a mistake.
Georg Brandl <georg@python.org>
parents: 37304
diff changeset
8057 {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8058 {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8059 {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__},
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
8060 {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8061 {"join", (PyCFunction) unicode_join, METH_O, join__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8062 {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8063 {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8064 {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8065 {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8066 {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8067 {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__},
38338
958f91684209 needforspeed: use METH_O for argument handling, which made partition some
Fredrik Lundh <fredrik@pythonware.com>
parents: 38335
diff changeset
8068 {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8069 {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8070 {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8071 {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__},
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
8072 {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__},
32551
f46d49e2e0f0 Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents: 32363
diff changeset
8073 {"decode", (PyCFunction) unicode_decode, METH_VARARGS, decode__doc__},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8074 /* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8075 {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8076 {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8077 {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__},
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
8078 {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__},
38390
76807902a2f5 needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents: 38383
diff changeset
8079 {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8080 {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__},
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23205
diff changeset
8081 {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8082 {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8083 {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8084 {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8085 {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8086 {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8087 {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8088 {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8089 {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8090 {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8091 {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8092 {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8093 {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8094 {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8095 {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__},
23045
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 22981
diff changeset
8096 {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__},
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45341
diff changeset
8097 {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__},
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45341
diff changeset
8098 {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__},
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45341
diff changeset
8099 {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS},
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45341
diff changeset
8100 {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS},
47543
296ab1038b7f Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47527
diff changeset
8101 {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__},
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8102 #if 0
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19121
diff changeset
8103 {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__},
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8104 #endif
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8105
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8106 #if 0
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8107 /* This one is just used for debugging the implementation. */
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 45053
diff changeset
8108 {"freelistsize", (PyCFunction) free_listsize, METH_NOARGS},
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8109 #endif
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8110
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8111 {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS},
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8112 {NULL, NULL}
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8113 };
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8114
26274
958d07085dcf Add nb_remainder (i.e. __mod__) slot to unicode type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
8115 static PyObject *
958d07085dcf Add nb_remainder (i.e. __mod__) slot to unicode type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
8116 unicode_mod(PyObject *v, PyObject *w)
958d07085dcf Add nb_remainder (i.e. __mod__) slot to unicode type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
8117 {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8118 if (!PyUnicode_Check(v)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8119 Py_INCREF(Py_NotImplemented);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8120 return Py_NotImplemented;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8121 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8122 return PyUnicode_Format(v, w);
26274
958d07085dcf Add nb_remainder (i.e. __mod__) slot to unicode type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
8123 }
958d07085dcf Add nb_remainder (i.e. __mod__) slot to unicode type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
8124
958d07085dcf Add nb_remainder (i.e. __mod__) slot to unicode type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
8125 static PyNumberMethods unicode_as_number = {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8126 0, /*nb_add*/
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8127 0, /*nb_subtract*/
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8128 0, /*nb_multiply*/
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8129 0, /*nb_divide*/
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8130 unicode_mod, /*nb_remainder*/
26274
958d07085dcf Add nb_remainder (i.e. __mod__) slot to unicode type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
8131 };
958d07085dcf Add nb_remainder (i.e. __mod__) slot to unicode type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
8132
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8133 static PySequenceMethods unicode_as_sequence = {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8134 (lenfunc) unicode_length, /* sq_length */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8135 PyUnicode_Concat, /* sq_concat */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8136 (ssizeargfunc) unicode_repeat, /* sq_repeat */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8137 (ssizeargfunc) unicode_getitem, /* sq_item */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8138 (ssizessizeargfunc) unicode_slice, /* sq_slice */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8139 0, /* sq_ass_item */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8140 0, /* sq_ass_slice */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8141 PyUnicode_Contains, /* sq_contains */
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8142 };
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8143
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8144 static PyObject*
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8145 unicode_subscript(PyUnicodeObject* self, PyObject* item)
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8146 {
39491
2312e178b34d Correct an accidentally removed previous patch.
Marc-André Lemburg <mal@egenix.com>
parents: 39487
diff changeset
8147 if (PyIndex_Check(item)) {
2312e178b34d Correct an accidentally removed previous patch.
Marc-André Lemburg <mal@egenix.com>
parents: 39487
diff changeset
8148 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8149 if (i == -1 && PyErr_Occurred())
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8150 return NULL;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8151 if (i < 0)
36203
44cfca6136c6 Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents: 36056
diff changeset
8152 i += PyUnicode_GET_SIZE(self);
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8153 return unicode_getitem(self, i);
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8154 } else if (PySlice_Check(item)) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
8155 Py_ssize_t start, stop, step, slicelength, cur, i;
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8156 Py_UNICODE* source_buf;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8157 Py_UNICODE* result_buf;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8158 PyObject* result;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8159
36203
44cfca6136c6 Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents: 36056
diff changeset
8160 if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self),
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8161 &start, &stop, &step, &slicelength) < 0) {
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8162 return NULL;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8163 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8164
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8165 if (slicelength <= 0) {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8166 return PyUnicode_FromUnicode(NULL, 0);
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42652
diff changeset
8167 } else if (start == 0 && step == 1 && slicelength == self->length &&
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42652
diff changeset
8168 PyUnicode_CheckExact(self)) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42652
diff changeset
8169 Py_INCREF(self);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42652
diff changeset
8170 return (PyObject *)self;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42652
diff changeset
8171 } else if (step == 1) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42652
diff changeset
8172 return PyUnicode_FromUnicode(self->str + start, slicelength);
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8173 } else {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8174 source_buf = PyUnicode_AS_UNICODE((PyObject*)self);
45804
d33cadb595f6 Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents: 45731
diff changeset
8175 result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength*
d33cadb595f6 Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents: 45731
diff changeset
8176 sizeof(Py_UNICODE));
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8177
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8178 if (result_buf == NULL)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8179 return PyErr_NoMemory();
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8180
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8181 for (cur = start, i = 0; i < slicelength; cur += step, i++) {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8182 result_buf[i] = source_buf[cur];
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8183 }
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
8184
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8185 result = PyUnicode_FromUnicode(result_buf, slicelength);
45804
d33cadb595f6 Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents: 45731
diff changeset
8186 PyObject_FREE(result_buf);
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8187 return result;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8188 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8189 } else {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8190 PyErr_SetString(PyExc_TypeError, "string indices must be integers");
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8191 return NULL;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8192 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8193 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8194
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8195 static PyMappingMethods unicode_as_mapping = {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8196 (lenfunc)unicode_length, /* mp_length */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8197 (binaryfunc)unicode_subscript, /* mp_subscript */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8198 (objobjargproc)0, /* mp_ass_subscript */
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8199 };
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23562
diff changeset
8200
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
8201 static Py_ssize_t
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8202 unicode_buffer_getreadbuf(PyUnicodeObject *self,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8203 Py_ssize_t index,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8204 const void **ptr)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8205 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8206 if (index != 0) {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8207 PyErr_SetString(PyExc_SystemError,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8208 "accessing non-existent unicode segment");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8209 return -1;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8210 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8211 *ptr = (void *) self->str;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8212 return PyUnicode_GET_DATA_SIZE(self);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8213 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8214
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
8215 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
8216 unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8217 const void **ptr)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8218 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8219 PyErr_SetString(PyExc_TypeError,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8220 "cannot use unicode as modifiable buffer");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8221 return -1;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8222 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8223
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8224 static int
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8225 unicode_buffer_getsegcount(PyUnicodeObject *self,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8226 Py_ssize_t *lenp)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8227 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8228 if (lenp)
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8229 *lenp = PyUnicode_GET_DATA_SIZE(self);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8230 return 1;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8231 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8232
36519
d6d415df0df0 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
8233 static Py_ssize_t
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8234 unicode_buffer_getcharbuf(PyUnicodeObject *self,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8235 Py_ssize_t index,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8236 const void **ptr)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8237 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8238 PyObject *str;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
8239
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8240 if (index != 0) {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8241 PyErr_SetString(PyExc_SystemError,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8242 "accessing non-existent unicode segment");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8243 return -1;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8244 }
13837
2badb322355f This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents: 13828
diff changeset
8245 str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8246 if (str == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8247 return -1;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
8248 *ptr = (void *) PyString_AS_STRING(str);
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
8249 return PyString_GET_SIZE(str);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8250 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8251
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8252 /* Helpers for PyUnicode_Format() */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8253
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8254 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
8255 getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx)
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
8256 {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
8257 Py_ssize_t argidx = *p_argidx;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8258 if (argidx < arglen) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8259 (*p_argidx)++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8260 if (arglen < 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8261 return args;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8262 else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8263 return PyTuple_GetItem(args, argidx);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8264 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8265 PyErr_SetString(PyExc_TypeError,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8266 "not enough arguments for format string");
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8267 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8268 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8269
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8270 #define F_LJUST (1<<0)
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8271 #define F_SIGN (1<<1)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8272 #define F_BLANK (1<<2)
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8273 #define F_ALT (1<<3)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8274 #define F_ZERO (1<<4)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8275
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
8276 static Py_ssize_t
36261
303dea847639 - Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36236
diff changeset
8277 strtounicode(Py_UNICODE *buffer, const char *charbuffer)
303dea847639 - Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36236
diff changeset
8278 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
8279 register Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
8280 Py_ssize_t len = strlen(charbuffer);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8281 for (i = len - 1; i >= 0; i--)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8282 buffer[i] = (Py_UNICODE) charbuffer[i];
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8283
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8284 return len;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8285 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8286
36261
303dea847639 - Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36236
diff changeset
8287 static int
53442
6947bc3dd433 Issue #5835, deprecate PyOS_ascii_formatd.
Eric Smith <eric@trueblade.com>
parents: 52410
diff changeset
8288 doubletounicode(Py_UNICODE *buffer, size_t len, int format_code,
6947bc3dd433 Issue #5835, deprecate PyOS_ascii_formatd.
Eric Smith <eric@trueblade.com>
parents: 52410
diff changeset
8289 int precision, int flags, double x)
36261
303dea847639 - Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36236
diff changeset
8290 {
36508
7322dccba224 doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents: 36506
diff changeset
8291 Py_ssize_t result;
7322dccba224 doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents: 36506
diff changeset
8292
53442
6947bc3dd433 Issue #5835, deprecate PyOS_ascii_formatd.
Eric Smith <eric@trueblade.com>
parents: 52410
diff changeset
8293 _PyOS_double_to_string((char *)buffer, len, x, format_code, precision,
6947bc3dd433 Issue #5835, deprecate PyOS_ascii_formatd.
Eric Smith <eric@trueblade.com>
parents: 52410
diff changeset
8294 flags, NULL);
36508
7322dccba224 doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents: 36506
diff changeset
8295 result = strtounicode(buffer, (char *)buffer);
7322dccba224 doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents: 36506
diff changeset
8296 return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);
36261
303dea847639 - Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36236
diff changeset
8297 }
303dea847639 - Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36236
diff changeset
8298
303dea847639 - Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36236
diff changeset
8299 static int
303dea847639 - Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36236
diff changeset
8300 longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x)
303dea847639 - Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36236
diff changeset
8301 {
36508
7322dccba224 doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents: 36506
diff changeset
8302 Py_ssize_t result;
7322dccba224 doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents: 36506
diff changeset
8303
36261
303dea847639 - Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36236
diff changeset
8304 PyOS_snprintf((char *)buffer, len, format, x);
36508
7322dccba224 doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents: 36506
diff changeset
8305 result = strtounicode(buffer, (char *)buffer);
7322dccba224 doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents: 36506
diff changeset
8306 return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);
36261
303dea847639 - Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36236
diff changeset
8307 }
303dea847639 - Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36236
diff changeset
8308
24736
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24703
diff changeset
8309 /* XXX To save some code duplication, formatfloat/long/int could have been
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24703
diff changeset
8310 shared with stringobject.c, converting from 8-bit to Unicode after the
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24703
diff changeset
8311 formatting is done. */
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24703
diff changeset
8312
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8313 static int
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8314 formatfloat(Py_UNICODE *buf,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8315 size_t buflen,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8316 int flags,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8317 int prec,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8318 int type,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8319 PyObject *v)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8320 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8321 double x;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
8322
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8323 x = PyFloat_AsDouble(v);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8324 if (x == -1.0 && PyErr_Occurred())
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8325 return -1;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8326 if (prec < 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8327 prec = 6;
52410
f05b6d9adbe6 Issue #532631: Apply floatformat changes to unicodeobject.c
Mark Dickinson <dickinsm@gmail.com>
parents: 52404
diff changeset
8328 /* make sure that the decimal representation of precision really does
f05b6d9adbe6 Issue #532631: Apply floatformat changes to unicodeobject.c
Mark Dickinson <dickinsm@gmail.com>
parents: 52404
diff changeset
8329 need at most 10 digits: platforms with sizeof(int) == 8 exist! */
f05b6d9adbe6 Issue #532631: Apply floatformat changes to unicodeobject.c
Mark Dickinson <dickinsm@gmail.com>
parents: 52404
diff changeset
8330 if (prec > 0x7fffffffL) {
f05b6d9adbe6 Issue #532631: Apply floatformat changes to unicodeobject.c
Mark Dickinson <dickinsm@gmail.com>
parents: 52404
diff changeset
8331 PyErr_SetString(PyExc_OverflowError,
f05b6d9adbe6 Issue #532631: Apply floatformat changes to unicodeobject.c
Mark Dickinson <dickinsm@gmail.com>
parents: 52404
diff changeset
8332 "outrageously large precision "
f05b6d9adbe6 Issue #532631: Apply floatformat changes to unicodeobject.c
Mark Dickinson <dickinsm@gmail.com>
parents: 52404
diff changeset
8333 "for formatted float");
f05b6d9adbe6 Issue #532631: Apply floatformat changes to unicodeobject.c
Mark Dickinson <dickinsm@gmail.com>
parents: 52404
diff changeset
8334 return -1;
f05b6d9adbe6 Issue #532631: Apply floatformat changes to unicodeobject.c
Mark Dickinson <dickinsm@gmail.com>
parents: 52404
diff changeset
8335 }
f05b6d9adbe6 Issue #532631: Apply floatformat changes to unicodeobject.c
Mark Dickinson <dickinsm@gmail.com>
parents: 52404
diff changeset
8336
52404
019815bc37ea Issue #532631: Replace confusing fabs(x)/1e25 >= 1e25 test
Mark Dickinson <dickinsm@gmail.com>
parents: 52251
diff changeset
8337 if (type == 'f' && fabs(x) >= 1e50)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8338 type = 'g';
26825
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26274
diff changeset
8339 /* Worst case length calc to ensure no buffer overrun:
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26274
diff changeset
8340
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26274
diff changeset
8341 'g' formats:
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8342 fmt = %#.<prec>g
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8343 buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8344 for any double rep.)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8345 len = 1 + prec + 1 + 2 + 5 = 9 + prec
26825
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26274
diff changeset
8346
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26274
diff changeset
8347 'f' formats:
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8348 buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8349 len = 1 + 50 + 1 + prec = 52 + prec
26825
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26274
diff changeset
8350
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26274
diff changeset
8351 If prec=0 the effective precision is 1 (the leading digit is
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
8352 always given), therefore increase the length by one.
26825
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26274
diff changeset
8353
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26274
diff changeset
8354 */
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8355 if (((type == 'g' || type == 'G') &&
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8356 buflen <= (size_t)10 + (size_t)prec) ||
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8357 (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8358 PyErr_SetString(PyExc_OverflowError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8359 "formatted float is too long (precision too large?)");
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8360 return -1;
26825
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26274
diff changeset
8361 }
53442
6947bc3dd433 Issue #5835, deprecate PyOS_ascii_formatd.
Eric Smith <eric@trueblade.com>
parents: 52410
diff changeset
8362 return doubletounicode(buf, buflen, type, prec,
6947bc3dd433 Issue #5835, deprecate PyOS_ascii_formatd.
Eric Smith <eric@trueblade.com>
parents: 52410
diff changeset
8363 (flags&F_ALT)?Py_DTSF_ALT:0, x);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8364 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8365
14713
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14492
diff changeset
8366 static PyObject*
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14492
diff changeset
8367 formatlong(PyObject *val, int flags, int prec, int type)
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14492
diff changeset
8368 {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8369 char *buf;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8370 int i, len;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8371 PyObject *str; /* temporary string object. */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8372 PyUnicodeObject *result;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8373
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8374 str = _PyString_FormatLong(val, flags, prec, type, &buf, &len);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8375 if (!str)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8376 return NULL;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8377 result = _PyUnicode_New(len);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8378 if (!result) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8379 Py_DECREF(str);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8380 return NULL;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8381 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8382 for (i = 0; i < len; i++)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8383 result->str[i] = buf[i];
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8384 result->str[len] = 0;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8385 Py_DECREF(str);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8386 return (PyObject*)result;
14713
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14492
diff changeset
8387 }
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14492
diff changeset
8388
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8389 static int
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8390 formatint(Py_UNICODE *buf,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8391 size_t buflen,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8392 int flags,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8393 int prec,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8394 int type,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8395 PyObject *v)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8396 {
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 13110
diff changeset
8397 /* fmt = '%#.' + `prec` + 'l' + `type`
22087
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8398 * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine)
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8399 * + 1 + 1
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8400 * = 24
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8401 */
14713
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14492
diff changeset
8402 char fmt[64]; /* plenty big enough! */
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
8403 char *sign;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8404 long x;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8405
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8406 x = PyInt_AsLong(v);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8407 if (x == -1 && PyErr_Occurred())
22087
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8408 return -1;
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
8409 if (x < 0 && type == 'u') {
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
8410 type = 'd';
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
8411 }
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
8412 if (x < 0 && (type == 'x' || type == 'X' || type == 'o'))
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
8413 sign = "-";
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
8414 else
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
8415 sign = "";
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8416 if (prec < 0)
22087
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8417 prec = 1;
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8418
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
8419 /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal))
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
8420 * worst case buf = '-0x' + [0-9]*prec, where prec >= 11
17450
f74f5aafdc7e Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents: 17425
diff changeset
8421 */
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
8422 if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) {
22087
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8423 PyErr_SetString(PyExc_OverflowError,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8424 "formatted integer is too long (precision too large?)");
22087
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8425 return -1;
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8426 }
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8427
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8428 if ((flags & F_ALT) &&
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8429 (type == 'x' || type == 'X')) {
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
8430 /* When converting under %#x or %#X, there are a number
22087
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8431 * of issues that cause pain:
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8432 * - when 0 is being converted, the C standard leaves off
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8433 * the '0x' or '0X', which is inconsistent with other
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8434 * %#x/%#X conversions and inconsistent with Python's
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8435 * hex() function
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8436 * - there are platforms that violate the standard and
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8437 * convert 0 with the '0x' or '0X'
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8438 * (Metrowerks, Compaq Tru64)
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8439 * - there are platforms that give '0x' when converting
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
8440 * under %#X, but convert 0 in accordance with the
22087
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8441 * standard (OS/2 EMX)
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
8442 *
22087
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8443 * We can achieve the desired consistency by inserting our
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8444 * own '0x' or '0X' prefix, and substituting %x/%X in place
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8445 * of %#x/%#X.
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8446 *
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8447 * Note that this is the same approach as used in
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8448 * formatint() in stringobject.c
22053
49197fd76362 OS/2 EMX port changes (Objects part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
8449 */
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
8450 PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c",
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
8451 sign, type, prec, type);
22087
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8452 }
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8453 else {
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
8454 PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c",
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
8455 sign, (flags&F_ALT) ? "#" : "",
22087
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8456 prec, type);
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
8457 }
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
8458 if (sign[0])
36261
303dea847639 - Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36236
diff changeset
8459 return longtounicode(buf, buflen, fmt, -x);
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
8460 else
36261
303dea847639 - Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36236
diff changeset
8461 return longtounicode(buf, buflen, fmt, x);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8462 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8463
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8464 static int
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8465 formatchar(Py_UNICODE *buf,
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 13110
diff changeset
8466 size_t buflen,
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 13110
diff changeset
8467 PyObject *v)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8468 {
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 13110
diff changeset
8469 /* presume that the buffer is at least 2 characters long */
12899
9df91f91e5cc Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12876
diff changeset
8470 if (PyUnicode_Check(v)) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8471 if (PyUnicode_GET_SIZE(v) != 1)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8472 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8473 buf[0] = PyUnicode_AS_UNICODE(v)[0];
12899
9df91f91e5cc Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12876
diff changeset
8474 }
9df91f91e5cc Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12876
diff changeset
8475
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
8476 else if (PyString_Check(v)) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8477 if (PyString_GET_SIZE(v) != 1)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8478 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8479 buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0];
12899
9df91f91e5cc Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12876
diff changeset
8480 }
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8481
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8482 else {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8483 /* Integer input truncated to a character */
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8484 long x;
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8485 x = PyInt_AsLong(v);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8486 if (x == -1 && PyErr_Occurred())
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8487 goto onError;
24737
eaa87632b1e8 Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents: 24736
diff changeset
8488 #ifdef Py_UNICODE_WIDE
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8489 if (x < 0 || x > 0x10ffff) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8490 PyErr_SetString(PyExc_OverflowError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8491 "%c arg not in range(0x110000) "
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8492 "(wide Python build)");
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8493 return -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8494 }
24737
eaa87632b1e8 Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents: 24736
diff changeset
8495 #else
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8496 if (x < 0 || x > 0xffff) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8497 PyErr_SetString(PyExc_OverflowError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8498 "%c arg not in range(0x10000) "
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8499 "(narrow Python build)");
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8500 return -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8501 }
24737
eaa87632b1e8 Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents: 24736
diff changeset
8502 #endif
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8503 buf[0] = (Py_UNICODE) x;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8504 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8505 buf[1] = '\0';
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8506 return 1;
12899
9df91f91e5cc Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12876
diff changeset
8507
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8508 onError:
12899
9df91f91e5cc Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12876
diff changeset
8509 PyErr_SetString(PyExc_TypeError,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8510 "%c requires int or char");
12899
9df91f91e5cc Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12876
diff changeset
8511 return -1;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8512 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8513
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 13110
diff changeset
8514 /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...)
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 13110
diff changeset
8515
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 13110
diff changeset
8516 FORMATBUFLEN is the length of the buffer in which the floats, ints, &
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 13110
diff changeset
8517 chars are formatted. XXX This is a magic number. Each formatting
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 13110
diff changeset
8518 routine does bounds checking to ensure no overflow, but a better
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 13110
diff changeset
8519 solution may be to malloc a buffer of appropriate size for each
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 13110
diff changeset
8520 format. For now, the current solution is sufficient.
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 13110
diff changeset
8521 */
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 13110
diff changeset
8522 #define FORMATBUFLEN (size_t)120
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 13110
diff changeset
8523
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8524 PyObject *PyUnicode_Format(PyObject *format,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8525 PyObject *args)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8526 {
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8527 Py_UNICODE *fmt, *res;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
8528 Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8529 int args_owned = 0;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8530 PyUnicodeObject *result = NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8531 PyObject *dict = NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8532 PyObject *uformat;
30441
76e998aee644 On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents: 30439
diff changeset
8533
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8534 if (format == NULL || args == NULL) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8535 PyErr_BadInternalCall();
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8536 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8537 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8538 uformat = PyUnicode_FromObject(format);
12645
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
8539 if (uformat == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8540 return NULL;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8541 fmt = PyUnicode_AS_UNICODE(uformat);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8542 fmtcnt = PyUnicode_GET_SIZE(uformat);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8543
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8544 reslen = rescnt = fmtcnt + 100;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8545 result = _PyUnicode_New(reslen);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8546 if (result == NULL)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8547 goto onError;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8548 res = PyUnicode_AS_UNICODE(result);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8549
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8550 if (PyTuple_Check(args)) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8551 arglen = PyTuple_Size(args);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8552 argidx = 0;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8553 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8554 else {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8555 arglen = -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8556 argidx = -2;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8557 }
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43932
diff changeset
8558 if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) &&
26164
63a08639dae2 Fix SF # 635969, No error "not all arguments converted"
Neal Norwitz <nnorwitz@gmail.com>
parents: 25895
diff changeset
8559 !PyObject_TypeCheck(args, &PyBaseString_Type))
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8560 dict = args;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8561
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8562 while (--fmtcnt >= 0) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8563 if (*fmt != '%') {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8564 if (--rescnt < 0) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8565 rescnt = fmtcnt + 100;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8566 reslen += rescnt;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8567 if (_PyUnicode_Resize(&result, reslen) < 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8568 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8569 res = PyUnicode_AS_UNICODE(result) + reslen - rescnt;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8570 --rescnt;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8571 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8572 *res++ = *fmt++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8573 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8574 else {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8575 /* Got a format specifier */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8576 int flags = 0;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8577 Py_ssize_t width = -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8578 int prec = -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8579 Py_UNICODE c = '\0';
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8580 Py_UNICODE fill;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8581 int isnumok;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8582 PyObject *v = NULL;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8583 PyObject *temp = NULL;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8584 Py_UNICODE *pbuf;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8585 Py_UNICODE sign;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8586 Py_ssize_t len;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8587 Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8588
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8589 fmt++;
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8590 if (*fmt == '(') {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8591 Py_UNICODE *keystart;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8592 Py_ssize_t keylen;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8593 PyObject *key;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8594 int pcount = 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8595
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8596 if (dict == NULL) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8597 PyErr_SetString(PyExc_TypeError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8598 "format requires a mapping");
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8599 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8600 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8601 ++fmt;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8602 --fmtcnt;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8603 keystart = fmt;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8604 /* Skip over balanced parentheses */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8605 while (pcount > 0 && --fmtcnt >= 0) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8606 if (*fmt == ')')
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8607 --pcount;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8608 else if (*fmt == '(')
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8609 ++pcount;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8610 fmt++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8611 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8612 keylen = fmt - keystart - 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8613 if (fmtcnt < 0 || pcount > 0) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8614 PyErr_SetString(PyExc_ValueError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8615 "incomplete format key");
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8616 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8617 }
21028
3dfbbf42bf16 Fix for bug #438164: %-formatting using Unicode objects.
Marc-André Lemburg <mal@egenix.com>
parents: 20563
diff changeset
8618 #if 0
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8619 /* keys are converted to strings using UTF-8 and
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8620 then looked up since Python uses strings to hold
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8621 variables names etc. in its namespaces and we
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8622 wouldn't want to break common idioms. */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8623 key = PyUnicode_EncodeUTF8(keystart,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8624 keylen,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8625 NULL);
21028
3dfbbf42bf16 Fix for bug #438164: %-formatting using Unicode objects.
Marc-André Lemburg <mal@egenix.com>
parents: 20563
diff changeset
8626 #else
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8627 key = PyUnicode_FromUnicode(keystart, keylen);
21028
3dfbbf42bf16 Fix for bug #438164: %-formatting using Unicode objects.
Marc-André Lemburg <mal@egenix.com>
parents: 20563
diff changeset
8628 #endif
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8629 if (key == NULL)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8630 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8631 if (args_owned) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8632 Py_DECREF(args);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8633 args_owned = 0;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8634 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8635 args = PyObject_GetItem(dict, key);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8636 Py_DECREF(key);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8637 if (args == NULL) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8638 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8639 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8640 args_owned = 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8641 arglen = -1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8642 argidx = -2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8643 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8644 while (--fmtcnt >= 0) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8645 switch (c = *fmt++) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8646 case '-': flags |= F_LJUST; continue;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8647 case '+': flags |= F_SIGN; continue;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8648 case ' ': flags |= F_BLANK; continue;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8649 case '#': flags |= F_ALT; continue;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8650 case '0': flags |= F_ZERO; continue;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8651 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8652 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8653 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8654 if (c == '*') {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8655 v = getnextarg(args, arglen, &argidx);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8656 if (v == NULL)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8657 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8658 if (!PyInt_Check(v)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8659 PyErr_SetString(PyExc_TypeError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8660 "* wants int");
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8661 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8662 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8663 width = PyInt_AsLong(v);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8664 if (width < 0) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8665 flags |= F_LJUST;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8666 width = -width;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8667 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8668 if (--fmtcnt >= 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8669 c = *fmt++;
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8670 }
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8671 else if (c >= '0' && c <= '9') {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8672 width = c - '0';
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8673 while (--fmtcnt >= 0) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8674 c = *fmt++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8675 if (c < '0' || c > '9')
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8676 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8677 if ((width*10) / 10 != width) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8678 PyErr_SetString(PyExc_ValueError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8679 "width too big");
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8680 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8681 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8682 width = width*10 + (c - '0');
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8683 }
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8684 }
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8685 if (c == '.') {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8686 prec = 0;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8687 if (--fmtcnt >= 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8688 c = *fmt++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8689 if (c == '*') {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8690 v = getnextarg(args, arglen, &argidx);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8691 if (v == NULL)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8692 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8693 if (!PyInt_Check(v)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8694 PyErr_SetString(PyExc_TypeError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8695 "* wants int");
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8696 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8697 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8698 prec = PyInt_AsLong(v);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8699 if (prec < 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8700 prec = 0;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8701 if (--fmtcnt >= 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8702 c = *fmt++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8703 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8704 else if (c >= '0' && c <= '9') {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8705 prec = c - '0';
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8706 while (--fmtcnt >= 0) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8707 c = Py_CHARMASK(*fmt++);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8708 if (c < '0' || c > '9')
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8709 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8710 if ((prec*10) / 10 != prec) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8711 PyErr_SetString(PyExc_ValueError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8712 "prec too big");
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8713 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8714 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8715 prec = prec*10 + (c - '0');
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8716 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8717 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8718 } /* prec */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8719 if (fmtcnt >= 0) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8720 if (c == 'h' || c == 'l' || c == 'L') {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8721 if (--fmtcnt >= 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8722 c = *fmt++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8723 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8724 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8725 if (fmtcnt < 0) {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8726 PyErr_SetString(PyExc_ValueError,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8727 "incomplete format");
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8728 goto onError;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8729 }
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8730 if (c != '%') {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8731 v = getnextarg(args, arglen, &argidx);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8732 if (v == NULL)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8733 goto onError;
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8734 }
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8735 sign = 0;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8736 fill = ' ';
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8737 switch (c) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8738
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8739 case '%':
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8740 pbuf = formatbuf;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8741 /* presume that buffer length is at least 1 */
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8742 pbuf[0] = '%';
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8743 len = 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8744 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8745
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8746 case 's':
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8747 case 'r':
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8748 if (PyUnicode_Check(v) && c == 's') {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8749 temp = v;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8750 Py_INCREF(temp);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8751 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8752 else {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8753 PyObject *unicode;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8754 if (c == 's')
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8755 temp = PyObject_Unicode(v);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8756 else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8757 temp = PyObject_Repr(v);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8758 if (temp == NULL)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8759 goto onError;
32801
16de4f90ef44 Let u'%s' % obj try obj.__unicode__() first and fallback to obj.__str__().
Marc-André Lemburg <mal@egenix.com>
parents: 32695
diff changeset
8760 if (PyUnicode_Check(temp))
16de4f90ef44 Let u'%s' % obj try obj.__unicode__() first and fallback to obj.__str__().
Marc-André Lemburg <mal@egenix.com>
parents: 32695
diff changeset
8761 /* nothing to do */;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
8762 else if (PyString_Check(temp)) {
32801
16de4f90ef44 Let u'%s' % obj try obj.__unicode__() first and fallback to obj.__str__().
Marc-André Lemburg <mal@egenix.com>
parents: 32695
diff changeset
8763 /* convert to string to Unicode */
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8764 unicode = PyUnicode_Decode(PyString_AS_STRING(temp),
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8765 PyString_GET_SIZE(temp),
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8766 NULL,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8767 "strict");
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8768 Py_DECREF(temp);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8769 temp = unicode;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8770 if (temp == NULL)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8771 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8772 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8773 else {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8774 Py_DECREF(temp);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8775 PyErr_SetString(PyExc_TypeError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8776 "%s argument has non-string str()");
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8777 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8778 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8779 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8780 pbuf = PyUnicode_AS_UNICODE(temp);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8781 len = PyUnicode_GET_SIZE(temp);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8782 if (prec >= 0 && len > prec)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8783 len = prec;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8784 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8785
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8786 case 'i':
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8787 case 'd':
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8788 case 'u':
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8789 case 'o':
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8790 case 'x':
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8791 case 'X':
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8792 if (c == 'i')
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8793 c = 'd';
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8794 isnumok = 0;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8795 if (PyNumber_Check(v)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8796 PyObject *iobj=NULL;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8797
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8798 if (PyInt_Check(v) || (PyLong_Check(v))) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8799 iobj = v;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8800 Py_INCREF(iobj);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8801 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8802 else {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8803 iobj = PyNumber_Int(v);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8804 if (iobj==NULL) iobj = PyNumber_Long(v);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8805 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8806 if (iobj!=NULL) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8807 if (PyInt_Check(iobj)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8808 isnumok = 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8809 pbuf = formatbuf;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8810 len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE),
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8811 flags, prec, c, iobj);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8812 Py_DECREF(iobj);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8813 if (len < 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8814 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8815 sign = 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8816 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8817 else if (PyLong_Check(iobj)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8818 isnumok = 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8819 temp = formatlong(iobj, flags, prec, c);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8820 Py_DECREF(iobj);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8821 if (!temp)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8822 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8823 pbuf = PyUnicode_AS_UNICODE(temp);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8824 len = PyUnicode_GET_SIZE(temp);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8825 sign = 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8826 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8827 else {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8828 Py_DECREF(iobj);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8829 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8830 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8831 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8832 if (!isnumok) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8833 PyErr_Format(PyExc_TypeError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8834 "%%%c format: a number is required, "
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8835 "not %.200s", (char)c, Py_TYPE(v)->tp_name);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8836 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8837 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8838 if (flags & F_ZERO)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8839 fill = '0';
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8840 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8841
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8842 case 'e':
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8843 case 'E':
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8844 case 'f':
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8845 case 'F':
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8846 case 'g':
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8847 case 'G':
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8848 if (c == 'F')
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8849 c = 'f';
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8850 pbuf = formatbuf;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8851 len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE),
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8852 flags, prec, c, v);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8853 if (len < 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8854 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8855 sign = 1;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8856 if (flags & F_ZERO)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8857 fill = '0';
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8858 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8859
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8860 case 'c':
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8861 pbuf = formatbuf;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8862 len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8863 if (len < 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8864 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8865 break;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8866
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8867 default:
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8868 PyErr_Format(PyExc_ValueError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8869 "unsupported format character '%c' (0x%x) "
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8870 "at index %zd",
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8871 (31<=c && c<=126) ? (char)c : '?',
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8872 (int)c,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8873 (Py_ssize_t)(fmt - 1 -
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8874 PyUnicode_AS_UNICODE(uformat)));
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8875 goto onError;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8876 }
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8877 if (sign) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8878 if (*pbuf == '-' || *pbuf == '+') {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8879 sign = *pbuf++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8880 len--;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8881 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8882 else if (flags & F_SIGN)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8883 sign = '+';
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8884 else if (flags & F_BLANK)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8885 sign = ' ';
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8886 else
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8887 sign = 0;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8888 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8889 if (width < len)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8890 width = len;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8891 if (rescnt - (sign != 0) < width) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8892 reslen -= rescnt;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8893 rescnt = width + fmtcnt + 100;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8894 reslen += rescnt;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8895 if (reslen < 0) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8896 Py_XDECREF(temp);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8897 PyErr_NoMemory();
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8898 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8899 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8900 if (_PyUnicode_Resize(&result, reslen) < 0) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8901 Py_XDECREF(temp);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8902 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8903 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8904 res = PyUnicode_AS_UNICODE(result)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8905 + reslen - rescnt;
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8906 }
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8907 if (sign) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8908 if (fill != ' ')
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8909 *res++ = sign;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8910 rescnt--;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8911 if (width > len)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8912 width--;
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8913 }
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8914 if ((flags & F_ALT) && (c == 'x' || c == 'X')) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8915 assert(pbuf[0] == '0');
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8916 assert(pbuf[1] == c);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8917 if (fill != ' ') {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8918 *res++ = *pbuf++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8919 *res++ = *pbuf++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8920 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8921 rescnt -= 2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8922 width -= 2;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8923 if (width < 0)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8924 width = 0;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8925 len -= 2;
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8926 }
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8927 if (width > len && !(flags & F_LJUST)) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8928 do {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8929 --rescnt;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8930 *res++ = fill;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8931 } while (--width > len);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8932 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8933 if (fill == ' ') {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8934 if (sign)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8935 *res++ = sign;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8936 if ((flags & F_ALT) && (c == 'x' || c == 'X')) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8937 assert(pbuf[0] == '0');
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8938 assert(pbuf[1] == c);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8939 *res++ = *pbuf++;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8940 *res++ = *pbuf++;
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8941 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8942 }
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8943 Py_UNICODE_COPY(res, pbuf, len);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8944 res += len;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8945 rescnt -= len;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8946 while (--width >= len) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8947 --rescnt;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8948 *res++ = ' ';
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8949 }
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8950 if (dict && (argidx < arglen) && c != '%') {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8951 PyErr_SetString(PyExc_TypeError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8952 "not all arguments converted during string formatting");
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8953 Py_XDECREF(temp);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8954 goto onError;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8955 }
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8956 Py_XDECREF(temp);
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8957 } /* '%' */
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8958 } /* until end */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8959 if (argidx < arglen && !dict) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8960 PyErr_SetString(PyExc_TypeError,
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8961 "not all arguments converted during string formatting");
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8962 goto onError;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8963 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8964
36945
0f19a6fc9756 - Reindent a confusingly indented piece of code (no intended code changes
Thomas Wouters <thomas@python.org>
parents: 36924
diff changeset
8965 if (_PyUnicode_Resize(&result, reslen - rescnt) < 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8966 goto onError;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8967 if (args_owned) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8968 Py_DECREF(args);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8969 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8970 Py_DECREF(uformat);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8971 return (PyObject *)result;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8972
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8973 onError:
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8974 Py_XDECREF(result);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8975 Py_DECREF(uformat);
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8976 if (args_owned) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8977 Py_DECREF(args);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8978 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8979 return NULL;
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8980 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8981
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8982 static PyBufferProcs unicode_as_buffer = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
8983 (readbufferproc) unicode_buffer_getreadbuf,
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
8984 (writebufferproc) unicode_buffer_getwritebuf,
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
8985 (segcountproc) unicode_buffer_getsegcount,
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36261
diff changeset
8986 (charbufferproc) unicode_buffer_getcharbuf,
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8987 };
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8988
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24071
diff changeset
8989 static PyObject *
19593
a80b4b006eb7 Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
8990 unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
a80b4b006eb7 Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
8991
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
8992 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
8993 unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
8994 {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
8995 PyObject *x = NULL;
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8996 static char *kwlist[] = {"string", "encoding", "errors", 0};
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8997 char *encoding = NULL;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8998 char *errors = NULL;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
8999
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9000 if (type != &PyUnicode_Type)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9001 return unicode_subtype_new(type, args, kwds);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9002 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode",
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
9003 kwlist, &x, &encoding, &errors))
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9004 return NULL;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9005 if (x == NULL)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9006 return (PyObject *)_PyUnicode_New(0);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9007 if (encoding == NULL && errors == NULL)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9008 return PyObject_Unicode(x);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9009 else
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
9010 return PyUnicode_FromEncodedObject(x, encoding, errors);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
9011 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
9012
19593
a80b4b006eb7 Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
9013 static PyObject *
a80b4b006eb7 Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
9014 unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
a80b4b006eb7 Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
9015 {
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9016 PyUnicodeObject *tmp, *pnew;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9017 Py_ssize_t n;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9018
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9019 assert(PyType_IsSubtype(type, &PyUnicode_Type));
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9020 tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9021 if (tmp == NULL)
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9022 return NULL;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9023 assert(PyUnicode_Check(tmp));
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9024 pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9025 if (pnew == NULL) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9026 Py_DECREF(tmp);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9027 return NULL;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9028 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9029 pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1));
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9030 if (pnew->str == NULL) {
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9031 _Py_ForgetReference((PyObject *)pnew);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9032 PyObject_Del(pnew);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9033 Py_DECREF(tmp);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9034 return PyErr_NoMemory();
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9035 }
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9036 Py_UNICODE_COPY(pnew->str, tmp->str, n+1);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9037 pnew->length = n;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9038 pnew->hash = tmp->hash;
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9039 Py_DECREF(tmp);
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9040 return (PyObject *)pnew;
19593
a80b4b006eb7 Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
9041 }
a80b4b006eb7 Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents: 19301
diff changeset
9042
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
9043 PyDoc_STRVAR(unicode_doc,
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
9044 "unicode(string [, encoding[, errors]]) -> object\n\
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
9045 \n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
9046 Create a new Unicode object from the given encoded string.\n\
24392
0a327b768868 tighten up the unicode object's docstring a tad
Skip Montanaro <skip@pobox.com>
parents: 24280
diff changeset
9047 encoding defaults to the current default string encoding.\n\
0a327b768868 tighten up the unicode object's docstring a tad
Skip Montanaro <skip@pobox.com>
parents: 24280
diff changeset
9048 errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'.");
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
9049
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
9050 PyTypeObject PyUnicode_Type = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 42216
diff changeset
9051 PyVarObject_HEAD_INIT(&PyType_Type, 0)
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9052 "unicode", /* tp_name */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9053 sizeof(PyUnicodeObject), /* tp_size */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9054 0, /* tp_itemsize */
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
9055 /* Slots */
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9056 (destructor)unicode_dealloc, /* tp_dealloc */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9057 0, /* tp_print */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9058 0, /* tp_getattr */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9059 0, /* tp_setattr */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9060 0, /* tp_compare */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9061 unicode_repr, /* tp_repr */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9062 &unicode_as_number, /* tp_as_number */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9063 &unicode_as_sequence, /* tp_as_sequence */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9064 &unicode_as_mapping, /* tp_as_mapping */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9065 (hashfunc) unicode_hash, /* tp_hash*/
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9066 0, /* tp_call*/
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9067 (reprfunc) unicode_str, /* tp_str */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9068 PyObject_GenericGetAttr, /* tp_getattro */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9069 0, /* tp_setattro */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9070 &unicode_as_buffer, /* tp_as_buffer */
26274
958d07085dcf Add nb_remainder (i.e. __mod__) slot to unicode type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
9071 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
9072 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */
51204
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9073 unicode_doc, /* tp_doc */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9074 0, /* tp_traverse */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9075 0, /* tp_clear */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9076 PyUnicode_RichCompare, /* tp_richcompare */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9077 0, /* tp_weaklistoffset */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9078 0, /* tp_iter */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9079 0, /* tp_iternext */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9080 unicode_methods, /* tp_methods */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9081 0, /* tp_members */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9082 0, /* tp_getset */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9083 &PyBaseString_Type, /* tp_base */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9084 0, /* tp_dict */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9085 0, /* tp_descr_get */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9086 0, /* tp_descr_set */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9087 0, /* tp_dictoffset */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9088 0, /* tp_init */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9089 0, /* tp_alloc */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9090 unicode_new, /* tp_new */
dcfd436567ce expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 50305
diff changeset
9091 PyObject_Del, /* tp_free */
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
9092 };
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
9093
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
9094 /* Initialize the Unicode implementation */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
9095
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13635
diff changeset
9096 void _PyUnicode_Init(void)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
9097 {
17618
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
9098 int i;
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
9099
38257
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
9100 /* XXX - move this array to unicodectype.c ? */
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
9101 Py_UNICODE linebreak[] = {
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
9102 0x000A, /* LINE FEED */
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
9103 0x000D, /* CARRIAGE RETURN */
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
9104 0x001C, /* FILE SEPARATOR */
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
9105 0x001D, /* GROUP SEPARATOR */
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
9106 0x001E, /* RECORD SEPARATOR */
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
9107 0x0085, /* NEXT LINE */
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
9108 0x2028, /* LINE SEPARATOR */
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
9109 0x2029, /* PARAGRAPH SEPARATOR */
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
9110 };
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
9111
12645
c4eb70d24a87 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12629
diff changeset
9112 /* Init the implementation */
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 45053
diff changeset
9113 free_list = NULL;
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 45053
diff changeset
9114 numfree = 0;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
9115 unicode_empty = _PyUnicode_New(0);
39129
f2031f15d99d Handle allocation failures gracefully. Found with failmalloc.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38817
diff changeset
9116 if (!unicode_empty)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
9117 return;
39129
f2031f15d99d Handle allocation failures gracefully. Found with failmalloc.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38817
diff changeset
9118
12876
243593f11fcf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12646
diff changeset
9119 strcpy(unicode_default_encoding, "ascii");
17618
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
9120 for (i = 0; i < 256; i++)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
9121 unicode_latin1[i] = NULL;
23542
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
9122 if (PyType_Ready(&PyUnicode_Type) < 0)
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
9123 Py_FatalError("Can't initialize 'unicode'");
38257
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
9124
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
9125 /* initialize the linebreak bloom filter */
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
9126 bloom_linebreak = make_bloom_mask(
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
9127 linebreak, sizeof(linebreak) / sizeof(linebreak[0])
af46c7ed05eb needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents: 38243
diff changeset
9128 );
38786
9c52efcaff65 Initialize the type object so pychecker can't crash the interpreter.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38709
diff changeset
9129
9c52efcaff65 Initialize the type object so pychecker can't crash the interpreter.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38709
diff changeset
9130 PyType_Ready(&EncodingMapType);
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
9131 }
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
9132
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
9133 /* Finalize the Unicode implementation */
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
9134
45341
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
9135 int
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
9136 PyUnicode_ClearFreeList(void)
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
9137 {
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
9138 int freelist_size = numfree;
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
9139 PyUnicodeObject *u;
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
9140
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
9141 for (u = free_list; u != NULL;) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
9142 PyUnicodeObject *v = u;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
9143 u = *(PyUnicodeObject **)u;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
9144 if (v->str)
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
9145 PyObject_DEL(v->str);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
9146 Py_XDECREF(v->defenc);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
9147 PyObject_Del(v);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
9148 numfree--;
45341
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
9149 }
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
9150 free_list = NULL;
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
9151 assert(numfree == 0);
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
9152 return freelist_size;
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
9153 }
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
9154
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
9155 void
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13635
diff changeset
9156 _PyUnicode_Fini(void)
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
9157 {
17618
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
9158 int i;
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
9159
14998
c16bdf555c79 In _PyUnicode_Fini(), decref unicode_empty before tearng down the free
Guido van Rossum <guido@python.org>
parents: 14869
diff changeset
9160 Py_XDECREF(unicode_empty);
c16bdf555c79 In _PyUnicode_Fini(), decref unicode_empty before tearng down the free
Guido van Rossum <guido@python.org>
parents: 14869
diff changeset
9161 unicode_empty = NULL;
15001
a349b89bf10e _PyUnicode_Fini(): Initialize the local freelist walking variable `u'
Barry Warsaw <barry@python.org>
parents: 14998
diff changeset
9162
17618
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
9163 for (i = 0; i < 256; i++) {
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
9164 if (unicode_latin1[i]) {
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
9165 Py_DECREF(unicode_latin1[i]);
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
9166 unicode_latin1[i] = NULL;
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
9167 }
17618
b44ba7c01217 This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents: 17600
diff changeset
9168 }
45341
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
9169 (void)PyUnicode_ClearFreeList();
11968
13bc68faeb65 Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff changeset
9170 }
29164
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28575
diff changeset
9171
37655
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37596
diff changeset
9172 #ifdef __cplusplus
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37596
diff changeset
9173 }
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37596
diff changeset
9174 #endif
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37596
diff changeset
9175
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37596
diff changeset
9176
29164
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28575
diff changeset
9177 /*
51213
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
9178 Local variables:
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
9179 c-basic-offset: 4
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
9180 indent-tabs-mode: nil
cd5db43dbb65 fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents: 51212
diff changeset
9181 End:
29164
4d9017ef7b5f Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents: 28575
diff changeset
9182 */