Mercurial > cpython-lru_cache-weakref
annotate Objects/unicodeobject.c @ 53364:00a927815dcd 2.6
Merged revisions 71847 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r71847 | thomas.heller | 2009-04-24 22:00:41 +0200 (Fr, 24 Apr 2009) | 2 lines
Issue 5041: ctypes unwilling to allow pickling wide character.
........
| author | Thomas Heller <theller@ctypes.org> |
|---|---|
| date | Fri, 24 Apr 2009 20:18:22 +0000 |
| parents | f3b26a729e82 |
| children | 18a5ae96ae5c |
| 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 |
| 51216 | 15 Copyright (c) 1999 by Secret Labs AB |
| 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 | 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 | 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 | 68 Note: This is an experimental feature ! If you get core dumps when |
| 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 | 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 |
| 51216 | 199 #define BLOOM_LINEBREAK(ch) \ |
| 200 ((ch) < 128U ? ascii_linebreak[(ch)] : \ | |
| 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 |
| 51216 | 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, |
| 51216 | 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 | 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) |
| 51216 | 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 || |
| 51216 | 248 (unicode->length == 1 && |
| 249 unicode->str[0] < 256U && | |
| 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 | 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, |
| 51216 | 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) { |
| 51216 | 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 |
| 51216 | 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 |
| 51216 | 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 | 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--; |
| 51216 | 312 if (unicode->str) { |
| 313 /* Keep-Alive optimization: we only upsize the buffer, | |
| 314 never downsize it. */ | |
| 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) { |
| 51216 | 317 PyObject_DEL(unicode->str); |
| 318 unicode->str = NULL; | |
| 319 } | |
| 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 { |
| 51216 | 322 size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 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 { |
| 51216 | 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; |
| 51216 | 332 new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 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 | 336 if (!unicode->str) { |
| 51216 | 337 PyErr_NoMemory(); |
| 338 goto onError; | |
| 12511 | 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 |
| 51216 | 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) && |
| 51216 | 366 numfree < PyUnicode_MAXFREELIST) { |
| 12329 | 367 /* Keep-Alive optimization */ |
| 51216 | 368 if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
| 369 PyObject_DEL(unicode->str); | |
| 370 unicode->str = NULL; | |
| 371 unicode->length = 0; | |
| 372 } | |
| 373 if (unicode->defenc) { | |
| 374 Py_DECREF(unicode->defenc); | |
| 375 unicode->defenc = NULL; | |
| 376 } | |
| 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 { |
| 51216 | 383 PyObject_DEL(unicode->str); |
| 384 Py_XDECREF(unicode->defenc); | |
| 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) { |
| 51216 | 396 PyErr_BadInternalCall(); |
| 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) { |
| 51216 | 401 PyErr_BadInternalCall(); |
| 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 && |
| 51216 | 409 (v == unicode_empty || v->length == 1)) { |
| 410 PyUnicodeObject *w = _PyUnicode_New(length); | |
| 411 if (w == NULL) | |
| 412 return -1; | |
| 413 Py_UNICODE_COPY(w->str, v->str, | |
| 414 length < v->length ? length : v->length); | |
| 415 Py_DECREF(*unicode); | |
| 416 *unicode = w; | |
| 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, |
| 51216 | 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 |
| 51216 | 439 /* Optimization for empty strings */ |
| 440 if (size == 0 && unicode_empty != NULL) { | |
| 441 Py_INCREF(unicode_empty); | |
| 442 return (PyObject *)unicode_empty; | |
| 443 } | |
| 444 | |
| 445 /* Single character Unicode objects in the Latin-1 range are | |
| 446 shared when using this constructor */ | |
| 447 if (size == 1 && *u < 256) { | |
| 448 unicode = unicode_latin1[*u]; | |
| 449 if (!unicode) { | |
| 450 unicode = _PyUnicode_New(1); | |
| 451 if (!unicode) | |
| 452 return NULL; | |
| 453 unicode->str[0] = *u; | |
| 454 unicode_latin1[*u] = unicode; | |
| 455 } | |
| 456 Py_INCREF(unicode); | |
| 457 return (PyObject *)unicode; | |
| 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) |
| 51216 | 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, |
| 51216 | 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 |
| 51216 | 488 /* Optimization for empty strings */ |
| 489 if (size == 0 && unicode_empty != NULL) { | |
| 490 Py_INCREF(unicode_empty); | |
| 491 return (PyObject *)unicode_empty; | |
| 492 } | |
| 493 | |
| 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. */ |
| 51216 | 496 if (size == 1 && Py_CHARMASK(*u) < 128) { |
| 497 unicode = unicode_latin1[Py_CHARMASK(*u)]; | |
| 498 if (!unicode) { | |
| 499 unicode = _PyUnicode_New(1); | |
| 500 if (!unicode) | |
| 501 return NULL; | |
| 502 unicode->str[0] = Py_CHARMASK(*u); | |
| 503 unicode_latin1[Py_CHARMASK(*u)] = unicode; | |
| 504 } | |
| 505 Py_INCREF(unicode); | |
| 506 return (PyObject *)unicode; | |
| 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 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
532 PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
| 51216 | 533 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
|
534 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
535 PyUnicodeObject *unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
536 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
537 if (w == NULL) { |
| 51216 | 538 PyErr_BadInternalCall(); |
| 539 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
540 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
541 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
542 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
|
543 if (!unicode) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
544 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
545 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
546 /* 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
|
547 #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
|
548 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
|
549 #else |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
550 { |
| 51216 | 551 register Py_UNICODE *u; |
| 552 register Py_ssize_t i; | |
| 553 u = PyUnicode_AS_UNICODE(unicode); | |
| 554 for (i = size; i > 0; i--) | |
| 555 *u++ = *w++; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
556 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
557 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
558 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
559 return (PyObject *)unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
560 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
561 |
|
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
|
562 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
|
563 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
|
564 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
565 *fmt++ = '%'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
566 if (width) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
567 if (zeropad) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
568 *fmt++ = '0'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
569 fmt += sprintf(fmt, "%d", width); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
570 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
571 if (precision) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
572 fmt += sprintf(fmt, ".%d", precision); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
573 if (longflag) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
574 *fmt++ = 'l'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
575 else if (size_tflag) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
576 char *f = PY_FORMAT_SIZE_T; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
577 while (*f) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
578 *fmt++ = *f++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
579 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
580 *fmt++ = c; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
581 *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
|
582 } |
|
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
|
583 |
|
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
|
584 #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
|
585 |
|
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
|
586 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
|
587 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
|
588 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
589 va_list count; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
590 Py_ssize_t callcount = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
591 PyObject **callresults = NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
592 PyObject **callresult = NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
593 Py_ssize_t n = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
594 int width = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
595 int precision = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
596 int zeropad; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
597 const char* f; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
598 Py_UNICODE *s; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
599 PyObject *string; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
600 /* used by sprintf */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
601 char buffer[21]; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
602 /* 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
|
603 * (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
|
604 char *abuffer = NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
605 char *realbuffer; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
606 Py_ssize_t abuffersize = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
607 char fmt[60]; /* should be enough for %0width.precisionld */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
608 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
|
609 |
|
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
|
610 #ifdef VA_LIST_IS_ARRAY |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
611 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
|
612 #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
|
613 #ifdef __va_copy |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
614 __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
|
615 #else |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
616 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
|
617 #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
|
618 #endif |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
619 /* step 1: count the number of %S/%R format specifications |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
620 * (we call PyObject_Str()/PyObject_Repr() for these objects |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
621 * once during step 3 and put the result in an array) */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
622 for (f = format; *f; f++) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
623 if (*f == '%' && (*(f+1)=='S' || *(f+1)=='R')) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
624 ++callcount; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
625 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
626 /* step 2: allocate memory for the results of |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
627 * PyObject_Str()/PyObject_Repr() calls */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
628 if (callcount) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
629 callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
630 if (!callresults) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
631 PyErr_NoMemory(); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
632 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
633 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
634 callresult = callresults; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
635 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
636 /* 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
|
637 for (f = format; *f; f++) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
638 if (*f == '%') { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
639 const char* p = f; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
640 width = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
641 while (isdigit((unsigned)*f)) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
642 width = (width*10) + *f++ - '0'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
643 while (*++f && *f != '%' && !isalpha((unsigned)*f)) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
644 ; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
645 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
646 /* 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
|
647 * 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
|
648 */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
649 if ((*f == 'l' || *f == 'z') && |
| 51216 | 650 (f[1] == 'd' || f[1] == 'u')) |
| 651 ++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
|
652 |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
653 switch (*f) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
654 case 'c': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
655 (void)va_arg(count, int); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
656 /* fall through... */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
657 case '%': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
658 n++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
659 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
660 case 'd': case 'u': case 'i': case 'x': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
661 (void) va_arg(count, int); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
662 /* 20 bytes is enough to hold a 64-bit |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
663 integer. Decimal takes the most space. |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
664 This isn't enough for octal. |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
665 If a width is specified we need more |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
666 (which we allocate later). */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
667 if (width < 20) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
668 width = 20; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
669 n += width; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
670 if (abuffersize < width) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
671 abuffersize = width; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
672 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
673 case 's': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
674 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
675 /* UTF-8 */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
676 unsigned char*s; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
677 s = va_arg(count, unsigned char*); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
678 while (*s) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
679 if (*s < 128) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
680 n++; s++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
681 } else if (*s < 0xc0) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
682 /* invalid UTF-8 */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
683 n++; s++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
684 } else if (*s < 0xc0) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
685 n++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
686 s++; if(!*s)break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
687 s++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
688 } else if (*s < 0xe0) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
689 n++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
690 s++; if(!*s)break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
691 s++; if(!*s)break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
692 s++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
693 } else { |
| 51216 | 694 #ifdef Py_UNICODE_WIDE |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
695 n++; |
| 51216 | 696 #else |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
697 n+=2; |
| 51216 | 698 #endif |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
699 s++; if(!*s)break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
700 s++; if(!*s)break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
701 s++; if(!*s)break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
702 s++; |
|
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 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
705 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
706 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
707 case 'U': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
708 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
709 PyObject *obj = va_arg(count, PyObject *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
710 assert(obj && PyUnicode_Check(obj)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
711 n += PyUnicode_GET_SIZE(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
712 break; |
|
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 case 'V': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
715 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
716 PyObject *obj = va_arg(count, PyObject *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
717 const char *str = va_arg(count, const char *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
718 assert(obj || str); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
719 assert(!obj || PyUnicode_Check(obj)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
720 if (obj) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
721 n += PyUnicode_GET_SIZE(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
722 else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
723 n += strlen(str); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
724 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
725 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
726 case 'S': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
727 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
728 PyObject *obj = va_arg(count, PyObject *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
729 PyObject *str; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
730 assert(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
731 str = PyObject_Str(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
732 if (!str) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
733 goto fail; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
734 n += PyUnicode_GET_SIZE(str); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
735 /* Remember the str and switch to the next slot */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
736 *callresult++ = str; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
737 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
738 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
739 case 'R': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
740 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
741 PyObject *obj = va_arg(count, PyObject *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
742 PyObject *repr; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
743 assert(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
744 repr = PyObject_Repr(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
745 if (!repr) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
746 goto fail; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
747 n += PyUnicode_GET_SIZE(repr); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
748 /* Remember the repr and switch to the next slot */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
749 *callresult++ = repr; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
750 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
751 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
752 case 'p': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
753 (void) va_arg(count, int); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
754 /* maximum 64-bit pointer representation: |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
755 * 0xffffffffffffffff |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
756 * so 19 characters is enough. |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
757 * XXX I count 18 -- what's the extra for? |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
758 */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
759 n += 19; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
760 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
761 default: |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
762 /* if we stumble upon an unknown |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
763 formatting code, copy the rest of |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
764 the format string to the output |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
765 string. (we cannot just skip the |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
766 code, since there's no way to know |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
767 what's in the argument list) */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
768 n += strlen(p); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
769 goto expand; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
770 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
771 } else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
772 n++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
773 } |
| 51216 | 774 expand: |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
775 if (abuffersize > 20) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
776 abuffer = PyObject_Malloc(abuffersize); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
777 if (!abuffer) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
778 PyErr_NoMemory(); |
|
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 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
781 realbuffer = abuffer; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
782 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
783 else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
784 realbuffer = buffer; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
785 /* step 4: fill the buffer */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
786 /* 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
|
787 we don't have to resize the string. |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
788 There can be no errors beyond this point. */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
789 string = PyUnicode_FromUnicode(NULL, n); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
790 if (!string) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
791 goto fail; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
792 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
793 s = PyUnicode_AS_UNICODE(string); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
794 callresult = callresults; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
795 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
796 for (f = format; *f; f++) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
797 if (*f == '%') { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
798 const char* p = f++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
799 int longflag = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
800 int size_tflag = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
801 zeropad = (*f == '0'); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
802 /* parse the width.precision part */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
803 width = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
804 while (isdigit((unsigned)*f)) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
805 width = (width*10) + *f++ - '0'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
806 precision = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
807 if (*f == '.') { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
808 f++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
809 while (isdigit((unsigned)*f)) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
810 precision = (precision*10) + *f++ - '0'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
811 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
812 /* 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
|
813 others can be added when necessary. */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
814 if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
815 longflag = 1; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
816 ++f; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
817 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
818 /* handle the size_t flag. */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
819 if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
820 size_tflag = 1; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
821 ++f; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
822 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
823 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
824 switch (*f) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
825 case 'c': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
826 *s++ = va_arg(vargs, int); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
827 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
828 case 'd': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
829 makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd'); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
830 if (longflag) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
831 sprintf(realbuffer, fmt, va_arg(vargs, long)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
832 else if (size_tflag) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
833 sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
834 else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
835 sprintf(realbuffer, fmt, va_arg(vargs, int)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
836 appendstring(realbuffer); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
837 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
838 case 'u': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
839 makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u'); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
840 if (longflag) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
841 sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
842 else if (size_tflag) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
843 sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
844 else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
845 sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
846 appendstring(realbuffer); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
847 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
848 case 'i': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
849 makefmt(fmt, 0, 0, zeropad, width, precision, 'i'); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
850 sprintf(realbuffer, fmt, va_arg(vargs, int)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
851 appendstring(realbuffer); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
852 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
853 case 'x': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
854 makefmt(fmt, 0, 0, zeropad, width, precision, 'x'); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
855 sprintf(realbuffer, fmt, va_arg(vargs, int)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
856 appendstring(realbuffer); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
857 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
858 case 's': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
859 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
860 /* Parameter must be UTF-8 encoded. |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
861 In case of encoding errors, use |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
862 the replacement character. */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
863 PyObject *u; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
864 p = va_arg(vargs, char*); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
865 u = PyUnicode_DecodeUTF8(p, strlen(p), |
| 51216 | 866 "replace"); |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
867 if (!u) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
868 goto fail; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
869 Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(u), |
| 51216 | 870 PyUnicode_GET_SIZE(u)); |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
871 s += PyUnicode_GET_SIZE(u); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
872 Py_DECREF(u); |
|
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 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
875 case 'U': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
876 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
877 PyObject *obj = va_arg(vargs, PyObject *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
878 Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
879 Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
880 s += size; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
881 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
882 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
883 case 'V': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
884 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
885 PyObject *obj = va_arg(vargs, PyObject *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
886 const char *str = va_arg(vargs, const char *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
887 if (obj) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
888 Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
889 Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
890 s += size; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
891 } else { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
892 appendstring(str); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
893 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
894 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
895 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
896 case 'S': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
897 case 'R': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
898 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
899 Py_UNICODE *ucopy; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
900 Py_ssize_t usize; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
901 Py_ssize_t upos; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
902 /* unused, since we already have the result */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
903 (void) va_arg(vargs, PyObject *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
904 ucopy = PyUnicode_AS_UNICODE(*callresult); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
905 usize = PyUnicode_GET_SIZE(*callresult); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
906 for (upos = 0; upos<usize;) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
907 *s++ = ucopy[upos++]; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
908 /* We're done with the unicode()/repr() => forget it */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
909 Py_DECREF(*callresult); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
910 /* switch to next unicode()/repr() result */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
911 ++callresult; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
912 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
913 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
914 case 'p': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
915 sprintf(buffer, "%p", va_arg(vargs, void*)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
916 /* %p is ill-defined: ensure leading 0x. */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
917 if (buffer[1] == 'X') |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
918 buffer[1] = 'x'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
919 else if (buffer[1] != 'x') { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
920 memmove(buffer+2, buffer, strlen(buffer)+1); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
921 buffer[0] = '0'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
922 buffer[1] = 'x'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
923 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
924 appendstring(buffer); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
925 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
926 case '%': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
927 *s++ = '%'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
928 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
929 default: |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
930 appendstring(p); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
931 goto end; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
932 } |
|
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 *s++ = *f; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
935 } |
|
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
|
936 |
| 51216 | 937 end: |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
938 if (callresults) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
939 PyObject_Free(callresults); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
940 if (abuffer) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
941 PyObject_Free(abuffer); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
942 PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
943 return string; |
| 51216 | 944 fail: |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
945 if (callresults) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
946 PyObject **callresult2 = callresults; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
947 while (callresult2 < callresult) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
948 Py_DECREF(*callresult2); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
949 ++callresult2; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
950 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
951 PyObject_Free(callresults); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
952 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
953 if (abuffer) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
954 PyObject_Free(abuffer); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
955 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
|
956 } |
|
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
|
957 |
|
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
|
958 #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
|
959 |
|
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
|
960 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
|
961 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
|
962 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
963 PyObject* ret; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
964 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
|
965 |
|
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
|
966 #ifdef HAVE_STDARG_PROTOTYPES |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
967 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
|
968 #else |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
969 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
|
970 #endif |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
971 ret = PyUnicode_FromFormatV(format, vargs); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
972 va_end(vargs); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
973 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
|
974 } |
|
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
|
975 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
976 Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 51216 | 977 wchar_t *w, |
| 978 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
|
979 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
980 if (unicode == NULL) { |
| 51216 | 981 PyErr_BadInternalCall(); |
| 982 return -1; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
983 } |
|
34016
264e45baacd5
Correct the handling of 0-termination of PyUnicode_AsWideChar()
Marc-André Lemburg <mal@egenix.com>
parents:
33766
diff
changeset
|
984 |
|
264e45baacd5
Correct the handling of 0-termination of PyUnicode_AsWideChar()
Marc-André Lemburg <mal@egenix.com>
parents:
33766
diff
changeset
|
985 /* 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
|
986 if (size > PyUnicode_GET_SIZE(unicode)) |
| 51216 | 987 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
|
988 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
989 #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
|
990 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
|
991 #else |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
992 { |
| 51216 | 993 register Py_UNICODE *u; |
| 994 register Py_ssize_t i; | |
| 995 u = PyUnicode_AS_UNICODE(unicode); | |
| 996 for (i = size; i > 0; i--) | |
| 997 *w++ = *u++; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
998 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
999 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1000 |
|
34016
264e45baacd5
Correct the handling of 0-termination of PyUnicode_AsWideChar()
Marc-André Lemburg <mal@egenix.com>
parents:
33766
diff
changeset
|
1001 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
|
1002 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
|
1003 else |
| 51216 | 1004 return size; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1005 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1006 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1007 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1008 |
|
24737
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1009 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
|
1010 { |
|
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
|
1011 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
|
1012 |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1013 #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
|
1014 if (ordinal < 0 || ordinal > 0x10ffff) { |
| 51216 | 1015 PyErr_SetString(PyExc_ValueError, |
| 1016 "unichr() arg not in range(0x110000) " | |
| 1017 "(wide Python build)"); | |
| 1018 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
|
1019 } |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1020 #else |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1021 if (ordinal < 0 || ordinal > 0xffff) { |
| 51216 | 1022 PyErr_SetString(PyExc_ValueError, |
| 1023 "unichr() arg not in range(0x10000) " | |
| 1024 "(narrow Python build)"); | |
| 1025 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
|
1026 } |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1027 #endif |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1028 |
|
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
|
1029 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
|
1030 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
|
1031 } |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1032 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1033 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
|
1034 { |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1035 /* XXX Perhaps we should make this API an alias of |
| 51216 | 1036 PyObject_Unicode() instead ?! */ |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1037 if (PyUnicode_CheckExact(obj)) { |
| 51216 | 1038 Py_INCREF(obj); |
| 1039 return obj; | |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1040 } |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1041 if (PyUnicode_Check(obj)) { |
| 51216 | 1042 /* For a Unicode subtype that's not a Unicode object, |
| 1043 return a true Unicode object with the same data. */ | |
| 1044 return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), | |
| 1045 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
|
1046 } |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1047 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
|
1048 } |
|
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1049 |
|
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1050 PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 51216 | 1051 const char *encoding, |
| 1052 const char *errors) | |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1053 { |
|
19999
1f4e2627b627
Implement the changes proposed in patch #413333. unicode(obj) now
Marc-André Lemburg <mal@egenix.com>
parents:
19998
diff
changeset
|
1054 const char *s = NULL; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1055 Py_ssize_t len; |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1056 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
|
1057 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1058 if (obj == NULL) { |
| 51216 | 1059 PyErr_BadInternalCall(); |
| 1060 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1061 } |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1062 |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1063 #if 0 |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1064 /* 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
|
1065 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
|
1066 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
|
1067 Unicode subclasses. |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1068 |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1069 NOTE: This API should really only be used for object which |
| 51216 | 1070 represent *encoded* Unicode ! |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1071 |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1072 */ |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1073 if (PyUnicode_Check(obj)) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1074 if (encoding) { |
| 51216 | 1075 PyErr_SetString(PyExc_TypeError, |
| 1076 "decoding Unicode is not supported"); | |
| 1077 return NULL; | |
| 1078 } | |
| 1079 return PyObject_Unicode(obj); | |
| 1080 } | |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1081 #else |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1082 if (PyUnicode_Check(obj)) { |
| 51216 | 1083 PyErr_SetString(PyExc_TypeError, |
| 1084 "decoding Unicode is not supported"); | |
| 1085 return NULL; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1086 } |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1087 #endif |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1088 |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1089 /* 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
|
1090 if (PyString_Check(obj)) { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1091 s = PyString_AS_STRING(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1092 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
|
1093 } |
|
47291
04bd3690fb1f
First step of the C API rename:
Christian Heimes <christian@cheimes.de>
parents:
46908
diff
changeset
|
1094 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
|
1095 /* 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
|
1096 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
|
1097 "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
|
1098 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
|
1099 } |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1100 else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 51216 | 1101 /* Overwrite the error message with something more useful in |
| 1102 case of a TypeError. */ | |
| 1103 if (PyErr_ExceptionMatches(PyExc_TypeError)) | |
| 1104 PyErr_Format(PyExc_TypeError, | |
| 1105 "coercing to Unicode: need string or buffer, " | |
| 1106 "%.80s found", | |
| 1107 Py_TYPE(obj)->tp_name); | |
| 1108 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
|
1109 } |
|
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
|
1110 |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1111 /* 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
|
1112 if (len == 0) { |
| 51216 | 1113 Py_INCREF(unicode_empty); |
| 1114 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
|
1115 } |
|
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
|
1116 else |
| 51216 | 1117 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
|
1118 |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1119 return v; |
|
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1120 |
| 51216 | 1121 onError: |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1122 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1123 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1124 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1125 PyObject *PyUnicode_Decode(const char *s, |
| 51216 | 1126 Py_ssize_t size, |
| 1127 const char *encoding, | |
| 1128 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
|
1129 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1130 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
|
1131 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1132 if (encoding == NULL) |
| 51216 | 1133 encoding = PyUnicode_GetDefaultEncoding(); |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1134 |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1135 /* Shortcuts for common default encodings */ |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1136 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
|
1137 return PyUnicode_DecodeUTF8(s, size, errors); |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1138 else if (strcmp(encoding, "latin-1") == 0) |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1139 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
|
1140 #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
|
1141 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
|
1142 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
|
1143 #endif |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1144 else if (strcmp(encoding, "ascii") == 0) |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1145 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
|
1146 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1147 /* 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
|
1148 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
|
1149 if (buffer == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1150 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1151 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
|
1152 if (unicode == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1153 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1154 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
|
1155 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
|
1156 "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
|
1157 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
|
1158 Py_DECREF(unicode); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1159 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1160 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1161 Py_DECREF(buffer); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1162 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
|
1163 |
| 51216 | 1164 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1165 Py_XDECREF(buffer); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1166 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1167 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1168 |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1169 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
|
1170 const char *encoding, |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1171 const char *errors) |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1172 { |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1173 PyObject *v; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1174 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1175 if (!PyUnicode_Check(unicode)) { |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1176 PyErr_BadArgument(); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1177 goto onError; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1178 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1179 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1180 if (encoding == NULL) |
| 51216 | 1181 encoding = PyUnicode_GetDefaultEncoding(); |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1182 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1183 /* 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
|
1184 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
|
1185 if (v == NULL) |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1186 goto onError; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1187 return v; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1188 |
| 51216 | 1189 onError: |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1190 return NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1191 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1192 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1193 PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
| 51216 | 1194 Py_ssize_t size, |
| 1195 const char *encoding, | |
| 1196 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
|
1197 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1198 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
|
1199 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1200 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
|
1201 if (unicode == NULL) |
| 51216 | 1202 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1203 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
|
1204 Py_DECREF(unicode); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1205 return v; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1206 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1207 |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1208 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
|
1209 const char *encoding, |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1210 const char *errors) |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1211 { |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1212 PyObject *v; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1213 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1214 if (!PyUnicode_Check(unicode)) { |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1215 PyErr_BadArgument(); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1216 goto onError; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1217 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1218 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1219 if (encoding == NULL) |
| 51216 | 1220 encoding = PyUnicode_GetDefaultEncoding(); |
|
32551
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 /* 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
|
1223 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
|
1224 if (v == NULL) |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1225 goto onError; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1226 return v; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1227 |
| 51216 | 1228 onError: |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1229 return NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1230 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1231 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1232 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
|
1233 const char *encoding, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1234 const char *errors) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1235 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1236 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
|
1237 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1238 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
|
1239 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1240 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1241 } |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1242 |
|
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
|
1243 if (encoding == NULL) |
| 51216 | 1244 encoding = PyUnicode_GetDefaultEncoding(); |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1245 |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1246 /* Shortcuts for common default encodings */ |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1247 if (errors == NULL) { |
| 51216 | 1248 if (strcmp(encoding, "utf-8") == 0) |
| 1249 return PyUnicode_AsUTF8String(unicode); | |
| 1250 else if (strcmp(encoding, "latin-1") == 0) | |
| 1251 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
|
1252 #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 51216 | 1253 else if (strcmp(encoding, "mbcs") == 0) |
| 1254 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
|
1255 #endif |
| 51216 | 1256 else if (strcmp(encoding, "ascii") == 0) |
| 1257 return PyUnicode_AsASCIIString(unicode); | |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1258 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1259 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1260 /* 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
|
1261 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
|
1262 if (v == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1263 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
|
1264 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
|
1265 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
|
1266 "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
|
1267 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
|
1268 Py_DECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1269 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1270 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1271 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
|
1272 |
| 51216 | 1273 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1274 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1275 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1276 |
|
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
|
1277 PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 51216 | 1278 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
|
1279 { |
|
2badb322355f
This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents:
13828
diff
changeset
|
1280 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
|
1281 |
|
2badb322355f
This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents:
13828
diff
changeset
|
1282 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
|
1283 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
|
1284 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
|
1285 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
|
1286 ((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
|
1287 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
|
1288 } |
|
2badb322355f
This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents:
13828
diff
changeset
|
1289 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1290 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
|
1291 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1292 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
|
1293 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1294 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1295 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1296 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
|
1297 |
| 51216 | 1298 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1299 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1300 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1301 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1302 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
|
1303 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1304 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
|
1305 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1306 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1307 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1308 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
|
1309 |
| 51216 | 1310 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1311 return -1; |
|
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 |
|
13660
a1ce07218781
Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents:
13635
diff
changeset
|
1314 const char *PyUnicode_GetDefaultEncoding(void) |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1315 { |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1316 return unicode_default_encoding; |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1317 } |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1318 |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1319 int PyUnicode_SetDefaultEncoding(const char *encoding) |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1320 { |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1321 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
|
1322 |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1323 /* 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
|
1324 loads the encoding into the codec registry cache. */ |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1325 v = _PyCodec_Lookup(encoding); |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1326 if (v == NULL) |
| 51216 | 1327 goto onError; |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1328 Py_DECREF(v); |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1329 strncpy(unicode_default_encoding, |
| 51216 | 1330 encoding, |
| 1331 sizeof(unicode_default_encoding)); | |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1332 return 0; |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1333 |
| 51216 | 1334 onError: |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1335 return -1; |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1336 } |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1337 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1338 /* 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
|
1339 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
|
1340 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
|
1341 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
|
1342 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
|
1343 */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1344 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1345 static |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1346 int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 51216 | 1347 const char *encoding, const char *reason, |
| 1348 const char *input, Py_ssize_t insize, Py_ssize_t *startinpos, | |
| 1349 Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, | |
| 1350 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
|
1351 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1352 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
|
1353 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1354 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
|
1355 PyObject *repunicode = NULL; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1356 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
|
1357 Py_ssize_t requiredsize; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1358 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
|
1359 Py_UNICODE *repptr; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1360 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
|
1361 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
|
1362 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1363 if (*errorHandler == NULL) { |
| 51216 | 1364 *errorHandler = PyCodec_LookupError(errors); |
| 1365 if (*errorHandler == NULL) | |
| 1366 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
|
1367 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1368 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1369 if (*exceptionObject == NULL) { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1370 *exceptionObject = PyUnicodeDecodeError_Create( |
| 51216 | 1371 encoding, input, insize, *startinpos, *endinpos, reason); |
| 1372 if (*exceptionObject == NULL) | |
| 1373 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
|
1374 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1375 else { |
| 51216 | 1376 if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1377 goto onError; | |
| 1378 if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) | |
| 1379 goto onError; | |
| 1380 if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) | |
| 1381 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
|
1382 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1383 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1384 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
|
1385 if (restuple == NULL) |
| 51216 | 1386 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
|
1387 if (!PyTuple_Check(restuple)) { |
|
52909
f3b26a729e82
Merged revisions 69578-69580,69901,69907,69994,70022-70023,70025-70026,70166,70273,70275,70342,70386-70387,70389-70390,70392-70393,70395,70397,70400,70418 via svnmerge
Georg Brandl <georg@python.org>
parents:
52411
diff
changeset
|
1388 PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 51216 | 1389 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
|
1390 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1391 if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 51216 | 1392 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
|
1393 if (newpos<0) |
| 51216 | 1394 newpos = insize+newpos; |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
1395 if (newpos<0 || newpos>insize) { |
| 51216 | 1396 PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 1397 goto onError; | |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
1398 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1399 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1400 /* 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
|
1401 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
|
1402 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
|
1403 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
|
1404 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
|
1405 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
|
1406 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
|
1407 if (requiredsize > outsize) { |
| 51216 | 1408 if (requiredsize<2*outsize) |
| 1409 requiredsize = 2*outsize; | |
| 1410 if (_PyUnicode_Resize(output, requiredsize) < 0) | |
| 1411 goto onError; | |
| 1412 *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
|
1413 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1414 *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
|
1415 *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
|
1416 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
|
1417 *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
|
1418 *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
|
1419 /* 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
|
1420 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
|
1421 |
| 51216 | 1422 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
|
1423 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
|
1424 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
|
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 |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1427 /* --- UTF-7 Codec -------------------------------------------------------- */ |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1428 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1429 /* see RFC2152 for details */ |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1430 |
|
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
|
1431 static |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1432 char utf7_special[128] = { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1433 /* indicate whether a UTF-7 character is special i.e. cannot be directly |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1434 encoded: |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1435 0 - not special |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1436 1 - special |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1437 2 - whitespace (optional) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1438 3 - RFC2152 Set O (optional) */ |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1439 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1440 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1441 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1442 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1443 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1444 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1445 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1446 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1447 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1448 }; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1449 |
|
35728
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1450 /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
|
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1451 warnings about the comparison always being false; since |
|
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1452 utf7_special[0] is 1, we can safely make that one comparison |
|
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1453 true */ |
|
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1454 |
| 51216 | 1455 #define SPECIAL(c, encodeO, encodeWS) \ |
|
35728
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1456 ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
| 51216 | 1457 (encodeWS && (utf7_special[(c)] == 2)) || \ |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1458 (encodeO && (utf7_special[(c)] == 3))) |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1459 |
| 51216 | 1460 #define B64(n) \ |
|
35728
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1461 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 51216 | 1462 #define B64CHAR(c) \ |
|
35728
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1463 (isalnum(c) || (c) == '+' || (c) == '/') |
| 51216 | 1464 #define UB64(c) \ |
| 1465 ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ | |
|
35728
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1466 (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1467 |
|
35729
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1468 #define ENCODE(out, ch, bits) \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1469 while (bits >= 6) { \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1470 *out++ = B64(ch >> (bits-6)); \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1471 bits -= 6; \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1472 } |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1473 |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1474 #define DECODE(out, ch, bits, surrogate) \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1475 while (bits >= 16) { \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1476 Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1477 bits -= 16; \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1478 if (surrogate) { \ |
|
35728
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1479 /* We have already generated an error for the high surrogate \ |
|
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1480 so let's not bother seeing if the low surrogate is correct or not */ \ |
|
35729
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1481 surrogate = 0; \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1482 } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1483 /* This is a surrogate pair. Unfortunately we can't represent \ |
|
35729
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1484 it in a 16-bit character */ \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1485 surrogate = 1; \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1486 errmsg = "code pairs are not supported"; \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1487 goto utf7Error; \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1488 } else { \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1489 *out++ = outCh; \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1490 } \ |
|
35728
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1491 } |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1492 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1493 PyObject *PyUnicode_DecodeUTF7(const char *s, |
| 51216 | 1494 Py_ssize_t size, |
| 1495 const char *errors) | |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1496 { |
|
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
|
1497 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
|
1498 } |
|
e8ac1ae4503c
The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43888
diff
changeset
|
1499 |
|
e8ac1ae4503c
The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43888
diff
changeset
|
1500 PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
| 51216 | 1501 Py_ssize_t size, |
| 1502 const char *errors, | |
| 1503 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
|
1504 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1505 const char *starts = s; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1506 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1507 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1508 Py_ssize_t outpos; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1509 const char *e; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1510 PyUnicodeObject *unicode; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1511 Py_UNICODE *p; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1512 const char *errmsg = ""; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1513 int inShift = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1514 unsigned int bitsleft = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1515 unsigned long charsleft = 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
|
1516 int surrogate = 0; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1517 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
|
1518 PyObject *exc = NULL; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1519 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1520 unicode = _PyUnicode_New(size); |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1521 if (!unicode) |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1522 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
|
1523 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
|
1524 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
|
1525 *consumed = 0; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1526 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
|
1527 } |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1528 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1529 p = unicode->str; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1530 e = s + size; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1531 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1532 while (s < e) { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1533 Py_UNICODE ch; |
| 51216 | 1534 restart: |
|
48070
35a462ff9873
#2242: utf7 decoding crashes on bogus input on some Windows/MSVC versions
Antoine Pitrou <solipsis@pitrou.net>
parents:
48003
diff
changeset
|
1535 ch = (unsigned char) *s; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1536 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1537 if (inShift) { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1538 if ((ch == '-') || !B64CHAR(ch)) { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1539 inShift = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1540 s++; |
|
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
|
1541 |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1542 /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1543 if (bitsleft >= 6) { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1544 /* The shift sequence has a partial character in it. If |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1545 bitsleft < 6 then we could just classify it as padding |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1546 but that is not the case here */ |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1547 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1548 errmsg = "partial character in 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
|
1549 goto utf7Error; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1550 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1551 /* According to RFC2152 the remaining bits should be zero. We |
|
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
|
1552 choose to signal an error/insert a replacement character |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1553 here so indicate the potential of a misencoded character. */ |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1554 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1555 /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1556 if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1557 errmsg = "non-zero padding bits in 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
|
1558 goto utf7Error; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1559 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1560 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1561 if (ch == '-') { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1562 if ((s < e) && (*(s) == '-')) { |
|
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
|
1563 *p++ = '-'; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1564 inShift = 1; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1565 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1566 } else if (SPECIAL(ch,0,0)) { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1567 errmsg = "unexpected special character"; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1568 goto utf7Error; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1569 } else { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1570 *p++ = ch; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1571 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1572 } else { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1573 charsleft = (charsleft << 6) | UB64(ch); |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1574 bitsleft += 6; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1575 s++; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1576 /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1577 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1578 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1579 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
|
1580 startinpos = s-starts; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1581 s++; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1582 if (s < e && *s == '-') { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1583 s++; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1584 *p++ = '+'; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1585 } else |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1586 { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1587 inShift = 1; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1588 bitsleft = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1589 } |
|
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 else if (SPECIAL(ch,0,0)) { |
|
43053
6fcb873f396c
Set startinpos before calling the error handler.
Walter Dörwald <walter@livinglogic.de>
parents:
43052
diff
changeset
|
1592 startinpos = s-starts; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1593 errmsg = "unexpected special character"; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1594 s++; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1595 goto utf7Error; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1596 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1597 else { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1598 *p++ = ch; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1599 s++; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1600 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1601 continue; |
| 51216 | 1602 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
|
1603 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
|
1604 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
|
1605 if (unicode_decode_call_errorhandler( |
| 51216 | 1606 errors, &errorHandler, |
| 1607 "utf7", errmsg, | |
| 1608 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 1609 &unicode, &outpos, &p)) | |
| 1610 goto onError; | |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1611 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1612 |
|
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
|
1613 if (inShift && !consumed) { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1614 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
|
1615 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
|
1616 if (unicode_decode_call_errorhandler( |
| 51216 | 1617 errors, &errorHandler, |
| 1618 "utf7", "unterminated shift sequence", | |
| 1619 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 1620 &unicode, &outpos, &p)) | |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1621 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
|
1622 if (s < e) |
| 51216 | 1623 goto restart; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1624 } |
|
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
|
1625 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
|
1626 if(inShift) |
|
e8ac1ae4503c
The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43888
diff
changeset
|
1627 *consumed = startinpos; |
|
e8ac1ae4503c
The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43888
diff
changeset
|
1628 else |
|
e8ac1ae4503c
The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43888
diff
changeset
|
1629 *consumed = s-starts; |
|
e8ac1ae4503c
The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43888
diff
changeset
|
1630 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1631 |
|
30431
cd0c3f916b34
Change checks of PyUnicode_Resize() return value for clarity.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
30216
diff
changeset
|
1632 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
|
1633 goto onError; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1634 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1635 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
|
1636 Py_XDECREF(exc); |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1637 return (PyObject *)unicode; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1638 |
| 51216 | 1639 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
|
1640 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
|
1641 Py_XDECREF(exc); |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1642 Py_DECREF(unicode); |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1643 return NULL; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1644 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1645 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1646 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1647 PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 51216 | 1648 Py_ssize_t size, |
| 1649 int encodeSetO, | |
| 1650 int encodeWhiteSpace, | |
| 1651 const char *errors) | |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1652 { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1653 PyObject *v; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1654 /* It might be possible to tighten this worst case */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1655 Py_ssize_t cbAllocated = 5 * size; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1656 int inShift = 0; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1657 Py_ssize_t i = 0; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1658 unsigned int bitsleft = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1659 unsigned long charsleft = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1660 char * out; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1661 char * start; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1662 |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
1663 if (cbAllocated / 5 != size) |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
1664 return PyErr_NoMemory(); |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
1665 |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1666 if (size == 0) |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1667 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
|
1668 |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
1669 v = PyString_FromStringAndSize(NULL, cbAllocated); |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1670 if (v == NULL) |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1671 return NULL; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1672 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
1673 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
|
1674 for (;i < size; ++i) { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1675 Py_UNICODE ch = s[i]; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1676 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1677 if (!inShift) { |
|
31325
b4532c246a3a
Cosmetic fix for wrongly indented tabs with ts=4.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31252
diff
changeset
|
1678 if (ch == '+') { |
|
b4532c246a3a
Cosmetic fix for wrongly indented tabs with ts=4.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31252
diff
changeset
|
1679 *out++ = '+'; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1680 *out++ = '-'; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1681 } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1682 charsleft = ch; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1683 bitsleft = 16; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1684 *out++ = '+'; |
|
31325
b4532c246a3a
Cosmetic fix for wrongly indented tabs with ts=4.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31252
diff
changeset
|
1685 /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1686 inShift = bitsleft > 0; |
|
31325
b4532c246a3a
Cosmetic fix for wrongly indented tabs with ts=4.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31252
diff
changeset
|
1687 } else { |
|
b4532c246a3a
Cosmetic fix for wrongly indented tabs with ts=4.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31252
diff
changeset
|
1688 *out++ = (char) ch; |
|
b4532c246a3a
Cosmetic fix for wrongly indented tabs with ts=4.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31252
diff
changeset
|
1689 } |
|
b4532c246a3a
Cosmetic fix for wrongly indented tabs with ts=4.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31252
diff
changeset
|
1690 } else { |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1691 if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1692 *out++ = B64(charsleft << (6-bitsleft)); |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1693 charsleft = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1694 bitsleft = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1695 /* Characters not in the BASE64 set implicitly unshift the sequence |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1696 so no '-' is required, except if the character is itself a '-' */ |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1697 if (B64CHAR(ch) || ch == '-') { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1698 *out++ = '-'; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1699 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1700 inShift = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1701 *out++ = (char) ch; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1702 } else { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1703 bitsleft += 16; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1704 charsleft = (charsleft << 16) | ch; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1705 /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1706 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1707 /* If the next character is special then we dont' need to terminate |
|
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
|
1708 the shift sequence. If the next character is not a BASE64 character |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1709 or '-' then the shift sequence will be terminated implicitly and we |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1710 don't have to insert a '-'. */ |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1711 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1712 if (bitsleft == 0) { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1713 if (i + 1 < size) { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1714 Py_UNICODE ch2 = s[i+1]; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1715 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1716 if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
|
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
|
1717 |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1718 } else if (B64CHAR(ch2) || ch2 == '-') { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1719 *out++ = '-'; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1720 inShift = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1721 } else { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1722 inShift = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1723 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1724 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1725 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1726 else { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1727 *out++ = '-'; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1728 inShift = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1729 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1730 } |
|
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
|
1731 } |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1732 } |
|
31325
b4532c246a3a
Cosmetic fix for wrongly indented tabs with ts=4.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31252
diff
changeset
|
1733 } |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1734 if (bitsleft) { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1735 *out++= B64(charsleft << (6-bitsleft) ); |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1736 *out++ = '-'; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1737 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1738 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
1739 _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
|
1740 return v; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1741 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1742 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1743 #undef SPECIAL |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1744 #undef B64 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1745 #undef B64CHAR |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1746 #undef UB64 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1747 #undef ENCODE |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1748 #undef DECODE |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1749 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1750 /* --- UTF-8 Codec -------------------------------------------------------- */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1751 |
|
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
|
1752 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1753 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
|
1754 /* 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
|
1755 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
|
1756 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
|
1757 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
|
1758 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
|
1759 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
|
1760 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
|
1761 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
|
1762 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
|
1763 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
|
1764 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
|
1765 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
|
1766 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
|
1767 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
|
1768 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
|
1769 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
|
1770 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
|
1771 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
|
1772 }; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1773 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1774 PyObject *PyUnicode_DecodeUTF8(const char *s, |
| 51216 | 1775 Py_ssize_t size, |
| 1776 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
|
1777 { |
|
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
|
1778 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
|
1779 } |
|
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
|
1780 |
|
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
|
1781 PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
| 51216 | 1782 Py_ssize_t size, |
| 1783 const char *errors, | |
| 1784 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
|
1785 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1786 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
|
1787 int n; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1788 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1789 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1790 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
|
1791 const char *e; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1792 PyUnicodeObject *unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1793 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
|
1794 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
|
1795 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
|
1796 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
|
1797 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1798 /* 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
|
1799 character count */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1800 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
|
1801 if (!unicode) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1802 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
|
1803 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
|
1804 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
|
1805 *consumed = 0; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1806 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
|
1807 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1808 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1809 /* 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
|
1810 p = unicode->str; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1811 e = s + size; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1812 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1813 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
|
1814 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
|
1815 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1816 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
|
1817 *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
|
1818 s++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1819 continue; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1820 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1821 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1822 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
|
1823 |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1824 if (s + n > e) { |
| 51216 | 1825 if (consumed) |
| 1826 break; | |
| 1827 else { | |
| 1828 errmsg = "unexpected end of data"; | |
| 1829 startinpos = s-starts; | |
| 1830 endinpos = size; | |
| 1831 goto utf8Error; | |
| 1832 } | |
| 1833 } | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1834 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1835 switch (n) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1836 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1837 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
|
1838 errmsg = "unexpected code byte"; |
| 51216 | 1839 startinpos = s-starts; |
| 1840 endinpos = startinpos+1; | |
| 1841 goto utf8Error; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1842 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1843 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
|
1844 errmsg = "internal error"; |
| 51216 | 1845 startinpos = s-starts; |
| 1846 endinpos = startinpos+1; | |
| 1847 goto utf8Error; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1848 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1849 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
|
1850 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
|
1851 errmsg = "invalid data"; |
| 51216 | 1852 startinpos = s-starts; |
| 1853 endinpos = startinpos+2; | |
| 1854 goto utf8Error; | |
| 1855 } | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1856 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
|
1857 if (ch < 0x80) { |
| 51216 | 1858 startinpos = s-starts; |
| 1859 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
|
1860 errmsg = "illegal encoding"; |
| 51216 | 1861 goto utf8Error; |
| 1862 } | |
| 1863 else | |
| 1864 *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
|
1865 break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1866 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1867 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
|
1868 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
|
1869 (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
|
1870 errmsg = "invalid data"; |
| 51216 | 1871 startinpos = s-starts; |
| 1872 endinpos = startinpos+3; | |
| 1873 goto utf8Error; | |
| 1874 } | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1875 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
|
1876 if (ch < 0x0800) { |
| 51216 | 1877 /* Note: UTF-8 encodings of surrogates are considered |
| 1878 legal UTF-8 sequences; | |
| 1879 | |
| 1880 XXX For wide builds (UCS-4) we should probably try | |
| 1881 to recombine the surrogates into a single code | |
| 1882 unit. | |
| 1883 */ | |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1884 errmsg = "illegal encoding"; |
| 51216 | 1885 startinpos = s-starts; |
| 1886 endinpos = startinpos+3; | |
| 1887 goto utf8Error; | |
| 1888 } | |
| 1889 else | |
| 1890 *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
|
1891 break; |
|
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
1892 |
|
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
1893 case 4: |
|
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
1894 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
|
1895 (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
|
1896 (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
|
1897 errmsg = "invalid data"; |
| 51216 | 1898 startinpos = s-starts; |
| 1899 endinpos = startinpos+4; | |
| 1900 goto utf8Error; | |
| 1901 } | |
|
13411
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
1902 ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 51216 | 1903 ((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
|
1904 /* 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
|
1905 if ((ch < 0x10000) /* minimum value allowed for 4 |
| 51216 | 1906 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
|
1907 || (ch > 0x10ffff)) /* maximum value allowed for |
| 51216 | 1908 UTF-16 */ |
| 1909 { | |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1910 errmsg = "illegal encoding"; |
| 51216 | 1911 startinpos = s-starts; |
| 1912 endinpos = startinpos+4; | |
| 1913 goto utf8Error; | |
| 1914 } | |
|
18324
3ba7a358ce02
use Py_UNICODE_WIDE instead of USE_UCS4_STORAGE and Py_UNICODE_SIZE
Fredrik Lundh <fredrik@pythonware.com>
parents:
18298
diff
changeset
|
1915 #ifdef Py_UNICODE_WIDE |
| 51216 | 1916 *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
|
1917 #else |
|
13411
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
1918 /* 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
|
1919 |
|
13411
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
1920 /* 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
|
1921 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
|
1922 |
|
13411
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
1923 /* 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
|
1924 *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
|
1925 |
|
13411
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
1926 /* 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
|
1927 *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
|
1928 #endif |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1929 break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1930 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1931 default: |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1932 /* 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
|
1933 errmsg = "unsupported Unicode code range"; |
| 51216 | 1934 startinpos = s-starts; |
| 1935 endinpos = startinpos+n; | |
| 1936 goto utf8Error; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1937 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1938 s += n; |
| 51216 | 1939 continue; |
| 1940 | |
| 1941 utf8Error: | |
| 1942 outpos = p-PyUnicode_AS_UNICODE(unicode); | |
| 1943 if (unicode_decode_call_errorhandler( | |
| 1944 errors, &errorHandler, | |
| 1945 "utf8", errmsg, | |
| 1946 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 1947 &unicode, &outpos, &p)) | |
| 1948 goto onError; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1949 } |
|
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
|
1950 if (consumed) |
| 51216 | 1951 *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
|
1952 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1953 /* Adjust length */ |
|
30431
cd0c3f916b34
Change checks of PyUnicode_Resize() return value for clarity.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
30216
diff
changeset
|
1954 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
|
1955 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1956 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1957 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
|
1958 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
|
1959 return (PyObject *)unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1960 |
| 51216 | 1961 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
|
1962 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
|
1963 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
|
1964 Py_DECREF(unicode); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1965 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1966 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1967 |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1968 /* 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
|
1969 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
|
1970 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
|
1971 the excess memory at the end. |
| 23203 | 1972 */ |
|
23198
950a22eb8cc8
PyUnicode_EncodeUTF8: squash compiler wng. The difference of two
Tim Peters <tim.peters@gmail.com>
parents:
23192
diff
changeset
|
1973 PyObject * |
|
950a22eb8cc8
PyUnicode_EncodeUTF8: squash compiler wng. The difference of two
Tim Peters <tim.peters@gmail.com>
parents:
23192
diff
changeset
|
1974 PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 51216 | 1975 Py_ssize_t size, |
| 1976 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
|
1977 { |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1978 #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
|
1979 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1980 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
|
1981 PyObject *v; /* result string object */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1982 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
|
1983 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
|
1984 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
|
1985 char stackbuf[MAX_SHORT_UNICHARS * 4]; |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1986 |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1987 assert(s != NULL); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1988 assert(size >= 0); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1989 |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1990 if (size <= MAX_SHORT_UNICHARS) { |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1991 /* 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
|
1992 * 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
|
1993 * turns out we need. |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1994 */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1995 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
|
1996 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
|
1997 p = stackbuf; |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1998 } |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1999 else { |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2000 /* 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
|
2001 nallocated = size * 4; |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2002 if (nallocated / 4 != size) /* overflow! */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2003 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
|
2004 v = PyString_FromStringAndSize(NULL, nallocated); |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2005 if (v == NULL) |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2006 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
|
2007 p = PyString_AS_STRING(v); |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2008 } |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2009 |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2010 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
|
2011 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
|
2012 |
| 23203 | 2013 if (ch < 0x80) |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2014 /* Encode ASCII */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2015 *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
|
2016 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2017 else if (ch < 0x0800) { |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2018 /* Encode Latin-1 */ |
| 21884 | 2019 *p++ = (char)(0xc0 | (ch >> 6)); |
| 2020 *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
|
2021 } |
|
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
|
2022 else { |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2023 /* Encode UCS2 Unicode ordinals */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2024 if (ch < 0x10000) { |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2025 /* Special case: check for high surrogate */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2026 if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2027 Py_UCS4 ch2 = s[i]; |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2028 /* 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
|
2029 form a UCS4 value */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2030 if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 23203 | 2031 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
|
2032 i++; |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2033 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
|
2034 } |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2035 /* 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
|
2036 } |
|
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
2037 *p++ = (char)(0xe0 | (ch >> 12)); |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2038 *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2039 *p++ = (char)(0x80 | (ch & 0x3f)); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2040 continue; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
2041 } |
| 51216 | 2042 encodeUCS4: |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2043 /* Encode UCS4 Unicode ordinals */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2044 *p++ = (char)(0xf0 | (ch >> 18)); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2045 *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2046 *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2047 *p++ = (char)(0x80 | (ch & 0x3f)); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2048 } |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2049 } |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2050 |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2051 if (v == NULL) { |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2052 /* 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
|
2053 nneeded = p - stackbuf; |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2054 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
|
2055 v = PyString_FromStringAndSize(stackbuf, nneeded); |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2056 } |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2057 else { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
2058 /* 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
|
2059 nneeded = p - PyString_AS_STRING(v); |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2060 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
|
2061 _PyString_Resize(&v, nneeded); |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2062 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2063 return v; |
| 23203 | 2064 |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2065 #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
|
2066 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2067 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2068 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
|
2069 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2070 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
|
2071 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2072 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2073 } |
|
14059
d0348a26b093
PyUnicode_AsUTF8String(): Don't need to explicitly incref str since
Barry Warsaw <barry@python.org>
parents:
13966
diff
changeset
|
2074 return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 2075 PyUnicode_GET_SIZE(unicode), |
| 2076 NULL); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2077 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2078 |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2079 /* --- 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
|
2080 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2081 PyObject * |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2082 PyUnicode_DecodeUTF32(const char *s, |
| 51216 | 2083 Py_ssize_t size, |
| 2084 const char *errors, | |
| 2085 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
|
2086 { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2087 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
|
2088 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2089 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2090 PyObject * |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2091 PyUnicode_DecodeUTF32Stateful(const char *s, |
| 51216 | 2092 Py_ssize_t size, |
| 2093 const char *errors, | |
| 2094 int *byteorder, | |
| 2095 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
|
2096 { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2097 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
|
2098 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
|
2099 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
|
2100 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
|
2101 PyUnicodeObject *unicode; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2102 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
|
2103 #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
|
2104 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
|
2105 #else |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2106 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
|
2107 #endif |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2108 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
|
2109 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
|
2110 const char *errmsg = ""; |
|
42650
f2f0e95a797b
Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42649
diff
changeset
|
2111 /* 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
|
2112 #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
|
f2f0e95a797b
Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42649
diff
changeset
|
2113 int iorder[] = {0, 1, 2, 3}; |
|
f2f0e95a797b
Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42649
diff
changeset
|
2114 #else |
|
f2f0e95a797b
Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42649
diff
changeset
|
2115 int iorder[] = {3, 2, 1, 0}; |
|
f2f0e95a797b
Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42649
diff
changeset
|
2116 #endif |
|
42652
9c4f5b1d6b9d
Move another variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42650
diff
changeset
|
2117 PyObject *errorHandler = NULL; |
|
9c4f5b1d6b9d
Move another variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42650
diff
changeset
|
2118 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
|
2119 /* 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
|
2120 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
|
2121 #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
|
2122 for (i = pairs = 0; i < size/4; i++) |
| 51216 | 2123 if (((Py_UCS4 *)s)[i] >= 0x10000) |
| 2124 pairs++; | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2125 #endif |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2126 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2127 /* 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
|
2128 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
|
2129 if (!unicode) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2130 return NULL; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2131 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
|
2132 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
|
2133 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2134 /* 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
|
2135 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
|
2136 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
|
2137 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
|
2138 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2139 if (byteorder) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2140 bo = *byteorder; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2141 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2142 /* 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
|
2143 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
|
2144 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
|
2145 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
|
2146 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
|
2147 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
|
2148 const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 51216 | 2149 (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
|
2150 #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 51216 | 2151 if (bom == 0x0000FEFF) { |
| 2152 q += 4; | |
| 2153 bo = -1; | |
| 2154 } | |
| 2155 else if (bom == 0xFFFE0000) { | |
| 2156 q += 4; | |
| 2157 bo = 1; | |
| 2158 } | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2159 #else |
| 51216 | 2160 if (bom == 0x0000FEFF) { |
| 2161 q += 4; | |
| 2162 bo = 1; | |
| 2163 } | |
| 2164 else if (bom == 0xFFFE0000) { | |
| 2165 q += 4; | |
| 2166 bo = -1; | |
| 2167 } | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2168 #endif |
| 51216 | 2169 } |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2170 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2171 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2172 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
|
2173 /* force LE */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2174 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
|
2175 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
|
2176 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
|
2177 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
|
2178 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2179 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
|
2180 /* force BE */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2181 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
|
2182 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
|
2183 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
|
2184 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
|
2185 } |
|
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 while (q < e) { |
| 51216 | 2188 Py_UCS4 ch; |
| 2189 /* remaining bytes at the end? (size should be divisible by 4) */ | |
| 2190 if (e-q<4) { | |
| 2191 if (consumed) | |
| 2192 break; | |
| 2193 errmsg = "truncated data"; | |
| 2194 startinpos = ((const char *)q)-starts; | |
| 2195 endinpos = ((const char *)e)-starts; | |
| 2196 goto utf32Error; | |
| 2197 /* The remaining input chars are ignored if the callback | |
| 2198 chooses to skip the input */ | |
| 2199 } | |
| 2200 ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | | |
| 2201 (q[iorder[1]] << 8) | q[iorder[0]]; | |
| 2202 | |
| 2203 if (ch >= 0x110000) | |
| 2204 { | |
| 2205 errmsg = "codepoint not in range(0x110000)"; | |
| 2206 startinpos = ((const char *)q)-starts; | |
| 2207 endinpos = startinpos+4; | |
| 2208 goto utf32Error; | |
| 2209 } | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2210 #ifndef Py_UNICODE_WIDE |
| 51216 | 2211 if (ch >= 0x10000) |
| 2212 { | |
| 2213 *p++ = 0xD800 | ((ch-0x10000) >> 10); | |
| 2214 *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); | |
| 2215 } | |
| 2216 else | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2217 #endif |
| 51216 | 2218 *p++ = ch; |
| 2219 q += 4; | |
| 2220 continue; | |
| 2221 utf32Error: | |
| 2222 outpos = p-PyUnicode_AS_UNICODE(unicode); | |
| 2223 if (unicode_decode_call_errorhandler( | |
| 2224 errors, &errorHandler, | |
| 2225 "utf32", errmsg, | |
| 2226 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 2227 &unicode, &outpos, &p)) | |
| 2228 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
|
2229 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2230 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2231 if (byteorder) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2232 *byteorder = bo; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2233 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2234 if (consumed) |
| 51216 | 2235 *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
|
2236 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2237 /* Adjust length */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2238 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
|
2239 goto onError; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2240 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2241 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
|
2242 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
|
2243 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
|
2244 |
| 51216 | 2245 onError: |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2246 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
|
2247 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
|
2248 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
|
2249 return NULL; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2250 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2251 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2252 PyObject * |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2253 PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 51216 | 2254 Py_ssize_t size, |
| 2255 const char *errors, | |
| 2256 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
|
2257 { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2258 PyObject *v; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2259 unsigned char *p; |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2260 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
|
2261 #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
|
2262 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
|
2263 #else |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2264 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
|
2265 #endif |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2266 /* 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
|
2267 #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
|
2268 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
|
2269 #else |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2270 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
|
2271 #endif |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2272 |
| 51216 | 2273 #define STORECHAR(CH) \ |
| 2274 do { \ | |
| 2275 p[iorder[3]] = ((CH) >> 24) & 0xff; \ | |
| 2276 p[iorder[2]] = ((CH) >> 16) & 0xff; \ | |
| 2277 p[iorder[1]] = ((CH) >> 8) & 0xff; \ | |
| 2278 p[iorder[0]] = (CH) & 0xff; \ | |
| 2279 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
|
2280 } while(0) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2281 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2282 /* 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
|
2283 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
|
2284 #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
|
2285 for (i = pairs = 0; i < size-1; i++) |
| 51216 | 2286 if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2287 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) | |
| 2288 pairs++; | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2289 #endif |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2290 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
|
2291 bytesize = nsize * 4; |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2292 if (bytesize / 4 != nsize) |
| 51216 | 2293 return PyErr_NoMemory(); |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2294 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
|
2295 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
|
2296 return NULL; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2297 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
2298 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
|
2299 if (byteorder == 0) |
| 51216 | 2300 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
|
2301 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
|
2302 return v; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2303 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2304 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
|
2305 /* force LE */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2306 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
|
2307 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
|
2308 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
|
2309 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
|
2310 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2311 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
|
2312 /* force BE */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2313 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
|
2314 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
|
2315 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
|
2316 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
|
2317 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2318 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2319 while (size-- > 0) { |
| 51216 | 2320 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
|
2321 #ifndef Py_UNICODE_WIDE |
| 51216 | 2322 if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2323 Py_UCS4 ch2 = *s; | |
| 2324 if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { | |
| 2325 ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; | |
| 2326 s++; | |
| 2327 size--; | |
| 2328 } | |
| 2329 } | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2330 #endif |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2331 STORECHAR(ch); |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2332 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2333 return v; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2334 #undef STORECHAR |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2335 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2336 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2337 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
|
2338 { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2339 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
|
2340 PyErr_BadArgument(); |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2341 return NULL; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2342 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2343 return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 2344 PyUnicode_GET_SIZE(unicode), |
| 2345 NULL, | |
| 2346 0); | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2347 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2348 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2349 /* --- UTF-16 Codec ------------------------------------------------------- */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2350 |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2351 PyObject * |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2352 PyUnicode_DecodeUTF16(const char *s, |
| 51216 | 2353 Py_ssize_t size, |
| 2354 const char *errors, | |
| 2355 int *byteorder) | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2356 { |
|
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
|
2357 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
|
2358 } |
|
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
|
2359 |
|
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
|
2360 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
|
2361 PyUnicode_DecodeUTF16Stateful(const char *s, |
| 51216 | 2362 Py_ssize_t size, |
| 2363 const char *errors, | |
| 2364 int *byteorder, | |
| 2365 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
|
2366 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2367 const char *starts = s; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2368 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2369 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2370 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
|
2371 PyUnicodeObject *unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2372 Py_UNICODE *p; |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2373 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
|
2374 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
|
2375 const char *errmsg = ""; |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2376 /* 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
|
2377 #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
|
2378 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
|
2379 #else |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2380 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
|
2381 #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
|
2382 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
|
2383 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
|
2384 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2385 /* 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
|
2386 character count */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2387 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
|
2388 if (!unicode) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2389 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2390 if (size == 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2391 return (PyObject *)unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2392 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2393 /* 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
|
2394 p = unicode->str; |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2395 q = (unsigned char *)s; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2396 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
|
2397 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2398 if (byteorder) |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2399 bo = *byteorder; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2400 |
|
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
|
2401 /* 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
|
2402 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
|
2403 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
|
2404 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
|
2405 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
|
2406 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
|
2407 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
|
2408 #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 51216 | 2409 if (bom == 0xFEFF) { |
| 2410 q += 2; | |
| 2411 bo = -1; | |
| 2412 } | |
| 2413 else if (bom == 0xFFFE) { | |
| 2414 q += 2; | |
| 2415 bo = 1; | |
| 2416 } | |
|
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
|
2417 #else |
| 51216 | 2418 if (bom == 0xFEFF) { |
| 2419 q += 2; | |
| 2420 bo = 1; | |
| 2421 } | |
| 2422 else if (bom == 0xFFFE) { | |
| 2423 q += 2; | |
| 2424 bo = -1; | |
| 2425 } | |
|
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
|
2426 #endif |
| 51216 | 2427 } |
|
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
|
2428 } |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2429 |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2430 if (bo == -1) { |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2431 /* force LE */ |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2432 ihi = 1; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2433 ilo = 0; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2434 } |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2435 else if (bo == 1) { |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2436 /* force BE */ |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2437 ihi = 0; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2438 ilo = 1; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2439 } |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2440 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2441 while (q < e) { |
| 51216 | 2442 Py_UNICODE ch; |
| 2443 /* remaining bytes at the end? (size should be even) */ | |
| 2444 if (e-q<2) { | |
| 2445 if (consumed) | |
| 2446 break; | |
| 2447 errmsg = "truncated data"; | |
| 2448 startinpos = ((const char *)q)-starts; | |
| 2449 endinpos = ((const char *)e)-starts; | |
| 2450 goto utf16Error; | |
| 2451 /* The remaining input chars are ignored if the callback | |
| 2452 chooses to skip the input */ | |
| 2453 } | |
| 2454 ch = (q[ihi] << 8) | q[ilo]; | |
| 2455 | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
2456 q += 2; |
| 51216 | 2457 |
| 2458 if (ch < 0xD800 || ch > 0xDFFF) { | |
| 2459 *p++ = ch; | |
| 2460 continue; | |
| 2461 } | |
| 2462 | |
| 2463 /* UTF-16 code pair: */ | |
| 2464 if (q >= e) { | |
| 2465 errmsg = "unexpected end of data"; | |
| 2466 startinpos = (((const char *)q)-2)-starts; | |
| 2467 endinpos = ((const char *)e)-starts; | |
| 2468 goto utf16Error; | |
| 2469 } | |
| 2470 if (0xD800 <= ch && ch <= 0xDBFF) { | |
| 2471 Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; | |
| 2472 q += 2; | |
| 2473 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
|
2474 #ifndef Py_UNICODE_WIDE |
| 51216 | 2475 *p++ = ch; |
| 2476 *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
|
2477 #else |
| 51216 | 2478 *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
|
2479 #endif |
| 51216 | 2480 continue; |
| 2481 } | |
| 2482 else { | |
|
18292
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2483 errmsg = "illegal UTF-16 surrogate"; |
| 51216 | 2484 startinpos = (((const char *)q)-4)-starts; |
| 2485 endinpos = startinpos+2; | |
| 2486 goto utf16Error; | |
| 2487 } | |
| 2488 | |
| 2489 } | |
| 2490 errmsg = "illegal encoding"; | |
| 2491 startinpos = (((const char *)q)-2)-starts; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
2492 endinpos = startinpos+2; |
| 51216 | 2493 /* Fall through to report the error */ |
| 2494 | |
| 2495 utf16Error: | |
| 2496 outpos = p-PyUnicode_AS_UNICODE(unicode); | |
| 2497 if (unicode_decode_call_errorhandler( | |
| 2498 errors, &errorHandler, | |
| 2499 "utf16", errmsg, | |
| 2500 starts, size, &startinpos, &endinpos, &exc, (const char **)&q, | |
| 2501 &unicode, &outpos, &p)) | |
| 2502 goto onError; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2503 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2504 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2505 if (byteorder) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2506 *byteorder = bo; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2507 |
|
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
|
2508 if (consumed) |
| 51216 | 2509 *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
|
2510 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2511 /* Adjust length */ |
|
30431
cd0c3f916b34
Change checks of PyUnicode_Resize() return value for clarity.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
30216
diff
changeset
|
2512 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
|
2513 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2514 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2515 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
|
2516 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
|
2517 return (PyObject *)unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2518 |
| 51216 | 2519 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2520 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
|
2521 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
|
2522 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
|
2523 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2524 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2525 |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2526 PyObject * |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2527 PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 51216 | 2528 Py_ssize_t size, |
| 2529 const char *errors, | |
| 2530 int byteorder) | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2531 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2532 PyObject *v; |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2533 unsigned char *p; |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2534 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
|
2535 #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
|
2536 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
|
2537 #else |
|
4edf200a8118
SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31199
diff
changeset
|
2538 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
|
2539 #endif |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2540 /* 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
|
2541 #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
|
2542 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
|
2543 #else |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2544 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
|
2545 #endif |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2546 |
| 51216 | 2547 #define STORECHAR(CH) \ |
| 2548 do { \ | |
| 2549 p[ihi] = ((CH) >> 8) & 0xff; \ | |
| 2550 p[ilo] = (CH) & 0xff; \ | |
| 2551 p += 2; \ | |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2552 } 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
|
2553 |
|
31238
4edf200a8118
SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31199
diff
changeset
|
2554 #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
|
2555 for (i = pairs = 0; i < size; i++) |
| 51216 | 2556 if (s[i] >= 0x10000) |
| 2557 pairs++; | |
|
31238
4edf200a8118
SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31199
diff
changeset
|
2558 #endif |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2559 /* 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
|
2560 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
|
2561 size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
| 51216 | 2562 return PyErr_NoMemory(); |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2563 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
|
2564 bytesize = nsize * 2; |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2565 if (bytesize / 2 != nsize) |
| 51216 | 2566 return PyErr_NoMemory(); |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2567 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
|
2568 if (v == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2569 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2570 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
2571 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
|
2572 if (byteorder == 0) |
| 51216 | 2573 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
|
2574 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
|
2575 return v; |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2576 |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2577 if (byteorder == -1) { |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2578 /* force LE */ |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2579 ihi = 1; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2580 ilo = 0; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2581 } |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2582 else if (byteorder == 1) { |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2583 /* force BE */ |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2584 ihi = 0; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2585 ilo = 1; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2586 } |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2587 |
|
18292
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2588 while (size-- > 0) { |
| 51216 | 2589 Py_UNICODE ch = *s++; |
| 2590 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
|
2591 #ifdef Py_UNICODE_WIDE |
| 51216 | 2592 if (ch >= 0x10000) { |
| 2593 ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); | |
| 2594 ch = 0xD800 | ((ch-0x10000) >> 10); | |
| 2595 } | |
|
31238
4edf200a8118
SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31199
diff
changeset
|
2596 #endif |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2597 STORECHAR(ch); |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2598 if (ch2) |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2599 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
|
2600 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2601 return v; |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2602 #undef STORECHAR |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2603 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2604 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2605 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
|
2606 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2607 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
|
2608 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2609 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2610 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2611 return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 2612 PyUnicode_GET_SIZE(unicode), |
| 2613 NULL, | |
| 2614 0); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2615 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2616 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2617 /* --- Unicode Escape Codec ----------------------------------------------- */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2618 |
|
16200
3484670c37f2
Move uchhash functionality into unicodedata (after the recent
Fredrik Lundh <fredrik@pythonware.com>
parents:
16064
diff
changeset
|
2619 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
|
2620 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2621 PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
| 51216 | 2622 Py_ssize_t size, |
| 2623 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
|
2624 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2625 const char *starts = s; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2626 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2627 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2628 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
|
2629 int i; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2630 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
|
2631 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
|
2632 const char *end; |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2633 char* message; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2634 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
|
2635 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
|
2636 PyObject *exc = NULL; |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2637 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2638 /* 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
|
2639 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
|
2640 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
|
2641 (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
|
2642 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
|
2643 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
|
2644 if (v == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2645 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2646 if (size == 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2647 return (PyObject *)v; |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2648 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2649 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
|
2650 end = s + size; |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2651 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2652 while (s < end) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2653 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
|
2654 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
|
2655 int digits; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2656 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2657 /* 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
|
2658 if (*s != '\\') { |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2659 *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
|
2660 continue; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2661 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2662 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2663 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
|
2664 /* \ - Escapes */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2665 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
|
2666 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
|
2667 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
|
2668 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
|
2669 switch (c) { |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2670 |
| 51216 | 2671 /* \x escapes */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2672 case '\n': break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2673 case '\\': *p++ = '\\'; break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2674 case '\'': *p++ = '\''; break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2675 case '\"': *p++ = '\"'; break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2676 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
|
2677 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
|
2678 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
|
2679 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
|
2680 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
|
2681 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
|
2682 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
|
2683 |
| 51216 | 2684 /* \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
|
2685 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
|
2686 case '4': case '5': case '6': case '7': |
| 12544 | 2687 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
|
2688 if (s < end && '0' <= *s && *s <= '7') { |
| 12544 | 2689 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
|
2690 if (s < end && '0' <= *s && *s <= '7') |
| 12544 | 2691 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
|
2692 } |
| 12544 | 2693 *p++ = x; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2694 break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2695 |
| 51216 | 2696 /* hex escapes */ |
| 2697 /* \xXX */ | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2698 case 'x': |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2699 digits = 2; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2700 message = "truncated \\xXX escape"; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2701 goto hexescape; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2702 |
| 51216 | 2703 /* \uXXXX */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2704 case 'u': |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2705 digits = 4; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2706 message = "truncated \\uXXXX escape"; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2707 goto hexescape; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2708 |
| 51216 | 2709 /* \UXXXXXXXX */ |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2710 case 'U': |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2711 digits = 8; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2712 message = "truncated \\UXXXXXXXX escape"; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2713 hexescape: |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2714 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
|
2715 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
|
2716 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
|
2717 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
|
2718 if (unicode_decode_call_errorhandler( |
| 51216 | 2719 errors, &errorHandler, |
| 2720 "unicodeescape", "end of string in escape sequence", | |
| 2721 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 2722 &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
|
2723 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
|
2724 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
|
2725 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2726 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
|
2727 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
|
2728 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
|
2729 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
|
2730 if (unicode_decode_call_errorhandler( |
| 51216 | 2731 errors, &errorHandler, |
| 2732 "unicodeescape", message, | |
| 2733 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 2734 &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
|
2735 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
|
2736 goto nextByte; |
|
14388
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2737 } |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2738 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
|
2739 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
|
2740 chr += c - '0'; |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2741 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
|
2742 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
|
2743 else |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2744 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
|
2745 } |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2746 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
|
2747 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
|
2748 /* _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
|
2749 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
|
2750 break; |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2751 store: |
|
14388
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2752 /* 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
|
2753 if (chr <= 0xffff) |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2754 /* UCS-2 character */ |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2755 *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
|
2756 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
|
2757 /* UCS-4 character. Either store directly, or as |
| 22521 | 2758 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
|
2759 #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
|
2760 *p++ = chr; |
|
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2761 #else |
|
14388
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2762 chr -= 0x10000L; |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2763 *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
|
2764 *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
|
2765 #endif |
|
14388
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2766 } 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
|
2767 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
|
2768 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
|
2769 if (unicode_decode_call_errorhandler( |
| 51216 | 2770 errors, &errorHandler, |
| 2771 "unicodeescape", "illegal Unicode character", | |
| 2772 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 2773 &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
|
2774 goto onError; |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2775 } |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2776 break; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2777 |
| 51216 | 2778 /* \N{name} */ |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2779 case 'N': |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2780 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
|
2781 if (ucnhash_CAPI == NULL) { |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2782 /* 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
|
2783 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
|
2784 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
|
2785 if (m == NULL) |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2786 goto ucnhashError; |
|
36877
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36675
diff
changeset
|
2787 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
|
2788 Py_DECREF(m); |
|
36877
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36675
diff
changeset
|
2789 if (api == NULL) |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2790 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
|
2791 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
|
2792 Py_DECREF(api); |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2793 if (ucnhash_CAPI == NULL) |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2794 goto ucnhashError; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2795 } |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2796 if (*s == '{') { |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2797 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
|
2798 /* 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
|
2799 while (*s != '}' && s < end) |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2800 s++; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2801 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
|
2802 /* 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
|
2803 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
|
2804 s++; |
|
36924
48e0a5e39403
Update Unicode database to Unicode 4.1.
Martin v. Löwis <martin@v.loewis.de>
parents:
36882
diff
changeset
|
2805 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
|
2806 goto store; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2807 } |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2808 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2809 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
|
2810 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
|
2811 if (unicode_decode_call_errorhandler( |
| 51216 | 2812 errors, &errorHandler, |
| 2813 "unicodeescape", message, | |
| 2814 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 2815 &v, &outpos, &p)) | |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2816 goto onError; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2817 break; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2818 |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2819 default: |
| 22521 | 2820 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
|
2821 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
|
2822 s--; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2823 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
|
2824 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
|
2825 if (unicode_decode_call_errorhandler( |
| 51216 | 2826 errors, &errorHandler, |
| 2827 "unicodeescape", message, | |
| 2828 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 2829 &v, &outpos, &p)) | |
| 22521 | 2830 goto onError; |
| 2831 } | |
| 2832 else { | |
| 2833 *p++ = '\\'; | |
| 2834 *p++ = (unsigned char)s[-1]; | |
| 2835 } | |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2836 break; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2837 } |
| 51216 | 2838 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
|
2839 ; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2840 } |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
2841 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
|
2842 goto onError; |
|
30160
3329b058db20
Fix another refcounting leak (in PyUnicode_DecodeUnicodeEscape()).
Walter Dörwald <walter@livinglogic.de>
parents:
30149
diff
changeset
|
2843 Py_XDECREF(errorHandler); |
|
3329b058db20
Fix another refcounting leak (in PyUnicode_DecodeUnicodeEscape()).
Walter Dörwald <walter@livinglogic.de>
parents:
30149
diff
changeset
|
2844 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
|
2845 return (PyObject *)v; |
| 22521 | 2846 |
| 51216 | 2847 ucnhashError: |
|
16200
3484670c37f2
Move uchhash functionality into unicodedata (after the recent
Fredrik Lundh <fredrik@pythonware.com>
parents:
16064
diff
changeset
|
2848 PyErr_SetString( |
|
3484670c37f2
Move uchhash functionality into unicodedata (after the recent
Fredrik Lundh <fredrik@pythonware.com>
parents:
16064
diff
changeset
|
2849 PyExc_UnicodeError, |
|
3484670c37f2
Move uchhash functionality into unicodedata (after the recent
Fredrik Lundh <fredrik@pythonware.com>
parents:
16064
diff
changeset
|
2850 "\\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
|
2851 ); |
|
36877
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36675
diff
changeset
|
2852 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
|
2853 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
|
2854 Py_XDECREF(exc); |
|
16064
a97aa9238771
Better error message if ucnhash cannot be found (obscure attribute
Fredrik Lundh <fredrik@pythonware.com>
parents:
16029
diff
changeset
|
2855 return NULL; |
|
a97aa9238771
Better error message if ucnhash cannot be found (obscure attribute
Fredrik Lundh <fredrik@pythonware.com>
parents:
16029
diff
changeset
|
2856 |
| 51216 | 2857 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2858 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
|
2859 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
|
2860 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
|
2861 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2862 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2863 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2864 /* 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
|
2865 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2866 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
|
2867 appropriate. |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2868 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2869 */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2870 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
2871 Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
| 51216 | 2872 Py_ssize_t size, |
| 2873 Py_UNICODE ch) | |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2874 { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2875 /* 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
|
2876 |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2877 while (size-- > 0) { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2878 if (*s == ch) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2879 return s; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2880 s++; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2881 } |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2882 |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2883 return NULL; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2884 } |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11997
diff
changeset
|
2885 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2886 static |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2887 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
|
2888 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
|
2889 int quotes) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2890 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2891 PyObject *repr; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2892 char *p; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2893 |
|
16226
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
2894 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
|
2895 #ifdef Py_UNICODE_WIDE |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2896 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
|
2897 #else |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2898 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
|
2899 #endif |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2900 |
|
39621
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2901 /* 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
|
2902 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
|
2903 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
|
2904 */ |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2905 /* 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
|
2906 escape. |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2907 |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2908 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
|
2909 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
|
2910 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
|
2911 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
|
2912 (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
|
2913 |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2914 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
|
2915 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
|
2916 escape. |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2917 */ |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2918 |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2919 if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
| 51216 | 2920 return PyErr_NoMemory(); |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2921 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
2922 repr = PyString_FromStringAndSize(NULL, |
| 51216 | 2923 2 |
| 2924 + expandsize*size | |
| 2925 + 1); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2926 if (repr == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2927 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2928 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
2929 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
|
2930 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2931 if (quotes) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2932 *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
|
2933 *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
|
2934 !findchar(s, size, '"')) ? '"' : '\''; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2935 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2936 while (size-- > 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2937 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
|
2938 |
|
36056
f1ae11457d34
Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents:
35999
diff
changeset
|
2939 /* 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
|
2940 if ((quotes && |
| 51216 | 2941 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
|
2942 *p++ = '\\'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2943 *p++ = (char) ch; |
| 51216 | 2944 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
|
2945 } |
|
18828
081d96fbcf43
Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents:
18774
diff
changeset
|
2946 |
|
18773
743f8e1d953c
#ifdef out generation of \U escapes unless Py_UNICODE_WIDE. This
Guido van Rossum <guido@python.org>
parents:
18324
diff
changeset
|
2947 #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
|
2948 /* 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
|
2949 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
|
2950 *p++ = '\\'; |
|
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2951 *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
|
2952 *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
|
2953 *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
|
2954 *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
|
2955 *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
|
2956 *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
|
2957 *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
|
2958 *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
|
2959 *p++ = hexdigit[ch & 0x0000000F]; |
| 51216 | 2960 continue; |
|
18292
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2961 } |
|
39621
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2962 #else |
| 51216 | 2963 /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 2964 else if (ch >= 0xD800 && ch < 0xDC00) { | |
| 2965 Py_UNICODE ch2; | |
| 2966 Py_UCS4 ucs; | |
| 2967 | |
| 2968 ch2 = *s++; | |
| 2969 size--; | |
| 2970 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { | |
| 2971 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; | |
| 2972 *p++ = '\\'; | |
| 2973 *p++ = 'U'; | |
| 2974 *p++ = hexdigit[(ucs >> 28) & 0x0000000F]; | |
| 2975 *p++ = hexdigit[(ucs >> 24) & 0x0000000F]; | |
| 2976 *p++ = hexdigit[(ucs >> 20) & 0x0000000F]; | |
| 2977 *p++ = hexdigit[(ucs >> 16) & 0x0000000F]; | |
| 2978 *p++ = hexdigit[(ucs >> 12) & 0x0000000F]; | |
| 2979 *p++ = hexdigit[(ucs >> 8) & 0x0000000F]; | |
| 2980 *p++ = hexdigit[(ucs >> 4) & 0x0000000F]; | |
| 2981 *p++ = hexdigit[ucs & 0x0000000F]; | |
| 2982 continue; | |
| 2983 } | |
| 2984 /* Fall through: isolated surrogates are copied as-is */ | |
| 2985 s--; | |
| 2986 size++; | |
| 2987 } | |
|
39621
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2988 #endif |
|
18774
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2989 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2990 /* 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
|
2991 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
|
2992 *p++ = '\\'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2993 *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
|
2994 *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
|
2995 *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
|
2996 *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
|
2997 *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
|
2998 } |
|
18828
081d96fbcf43
Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents:
18774
diff
changeset
|
2999 |
|
16226
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3000 /* 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
|
3001 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
|
3002 *p++ = '\\'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3003 *p++ = 't'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3004 } |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3005 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
|
3006 *p++ = '\\'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3007 *p++ = 'n'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3008 } |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3009 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
|
3010 *p++ = '\\'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3011 *p++ = 'r'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3012 } |
|
18828
081d96fbcf43
Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents:
18774
diff
changeset
|
3013 |
|
16226
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3014 /* 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
|
3015 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
|
3016 *p++ = '\\'; |
|
16226
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3017 *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
|
3018 *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
|
3019 *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
|
3020 } |
|
18828
081d96fbcf43
Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents:
18774
diff
changeset
|
3021 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3022 /* 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
|
3023 else |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3024 *p++ = (char) ch; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3025 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3026 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
|
3027 *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
|
3028 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3029 *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
|
3030 _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
|
3031 return repr; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3032 } |
|
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 PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 51216 | 3035 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
|
3036 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3037 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
|
3038 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3039 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3040 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
|
3041 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3042 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
|
3043 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3044 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3045 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3046 return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 3047 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
|
3048 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3049 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3050 /* --- 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
|
3051 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3052 PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
| 51216 | 3053 Py_ssize_t size, |
| 3054 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
|
3055 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3056 const char *starts = s; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3057 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3058 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3059 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
|
3060 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
|
3061 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
|
3062 const char *end; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3063 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
|
3064 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
|
3065 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
|
3066 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3067 /* 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
|
3068 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
|
3069 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
|
3070 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
|
3071 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
|
3072 if (v == NULL) |
| 51216 | 3073 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3074 if (size == 0) |
| 51216 | 3075 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
|
3076 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
|
3077 end = s + size; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3078 while (s < end) { |
| 51216 | 3079 unsigned char c; |
| 3080 Py_UCS4 x; | |
| 3081 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
|
3082 int count; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3083 |
| 51216 | 3084 /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3085 if (*s != '\\') { | |
| 3086 *p++ = (unsigned char)*s++; | |
| 3087 continue; | |
| 3088 } | |
| 3089 startinpos = s-starts; | |
| 3090 | |
| 3091 /* \u-escapes are only interpreted iff the number of leading | |
| 3092 backslashes if odd */ | |
| 3093 bs = s; | |
| 3094 for (;s < end;) { | |
| 3095 if (*s != '\\') | |
| 3096 break; | |
| 3097 *p++ = (unsigned char)*s++; | |
| 3098 } | |
| 3099 if (((s - bs) & 1) == 0 || | |
| 3100 s >= end || | |
| 3101 (*s != 'u' && *s != 'U')) { | |
| 3102 continue; | |
| 3103 } | |
| 3104 p--; | |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3105 count = *s=='u' ? 4 : 8; |
| 51216 | 3106 s++; |
| 3107 | |
| 3108 /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ | |
| 3109 outpos = p-PyUnicode_AS_UNICODE(v); | |
| 3110 for (x = 0, i = 0; i < count; ++i, ++s) { | |
| 3111 c = (unsigned char)*s; | |
| 3112 if (!isxdigit(c)) { | |
| 3113 endinpos = s-starts; | |
| 3114 if (unicode_decode_call_errorhandler( | |
| 3115 errors, &errorHandler, | |
| 3116 "rawunicodeescape", "truncated \\uXXXX", | |
| 3117 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 3118 &v, &outpos, &p)) | |
| 3119 goto onError; | |
| 3120 goto nextByte; | |
| 3121 } | |
| 3122 x = (x<<4) & ~0xF; | |
| 3123 if (c >= '0' && c <= '9') | |
| 3124 x += c - '0'; | |
| 3125 else if (c >= 'a' && c <= 'f') | |
| 3126 x += 10 + c - 'a'; | |
| 3127 else | |
| 3128 x += 10 + c - 'A'; | |
| 3129 } | |
|
46028
8710ba7fba2b
#1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46026
diff
changeset
|
3130 if (x <= 0xffff) |
| 51216 | 3131 /* UCS-2 character */ |
| 3132 *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
|
3133 else if (x <= 0x10ffff) { |
| 51216 | 3134 /* UCS-4 character. Either store directly, or as |
| 3135 surrogate pair. */ | |
|
46028
8710ba7fba2b
#1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46026
diff
changeset
|
3136 #ifdef Py_UNICODE_WIDE |
| 51216 | 3137 *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
|
3138 #else |
| 51216 | 3139 x -= 0x10000L; |
| 3140 *p++ = 0xD800 + (Py_UNICODE) (x >> 10); | |
| 3141 *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
|
3142 #endif |
|
8710ba7fba2b
#1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46026
diff
changeset
|
3143 } else { |
|
8710ba7fba2b
#1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46026
diff
changeset
|
3144 endinpos = s-starts; |
|
8710ba7fba2b
#1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46026
diff
changeset
|
3145 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
|
3146 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
|
3147 errors, &errorHandler, |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3148 "rawunicodeescape", "\\Uxxxxxxxx out of range", |
| 51216 | 3149 starts, size, &startinpos, &endinpos, &exc, &s, |
| 3150 &v, &outpos, &p)) | |
| 3151 goto onError; | |
| 3152 } | |
| 3153 nextByte: | |
| 3154 ; | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3155 } |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
3156 if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 51216 | 3157 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
|
3158 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
|
3159 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
|
3160 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
|
3161 |
| 51216 | 3162 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3163 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
|
3164 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
|
3165 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
|
3166 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3167 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3168 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3169 PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 51216 | 3170 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
|
3171 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3172 PyObject *repr; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3173 char *p; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3174 char *q; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3175 |
|
16226
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3176 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
|
3177 #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
|
3178 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
|
3179 #else |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
3180 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
|
3181 #endif |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
3182 |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
3183 if (size > PY_SSIZE_T_MAX / expandsize) |
| 51216 | 3184 return PyErr_NoMemory(); |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
3185 |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
3186 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
|
3187 if (repr == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3188 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
|
3189 if (size == 0) |
| 51216 | 3190 return repr; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3191 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
3192 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
|
3193 while (size-- > 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3194 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
|
3195 #ifdef Py_UNICODE_WIDE |
| 51216 | 3196 /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3197 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
|
3198 *p++ = '\\'; |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3199 *p++ = 'U'; |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3200 *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
|
3201 *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
|
3202 *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
|
3203 *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
|
3204 *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
|
3205 *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
|
3206 *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
|
3207 *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
|
3208 } |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3209 else |
|
46028
8710ba7fba2b
#1477: ur'\U0010FFFF' raised in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46026
diff
changeset
|
3210 #else |
| 51216 | 3211 /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3212 if (ch >= 0xD800 && ch < 0xDC00) { | |
| 3213 Py_UNICODE ch2; | |
| 3214 Py_UCS4 ucs; | |
| 3215 | |
| 3216 ch2 = *s++; | |
| 3217 size--; | |
| 3218 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { | |
| 3219 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; | |
| 3220 *p++ = '\\'; | |
| 3221 *p++ = 'U'; | |
| 3222 *p++ = hexdigit[(ucs >> 28) & 0xf]; | |
| 3223 *p++ = hexdigit[(ucs >> 24) & 0xf]; | |
| 3224 *p++ = hexdigit[(ucs >> 20) & 0xf]; | |
| 3225 *p++ = hexdigit[(ucs >> 16) & 0xf]; | |
| 3226 *p++ = hexdigit[(ucs >> 12) & 0xf]; | |
| 3227 *p++ = hexdigit[(ucs >> 8) & 0xf]; | |
| 3228 *p++ = hexdigit[(ucs >> 4) & 0xf]; | |
| 3229 *p++ = hexdigit[ucs & 0xf]; | |
| 3230 continue; | |
| 3231 } | |
| 3232 /* Fall through: isolated surrogates are copied as-is */ | |
| 3233 s--; | |
| 3234 size++; | |
| 3235 } | |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3236 #endif |
| 51216 | 3237 /* Map 16-bit characters to '\uxxxx' */ |
| 3238 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
|
3239 *p++ = '\\'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3240 *p++ = 'u'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3241 *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
|
3242 *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
|
3243 *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
|
3244 *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
|
3245 } |
| 51216 | 3246 /* Copy everything else as-is */ |
| 3247 else | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3248 *p++ = (char) ch; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3249 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3250 *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
|
3251 _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
|
3252 return repr; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3253 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3254 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3255 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
|
3256 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3257 if (!PyUnicode_Check(unicode)) { |
| 51216 | 3258 PyErr_BadArgument(); |
| 3259 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3260 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3261 return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 3262 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
|
3263 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3264 |
| 35485 | 3265 /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3266 | |
| 3267 PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, | |
| 51216 | 3268 Py_ssize_t size, |
| 3269 const char *errors) | |
| 35485 | 3270 { |
| 3271 const char *starts = s; | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3272 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3273 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3274 Py_ssize_t outpos; |
| 35485 | 3275 PyUnicodeObject *v; |
| 3276 Py_UNICODE *p; | |
| 3277 const char *end; | |
| 3278 const char *reason; | |
| 3279 PyObject *errorHandler = NULL; | |
| 3280 PyObject *exc = NULL; | |
| 3281 | |
|
36236
ecd423718a32
Fix icc warnings: remove (sometimes) unused variable conditionally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36203
diff
changeset
|
3282 #ifdef Py_UNICODE_WIDE |
|
ecd423718a32
Fix icc warnings: remove (sometimes) unused variable conditionally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36203
diff
changeset
|
3283 Py_UNICODE unimax = PyUnicode_GetMax(); |
|
ecd423718a32
Fix icc warnings: remove (sometimes) unused variable conditionally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36203
diff
changeset
|
3284 #endif |
|
ecd423718a32
Fix icc warnings: remove (sometimes) unused variable conditionally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36203
diff
changeset
|
3285 |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39734
diff
changeset
|
3286 /* XXX overflow detection missing */ |
| 35485 | 3287 v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3288 if (v == NULL) | |
| 51216 | 3289 goto onError; |
| 35485 | 3290 if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 51216 | 3291 return (PyObject *)v; |
| 35485 | 3292 p = PyUnicode_AS_UNICODE(v); |
| 3293 end = s + size; | |
| 3294 | |
| 3295 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
|
3296 memcpy(p, s, sizeof(Py_UNICODE)); |
| 35485 | 3297 /* We have to sanity check the raw data, otherwise doom looms for |
| 3298 some malformed UCS-4 data. */ | |
| 3299 if ( | |
| 51216 | 3300 #ifdef Py_UNICODE_WIDE |
| 35485 | 3301 *p > unimax || *p < 0 || |
| 51216 | 3302 #endif |
| 35485 | 3303 end-s < Py_UNICODE_SIZE |
| 3304 ) | |
| 51216 | 3305 { |
| 35485 | 3306 startinpos = s - starts; |
| 3307 if (end-s < Py_UNICODE_SIZE) { | |
| 3308 endinpos = end-starts; | |
| 3309 reason = "truncated input"; | |
| 3310 } | |
| 3311 else { | |
| 3312 endinpos = s - starts + Py_UNICODE_SIZE; | |
| 3313 reason = "illegal code point (> 0x10FFFF)"; | |
| 3314 } | |
| 3315 outpos = p - PyUnicode_AS_UNICODE(v); | |
| 3316 if (unicode_decode_call_errorhandler( | |
| 3317 errors, &errorHandler, | |
| 3318 "unicode_internal", reason, | |
| 3319 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
|
3320 &v, &outpos, &p)) { |
| 35485 | 3321 goto onError; |
| 3322 } | |
| 3323 } | |
| 3324 else { | |
| 3325 p++; | |
| 3326 s += Py_UNICODE_SIZE; | |
| 3327 } | |
| 3328 } | |
| 3329 | |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
3330 if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 35485 | 3331 goto onError; |
| 3332 Py_XDECREF(errorHandler); | |
| 3333 Py_XDECREF(exc); | |
| 3334 return (PyObject *)v; | |
| 3335 | |
| 51216 | 3336 onError: |
| 35485 | 3337 Py_XDECREF(v); |
| 3338 Py_XDECREF(errorHandler); | |
| 3339 Py_XDECREF(exc); | |
| 3340 return NULL; | |
| 3341 } | |
| 3342 | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3343 /* --- Latin-1 Codec ------------------------------------------------------ */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3344 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3345 PyObject *PyUnicode_DecodeLatin1(const char *s, |
| 51216 | 3346 Py_ssize_t size, |
| 3347 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
|
3348 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3349 PyUnicodeObject *v; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3350 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
|
3351 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3352 /* 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
|
3353 if (size == 1) { |
| 51216 | 3354 Py_UNICODE r = *(unsigned char*)s; |
| 3355 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
|
3356 } |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
3357 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3358 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
|
3359 if (v == NULL) |
| 51216 | 3360 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3361 if (size == 0) |
| 51216 | 3362 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
|
3363 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
|
3364 while (size-- > 0) |
| 51216 | 3365 *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
|
3366 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
|
3367 |
| 51216 | 3368 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3369 Py_XDECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3370 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3371 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3372 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3373 /* 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
|
3374 static void make_encode_exception(PyObject **exceptionObject, |
| 51216 | 3375 const char *encoding, |
| 3376 const Py_UNICODE *unicode, Py_ssize_t size, | |
| 3377 Py_ssize_t startpos, Py_ssize_t endpos, | |
| 3378 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
|
3379 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3380 if (*exceptionObject == NULL) { |
| 51216 | 3381 *exceptionObject = PyUnicodeEncodeError_Create( |
| 3382 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
|
3383 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3384 else { |
| 51216 | 3385 if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 3386 goto onError; | |
| 3387 if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) | |
| 3388 goto onError; | |
| 3389 if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) | |
| 3390 goto onError; | |
| 3391 return; | |
| 3392 onError: | |
| 3393 Py_DECREF(*exceptionObject); | |
| 3394 *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
|
3395 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3396 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3397 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3398 /* 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
|
3399 static void raise_encode_exception(PyObject **exceptionObject, |
| 51216 | 3400 const char *encoding, |
| 3401 const Py_UNICODE *unicode, Py_ssize_t size, | |
| 3402 Py_ssize_t startpos, Py_ssize_t endpos, | |
| 3403 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
|
3404 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3405 make_encode_exception(exceptionObject, |
| 51216 | 3406 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
|
3407 if (*exceptionObject != NULL) |
| 51216 | 3408 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
|
3409 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3410 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3411 /* 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
|
3412 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
|
3413 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
|
3414 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
|
3415 static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 51216 | 3416 PyObject **errorHandler, |
| 3417 const char *encoding, const char *reason, | |
| 3418 const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, | |
| 3419 Py_ssize_t startpos, Py_ssize_t endpos, | |
| 3420 Py_ssize_t *newpos) | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3421 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3422 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
|
3423 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3424 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
|
3425 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
|
3426 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3427 if (*errorHandler == NULL) { |
| 51216 | 3428 *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
|
3429 if (*errorHandler == NULL) |
| 51216 | 3430 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
|
3431 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3432 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3433 make_encode_exception(exceptionObject, |
| 51216 | 3434 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
|
3435 if (*exceptionObject == NULL) |
| 51216 | 3436 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
|
3437 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3438 restuple = PyObject_CallFunctionObjArgs( |
| 51216 | 3439 *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
|
3440 if (restuple == NULL) |
| 51216 | 3441 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
|
3442 if (!PyTuple_Check(restuple)) { |
|
52909
f3b26a729e82
Merged revisions 69578-69580,69901,69907,69994,70022-70023,70025-70026,70166,70273,70275,70342,70386-70387,70389-70390,70392-70393,70395,70397,70400,70418 via svnmerge
Georg Brandl <georg@python.org>
parents:
52411
diff
changeset
|
3443 PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 51216 | 3444 Py_DECREF(restuple); |
| 3445 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
|
3446 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3447 if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 51216 | 3448 &resunicode, newpos)) { |
| 3449 Py_DECREF(restuple); | |
| 3450 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
|
3451 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3452 if (*newpos<0) |
| 51216 | 3453 *newpos = size+*newpos; |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
3454 if (*newpos<0 || *newpos>size) { |
| 51216 | 3455 PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 3456 Py_DECREF(restuple); | |
| 3457 return NULL; | |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
3458 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3459 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
|
3460 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
|
3461 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
|
3462 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3463 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3464 static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
| 51216 | 3465 Py_ssize_t size, |
| 3466 const char *errors, | |
| 3467 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
|
3468 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3469 /* 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
|
3470 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
|
3471 /* 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
|
3472 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
|
3473 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
|
3474 /* 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
|
3475 /* 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
|
3476 /* 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
|
3477 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
|
3478 /* current output position */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3479 Py_ssize_t respos = 0; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3480 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
|
3481 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
|
3482 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
|
3483 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
|
3484 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
|
3485 /* 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
|
3486 * -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
|
3487 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
|
3488 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3489 /* 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
|
3490 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
|
3491 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
|
3492 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
|
3493 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
|
3494 if (size == 0) |
| 51216 | 3495 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
|
3496 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
|
3497 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
|
3498 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3499 while (p<endp) { |
| 51216 | 3500 Py_UNICODE c = *p; |
| 3501 | |
| 3502 /* can we encode this? */ | |
| 3503 if (c<limit) { | |
| 3504 /* no overflow check, because we know that the space is enough */ | |
| 3505 *str++ = (char)c; | |
| 3506 ++p; | |
| 3507 } | |
| 3508 else { | |
| 3509 Py_ssize_t unicodepos = p-startp; | |
| 3510 Py_ssize_t requiredsize; | |
| 3511 PyObject *repunicode; | |
| 3512 Py_ssize_t repsize; | |
| 3513 Py_ssize_t newpos; | |
| 3514 Py_ssize_t respos; | |
| 3515 Py_UNICODE *uni2; | |
| 3516 /* startpos for collecting unencodable chars */ | |
| 3517 const Py_UNICODE *collstart = p; | |
| 3518 const Py_UNICODE *collend = p; | |
| 3519 /* find all unecodable characters */ | |
| 3520 while ((collend < endp) && ((*collend)>=limit)) | |
| 3521 ++collend; | |
| 3522 /* cache callback name lookup (if not done yet, i.e. it's the first error) */ | |
| 3523 if (known_errorHandler==-1) { | |
| 3524 if ((errors==NULL) || (!strcmp(errors, "strict"))) | |
| 3525 known_errorHandler = 1; | |
| 3526 else if (!strcmp(errors, "replace")) | |
| 3527 known_errorHandler = 2; | |
| 3528 else if (!strcmp(errors, "ignore")) | |
| 3529 known_errorHandler = 3; | |
| 3530 else if (!strcmp(errors, "xmlcharrefreplace")) | |
| 3531 known_errorHandler = 4; | |
| 3532 else | |
| 3533 known_errorHandler = 0; | |
| 3534 } | |
| 3535 switch (known_errorHandler) { | |
| 3536 case 1: /* strict */ | |
| 3537 raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); | |
| 3538 goto onError; | |
| 3539 case 2: /* replace */ | |
| 3540 while (collstart++<collend) | |
| 3541 *str++ = '?'; /* fall through */ | |
| 3542 case 3: /* ignore */ | |
| 3543 p = collend; | |
| 3544 break; | |
| 3545 case 4: /* xmlcharrefreplace */ | |
| 3546 respos = str-PyString_AS_STRING(res); | |
| 3547 /* determine replacement size (temporarily (mis)uses p) */ | |
| 3548 for (p = collstart, repsize = 0; p < collend; ++p) { | |
| 3549 if (*p<10) | |
| 3550 repsize += 2+1+1; | |
| 3551 else if (*p<100) | |
| 3552 repsize += 2+2+1; | |
| 3553 else if (*p<1000) | |
| 3554 repsize += 2+3+1; | |
| 3555 else if (*p<10000) | |
| 3556 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
|
3557 #ifndef Py_UNICODE_WIDE |
| 51216 | 3558 else |
| 3559 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
|
3560 #else |
| 51216 | 3561 else if (*p<100000) |
| 3562 repsize += 2+5+1; | |
| 3563 else if (*p<1000000) | |
| 3564 repsize += 2+6+1; | |
| 3565 else | |
| 3566 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
|
3567 #endif |
| 51216 | 3568 } |
| 3569 requiredsize = respos+repsize+(endp-collend); | |
| 3570 if (requiredsize > ressize) { | |
| 3571 if (requiredsize<2*ressize) | |
| 3572 requiredsize = 2*ressize; | |
| 3573 if (_PyString_Resize(&res, requiredsize)) | |
| 3574 goto onError; | |
| 3575 str = PyString_AS_STRING(res) + respos; | |
| 3576 ressize = requiredsize; | |
| 3577 } | |
| 3578 /* generate replacement (temporarily (mis)uses p) */ | |
| 3579 for (p = collstart; p < collend; ++p) { | |
| 3580 str += sprintf(str, "&#%d;", (int)*p); | |
| 3581 } | |
| 3582 p = collend; | |
| 3583 break; | |
| 3584 default: | |
| 3585 repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, | |
| 3586 encoding, reason, startp, size, &exc, | |
| 3587 collstart-startp, collend-startp, &newpos); | |
| 3588 if (repunicode == NULL) | |
| 3589 goto onError; | |
| 3590 /* need more space? (at least enough for what we | |
| 3591 have+the replacement+the rest of the string, so | |
| 3592 we won't have to check space for encodable characters) */ | |
| 3593 respos = str-PyString_AS_STRING(res); | |
| 3594 repsize = PyUnicode_GET_SIZE(repunicode); | |
| 3595 requiredsize = respos+repsize+(endp-collend); | |
| 3596 if (requiredsize > ressize) { | |
| 3597 if (requiredsize<2*ressize) | |
| 3598 requiredsize = 2*ressize; | |
| 3599 if (_PyString_Resize(&res, requiredsize)) { | |
| 3600 Py_DECREF(repunicode); | |
| 3601 goto onError; | |
| 3602 } | |
| 3603 str = PyString_AS_STRING(res) + respos; | |
| 3604 ressize = requiredsize; | |
| 3605 } | |
| 3606 /* check if there is anything unencodable in the replacement | |
| 3607 and copy it to the output */ | |
| 3608 for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { | |
| 3609 c = *uni2; | |
| 3610 if (c >= limit) { | |
| 3611 raise_encode_exception(&exc, encoding, startp, size, | |
| 3612 unicodepos, unicodepos+1, reason); | |
| 3613 Py_DECREF(repunicode); | |
| 3614 goto onError; | |
| 3615 } | |
| 3616 *str = (char)c; | |
| 3617 } | |
| 3618 p = startp + newpos; | |
| 3619 Py_DECREF(repunicode); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
3620 } |
| 51216 | 3621 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3622 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3623 /* 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
|
3624 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
|
3625 if (respos<ressize) |
| 51216 | 3626 /* If this falls res will be NULL */ |
| 3627 _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
|
3628 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
|
3629 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
|
3630 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
|
3631 |
| 51216 | 3632 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
|
3633 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
|
3634 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
|
3635 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
|
3636 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3637 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3638 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3639 PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
| 51216 | 3640 Py_ssize_t size, |
| 3641 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
|
3642 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3643 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
|
3644 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3645 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3646 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
|
3647 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3648 if (!PyUnicode_Check(unicode)) { |
| 51216 | 3649 PyErr_BadArgument(); |
| 3650 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3651 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3652 return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 3653 PyUnicode_GET_SIZE(unicode), |
| 3654 NULL); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3655 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3656 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3657 /* --- 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
|
3658 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3659 PyObject *PyUnicode_DecodeASCII(const char *s, |
| 51216 | 3660 Py_ssize_t size, |
| 3661 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
|
3662 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3663 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
|
3664 PyUnicodeObject *v; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3665 Py_UNICODE *p; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3666 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3667 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3668 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
|
3669 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
|
3670 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
|
3671 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
|
3672 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3673 /* 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
|
3674 if (size == 1 && *(unsigned char*)s < 128) { |
| 51216 | 3675 Py_UNICODE r = *(unsigned char*)s; |
| 3676 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
|
3677 } |
|
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
|
3678 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3679 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
|
3680 if (v == NULL) |
| 51216 | 3681 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3682 if (size == 0) |
| 51216 | 3683 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
|
3684 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
|
3685 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
|
3686 while (s < e) { |
| 51216 | 3687 register unsigned char c = (unsigned char)*s; |
| 3688 if (c < 128) { | |
| 3689 *p++ = c; | |
| 3690 ++s; | |
| 3691 } | |
| 3692 else { | |
| 3693 startinpos = s-starts; | |
| 3694 endinpos = startinpos + 1; | |
| 3695 outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); | |
| 3696 if (unicode_decode_call_errorhandler( | |
| 3697 errors, &errorHandler, | |
| 3698 "ascii", "ordinal not in range(128)", | |
| 3699 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 3700 &v, &outpos, &p)) | |
| 3701 goto onError; | |
| 3702 } | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3703 } |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
3704 if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) |
| 51216 | 3705 if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 3706 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
|
3707 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
|
3708 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
|
3709 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
|
3710 |
| 51216 | 3711 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3712 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
|
3713 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
|
3714 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
|
3715 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3716 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3717 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3718 PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 51216 | 3719 Py_ssize_t size, |
| 3720 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
|
3721 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3722 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
|
3723 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3724 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3725 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
|
3726 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3727 if (!PyUnicode_Check(unicode)) { |
| 51216 | 3728 PyErr_BadArgument(); |
| 3729 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3730 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3731 return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 3732 PyUnicode_GET_SIZE(unicode), |
| 3733 NULL); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3734 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3735 |
|
24071
8142f8d668a0
Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents:
23837
diff
changeset
|
3736 #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 12193 | 3737 |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3738 /* --- MBCS codecs for Windows -------------------------------------------- */ |
| 12193 | 3739 |
|
52254
b8934dd9267d
Merged revisions 70499 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
51216
diff
changeset
|
3740 #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
|
3741 #define NEED_RETRY |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3742 #endif |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3743 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3744 /* 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
|
3745 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
|
3746 b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
| 51216 | 3747 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
|
3748 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3749 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
|
3750 { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3751 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
|
3752 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3753 if (IsDBCSLeadByte(*curr)) { |
| 51216 | 3754 const char *prev = CharPrev(s, curr); |
| 3755 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
|
3756 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3757 return 0; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3758 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3759 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3760 /* |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3761 * 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
|
3762 * 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
|
3763 */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3764 static int decode_mbcs(PyUnicodeObject **v, |
| 51216 | 3765 const char *s, /* MBCS string */ |
| 3766 int size, /* sizeof MBCS string */ | |
| 3767 int final) | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3768 { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3769 Py_UNICODE *p; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3770 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
|
3771 int usize = 0; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3772 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3773 assert(size >= 0); |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3774 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3775 /* 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
|
3776 if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
| 51216 | 3777 --size; |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3778 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3779 /* 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
|
3780 if (size > 0) { |
| 51216 | 3781 usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 3782 if (usize == 0) { | |
| 3783 PyErr_SetFromWindowsErrWithFilename(0, NULL); | |
| 3784 return -1; | |
| 3785 } | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3786 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3787 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3788 if (*v == NULL) { |
| 51216 | 3789 /* Create unicode object */ |
| 3790 *v = _PyUnicode_New(usize); | |
| 3791 if (*v == NULL) | |
| 3792 return -1; | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3793 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3794 else { |
| 51216 | 3795 /* Extend unicode object */ |
| 3796 n = PyUnicode_GET_SIZE(*v); | |
| 3797 if (_PyUnicode_Resize(v, n + usize) < 0) | |
| 3798 return -1; | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3799 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3800 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3801 /* Do the conversion */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3802 if (size > 0) { |
| 51216 | 3803 p = PyUnicode_AS_UNICODE(*v) + n; |
| 3804 if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { | |
| 3805 PyErr_SetFromWindowsErrWithFilename(0, NULL); | |
| 3806 return -1; | |
| 3807 } | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3808 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3809 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3810 return size; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3811 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3812 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3813 PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
| 51216 | 3814 Py_ssize_t size, |
| 3815 const char *errors, | |
| 3816 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
|
3817 { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3818 PyUnicodeObject *v = NULL; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3819 int done; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3820 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3821 if (consumed) |
| 51216 | 3822 *consumed = 0; |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3823 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3824 #ifdef NEED_RETRY |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3825 retry: |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3826 if (size > INT_MAX) |
| 51216 | 3827 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
|
3828 else |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3829 #endif |
| 51216 | 3830 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
|
3831 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3832 if (done < 0) { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3833 Py_XDECREF(v); |
| 51216 | 3834 return NULL; |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3835 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3836 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3837 if (consumed) |
| 51216 | 3838 *consumed += done; |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3839 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3840 #ifdef NEED_RETRY |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3841 if (size > INT_MAX) { |
| 51216 | 3842 s += done; |
| 3843 size -= done; | |
| 3844 goto retry; | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3845 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3846 #endif |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3847 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3848 return (PyObject *)v; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3849 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3850 |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3851 PyObject *PyUnicode_DecodeMBCS(const char *s, |
| 51216 | 3852 Py_ssize_t size, |
| 3853 const char *errors) | |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3854 { |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3855 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
|
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 * 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
|
3860 * 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
|
3861 */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3862 static int encode_mbcs(PyObject **repr, |
| 51216 | 3863 const Py_UNICODE *p, /* unicode */ |
| 3864 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
|
3865 { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3866 int mbcssize = 0; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3867 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
|
3868 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3869 assert(size >= 0); |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3870 |
|
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3871 /* 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
|
3872 if (size > 0) { |
| 51216 | 3873 mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 3874 if (mbcssize == 0) { | |
| 3875 PyErr_SetFromWindowsErrWithFilename(0, NULL); | |
| 3876 return -1; | |
| 3877 } | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3878 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3879 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3880 if (*repr == NULL) { |
| 51216 | 3881 /* Create string object */ |
| 3882 *repr = PyString_FromStringAndSize(NULL, mbcssize); | |
| 3883 if (*repr == NULL) | |
| 3884 return -1; | |
|
38817
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 else { |
| 51216 | 3887 /* Extend string object */ |
| 3888 n = PyString_Size(*repr); | |
| 3889 if (_PyString_Resize(repr, n + mbcssize) < 0) | |
| 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 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3893 /* Do the conversion */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3894 if (size > 0) { |
| 51216 | 3895 char *s = PyString_AS_STRING(*repr) + n; |
| 3896 if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { | |
| 3897 PyErr_SetFromWindowsErrWithFilename(0, NULL); | |
| 3898 return -1; | |
| 3899 } | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3900 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3901 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3902 return 0; |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3903 } |
|
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3904 |
|
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3905 PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 51216 | 3906 Py_ssize_t size, |
| 3907 const char *errors) | |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3908 { |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3909 PyObject *repr = NULL; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3910 int ret; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3911 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3912 #ifdef NEED_RETRY |
| 51216 | 3913 retry: |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3914 if (size > INT_MAX) |
| 51216 | 3915 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
|
3916 else |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3917 #endif |
| 51216 | 3918 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
|
3919 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3920 if (ret < 0) { |
| 51216 | 3921 Py_XDECREF(repr); |
| 3922 return NULL; | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3923 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3924 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3925 #ifdef NEED_RETRY |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3926 if (size > INT_MAX) { |
| 51216 | 3927 p += INT_MAX; |
| 3928 size -= INT_MAX; | |
| 3929 goto retry; | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3930 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3931 #endif |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3932 |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3933 return repr; |
|
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3934 } |
| 12193 | 3935 |
|
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
|
3936 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
|
3937 { |
|
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
|
3938 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
|
3939 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
|
3940 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
|
3941 } |
|
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
|
3942 return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 3943 PyUnicode_GET_SIZE(unicode), |
| 3944 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
|
3945 } |
|
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
|
3946 |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3947 #undef NEED_RETRY |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3948 |
|
24071
8142f8d668a0
Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents:
23837
diff
changeset
|
3949 #endif /* MS_WINDOWS */ |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3950 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3951 /* --- Character Mapping Codec -------------------------------------------- */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3952 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3953 PyObject *PyUnicode_DecodeCharmap(const char *s, |
| 51216 | 3954 Py_ssize_t size, |
| 3955 PyObject *mapping, | |
| 3956 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
|
3957 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3958 const char *starts = s; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3959 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3960 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3961 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
|
3962 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
|
3963 PyUnicodeObject *v; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3964 Py_UNICODE *p; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3965 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
|
3966 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
|
3967 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
|
3968 Py_UNICODE *mapstring = NULL; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3969 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
|
3970 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3971 /* 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
|
3972 if (mapping == NULL) |
| 51216 | 3973 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
|
3974 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3975 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
|
3976 if (v == NULL) |
| 51216 | 3977 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3978 if (size == 0) |
| 51216 | 3979 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
|
3980 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
|
3981 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
|
3982 if (PyUnicode_CheckExact(mapping)) { |
| 51216 | 3983 mapstring = PyUnicode_AS_UNICODE(mapping); |
| 3984 maplen = PyUnicode_GET_SIZE(mapping); | |
| 3985 while (s < e) { | |
| 3986 unsigned char ch = *s; | |
| 3987 Py_UNICODE x = 0xfffe; /* illegal value */ | |
| 3988 | |
| 3989 if (ch < maplen) | |
| 3990 x = mapstring[ch]; | |
| 3991 | |
| 3992 if (x == 0xfffe) { | |
| 3993 /* undefined mapping */ | |
| 3994 outpos = p-PyUnicode_AS_UNICODE(v); | |
| 3995 startinpos = s-starts; | |
| 3996 endinpos = startinpos+1; | |
| 3997 if (unicode_decode_call_errorhandler( | |
| 3998 errors, &errorHandler, | |
| 3999 "charmap", "character maps to <undefined>", | |
| 4000 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 4001 &v, &outpos, &p)) { | |
| 4002 goto onError; | |
| 4003 } | |
| 4004 continue; | |
| 4005 } | |
| 4006 *p++ = x; | |
| 4007 ++s; | |
| 4008 } | |
|
35702
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
4009 } |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
4010 else { |
| 51216 | 4011 while (s < e) { |
| 4012 unsigned char ch = *s; | |
| 4013 PyObject *w, *x; | |
| 4014 | |
| 4015 /* Get mapping (char ordinal -> integer, Unicode char or None) */ | |
| 4016 w = PyInt_FromLong((long)ch); | |
| 4017 if (w == NULL) | |
| 4018 goto onError; | |
| 4019 x = PyObject_GetItem(mapping, w); | |
| 4020 Py_DECREF(w); | |
| 4021 if (x == NULL) { | |
| 4022 if (PyErr_ExceptionMatches(PyExc_LookupError)) { | |
| 4023 /* No mapping found means: mapping is undefined. */ | |
| 4024 PyErr_Clear(); | |
| 4025 x = Py_None; | |
| 4026 Py_INCREF(x); | |
| 4027 } else | |
| 4028 goto onError; | |
| 4029 } | |
| 4030 | |
| 4031 /* Apply mapping */ | |
| 4032 if (PyInt_Check(x)) { | |
| 4033 long value = PyInt_AS_LONG(x); | |
| 4034 if (value < 0 || value > 65535) { | |
| 4035 PyErr_SetString(PyExc_TypeError, | |
| 4036 "character mapping must be in range(65536)"); | |
| 4037 Py_DECREF(x); | |
| 4038 goto onError; | |
| 4039 } | |
| 4040 *p++ = (Py_UNICODE)value; | |
| 4041 } | |
| 4042 else if (x == Py_None) { | |
| 4043 /* undefined mapping */ | |
| 4044 outpos = p-PyUnicode_AS_UNICODE(v); | |
| 4045 startinpos = s-starts; | |
| 4046 endinpos = startinpos+1; | |
| 4047 if (unicode_decode_call_errorhandler( | |
| 4048 errors, &errorHandler, | |
| 4049 "charmap", "character maps to <undefined>", | |
| 4050 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 4051 &v, &outpos, &p)) { | |
| 4052 Py_DECREF(x); | |
| 4053 goto onError; | |
| 4054 } | |
| 4055 Py_DECREF(x); | |
| 4056 continue; | |
| 4057 } | |
| 4058 else if (PyUnicode_Check(x)) { | |
| 4059 Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); | |
| 4060 | |
| 4061 if (targetsize == 1) | |
| 4062 /* 1-1 mapping */ | |
| 4063 *p++ = *PyUnicode_AS_UNICODE(x); | |
| 4064 | |
| 4065 else if (targetsize > 1) { | |
| 4066 /* 1-n mapping */ | |
| 4067 if (targetsize > extrachars) { | |
| 4068 /* resize first */ | |
| 4069 Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); | |
| 4070 Py_ssize_t needed = (targetsize - extrachars) + \ | |
| 4071 (targetsize << 2); | |
| 4072 extrachars += needed; | |
| 4073 /* XXX overflow detection missing */ | |
| 4074 if (_PyUnicode_Resize(&v, | |
| 4075 PyUnicode_GET_SIZE(v) + needed) < 0) { | |
| 4076 Py_DECREF(x); | |
| 4077 goto onError; | |
| 4078 } | |
| 4079 p = PyUnicode_AS_UNICODE(v) + oldpos; | |
| 4080 } | |
| 4081 Py_UNICODE_COPY(p, | |
| 4082 PyUnicode_AS_UNICODE(x), | |
| 4083 targetsize); | |
| 4084 p += targetsize; | |
| 4085 extrachars -= targetsize; | |
| 4086 } | |
| 4087 /* 1-0 mapping: skip the character */ | |
| 4088 } | |
| 4089 else { | |
| 4090 /* wrong return value */ | |
| 4091 PyErr_SetString(PyExc_TypeError, | |
| 4092 "character mapping must return integer, None or unicode"); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4093 Py_DECREF(x); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4094 goto onError; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4095 } |
| 51216 | 4096 Py_DECREF(x); |
| 4097 ++s; | |
| 4098 } | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4099 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4100 if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
| 51216 | 4101 if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4102 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
|
4103 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
|
4104 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
|
4105 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
|
4106 |
| 51216 | 4107 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
|
4108 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
|
4109 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
|
4110 Py_XDECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4111 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4112 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4113 |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4114 /* Charmap encoding: the lookup table */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4115 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4116 struct encoding_map{ |
| 51216 | 4117 PyObject_HEAD |
| 4118 unsigned char level1[32]; | |
| 4119 int count2, count3; | |
| 4120 unsigned char level23[1]; | |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4121 }; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4122 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4123 static PyObject* |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4124 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
|
4125 { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4126 struct encoding_map *map = (struct encoding_map*)obj; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4127 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
|
4128 128*map->count3); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4129 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4130 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4131 static PyMethodDef encoding_map_methods[] = { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4132 {"size", encoding_map_size, METH_NOARGS, |
| 51216 | 4133 PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4134 { 0 } | |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4135 }; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4136 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4137 static void |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4138 encoding_map_dealloc(PyObject* o) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4139 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4140 PyObject_FREE(o); |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4141 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4142 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4143 static PyTypeObject EncodingMapType = { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4144 PyVarObject_HEAD_INIT(NULL, 0) |
| 51216 | 4145 "EncodingMap", /*tp_name*/ |
| 4146 sizeof(struct encoding_map), /*tp_basicsize*/ | |
| 4147 0, /*tp_itemsize*/ | |
| 4148 /* methods */ | |
| 4149 encoding_map_dealloc, /*tp_dealloc*/ | |
| 4150 0, /*tp_print*/ | |
| 4151 0, /*tp_getattr*/ | |
| 4152 0, /*tp_setattr*/ | |
| 4153 0, /*tp_compare*/ | |
| 4154 0, /*tp_repr*/ | |
| 4155 0, /*tp_as_number*/ | |
| 4156 0, /*tp_as_sequence*/ | |
| 4157 0, /*tp_as_mapping*/ | |
| 4158 0, /*tp_hash*/ | |
| 4159 0, /*tp_call*/ | |
| 4160 0, /*tp_str*/ | |
| 4161 0, /*tp_getattro*/ | |
| 4162 0, /*tp_setattro*/ | |
| 4163 0, /*tp_as_buffer*/ | |
| 4164 Py_TPFLAGS_DEFAULT, /*tp_flags*/ | |
| 4165 0, /*tp_doc*/ | |
| 4166 0, /*tp_traverse*/ | |
| 4167 0, /*tp_clear*/ | |
| 4168 0, /*tp_richcompare*/ | |
| 4169 0, /*tp_weaklistoffset*/ | |
| 4170 0, /*tp_iter*/ | |
| 4171 0, /*tp_iternext*/ | |
| 4172 encoding_map_methods, /*tp_methods*/ | |
| 4173 0, /*tp_members*/ | |
| 4174 0, /*tp_getset*/ | |
| 4175 0, /*tp_base*/ | |
| 4176 0, /*tp_dict*/ | |
| 4177 0, /*tp_descr_get*/ | |
| 4178 0, /*tp_descr_set*/ | |
| 4179 0, /*tp_dictoffset*/ | |
| 4180 0, /*tp_init*/ | |
| 4181 0, /*tp_alloc*/ | |
| 4182 0, /*tp_new*/ | |
| 4183 0, /*tp_free*/ | |
| 4184 0, /*tp_is_gc*/ | |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4185 }; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4186 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4187 PyObject* |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4188 PyUnicode_BuildEncodingMap(PyObject* string) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4189 { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4190 Py_UNICODE *decode; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4191 PyObject *result; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4192 struct encoding_map *mresult; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4193 int i; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4194 int need_dict = 0; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4195 unsigned char level1[32]; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4196 unsigned char level2[512]; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4197 unsigned char *mlevel1, *mlevel2, *mlevel3; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4198 int count2 = 0, count3 = 0; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4199 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4200 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
|
4201 PyErr_BadArgument(); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4202 return NULL; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4203 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4204 decode = PyUnicode_AS_UNICODE(string); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4205 memset(level1, 0xFF, sizeof level1); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4206 memset(level2, 0xFF, sizeof level2); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4207 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4208 /* 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
|
4209 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
|
4210 a mapping dictionary. */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4211 if (decode[0] != 0) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4212 need_dict = 1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4213 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
|
4214 int l1, l2; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4215 if (decode[i] == 0 |
| 51216 | 4216 #ifdef Py_UNICODE_WIDE |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4217 || decode[i] > 0xFFFF |
| 51216 | 4218 #endif |
| 4219 ) { | |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4220 need_dict = 1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4221 break; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4222 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4223 if (decode[i] == 0xFFFE) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4224 /* unmapped character */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4225 continue; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4226 l1 = decode[i] >> 11; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4227 l2 = decode[i] >> 7; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4228 if (level1[l1] == 0xFF) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4229 level1[l1] = count2++; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4230 if (level2[l2] == 0xFF) |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4231 level2[l2] = count3++; |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4232 } |
|
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 if (count2 >= 0xFF || count3 >= 0xFF) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4235 need_dict = 1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4236 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4237 if (need_dict) { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4238 PyObject *result = PyDict_New(); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4239 PyObject *key, *value; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4240 if (!result) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4241 return NULL; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4242 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
|
4243 key = value = NULL; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4244 key = PyInt_FromLong(decode[i]); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4245 value = PyInt_FromLong(i); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4246 if (!key || !value) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4247 goto failed1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4248 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
|
4249 goto failed1; |
|
38587
21969b71c36d
Repair refleaks in unicodeobject.
Georg Brandl <georg@python.org>
parents:
38586
diff
changeset
|
4250 Py_DECREF(key); |
|
21969b71c36d
Repair refleaks in unicodeobject.
Georg Brandl <georg@python.org>
parents:
38586
diff
changeset
|
4251 Py_DECREF(value); |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4252 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4253 return result; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4254 failed1: |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4255 Py_XDECREF(key); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4256 Py_XDECREF(value); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4257 Py_DECREF(result); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4258 return NULL; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4259 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4260 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4261 /* Create a three-level trie */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4262 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
|
4263 16*count2 + 128*count3 - 1); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4264 if (!result) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4265 return PyErr_NoMemory(); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4266 PyObject_Init(result, &EncodingMapType); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4267 mresult = (struct encoding_map*)result; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4268 mresult->count2 = count2; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4269 mresult->count3 = count3; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4270 mlevel1 = mresult->level1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4271 mlevel2 = mresult->level23; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4272 mlevel3 = mresult->level23 + 16*count2; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4273 memcpy(mlevel1, level1, 32); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4274 memset(mlevel2, 0xFF, 16*count2); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4275 memset(mlevel3, 0, 128*count3); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4276 count3 = 0; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4277 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
|
4278 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
|
4279 if (decode[i] == 0xFFFE) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4280 /* unmapped character */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4281 continue; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4282 o1 = decode[i]>>11; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4283 o2 = (decode[i]>>7) & 0xF; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4284 i2 = 16*mlevel1[o1] + o2; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4285 if (mlevel2[i2] == 0xFF) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4286 mlevel2[i2] = count3++; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4287 o3 = decode[i] & 0x7F; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4288 i3 = 128*mlevel2[i2] + o3; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4289 mlevel3[i3] = i; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4290 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4291 return result; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4292 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4293 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4294 static int |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4295 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
|
4296 { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4297 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
|
4298 int l1 = c>>11; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4299 int l2 = (c>>7) & 0xF; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4300 int l3 = c & 0x7F; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4301 int i; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4302 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4303 #ifdef Py_UNICODE_WIDE |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4304 if (c > 0xFFFF) { |
| 51216 | 4305 return -1; |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4306 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4307 #endif |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4308 if (c == 0) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4309 return 0; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4310 /* level 1*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4311 i = map->level1[l1]; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4312 if (i == 0xFF) { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4313 return -1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4314 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4315 /* level 2*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4316 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
|
4317 if (i == 0xFF) { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4318 return -1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4319 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4320 /* level 3 */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4321 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
|
4322 if (i == 0) { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4323 return -1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4324 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4325 return i; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4326 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4327 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4328 /* 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
|
4329 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
|
4330 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
|
4331 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
|
4332 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4333 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
|
4334 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
|
4335 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4336 if (w == NULL) |
| 51216 | 4337 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
|
4338 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
|
4339 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
|
4340 if (x == NULL) { |
| 51216 | 4341 if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4342 /* No mapping found means: mapping is undefined. */ | |
| 4343 PyErr_Clear(); | |
| 4344 x = Py_None; | |
| 4345 Py_INCREF(x); | |
| 4346 return x; | |
| 4347 } else | |
| 4348 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
|
4349 } |
|
27175
47b134bb51c3
Fix charmapencode_lookup(), so that a None value in the mapping
Walter Dörwald <walter@livinglogic.de>
parents:
27170
diff
changeset
|
4350 else if (x == Py_None) |
| 51216 | 4351 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
|
4352 else if (PyInt_Check(x)) { |
| 51216 | 4353 long value = PyInt_AS_LONG(x); |
| 4354 if (value < 0 || value > 255) { | |
| 4355 PyErr_SetString(PyExc_TypeError, | |
| 4356 "character mapping must be in range(256)"); | |
| 4357 Py_DECREF(x); | |
| 4358 return NULL; | |
| 4359 } | |
| 4360 return x; | |
| 4361 } | |
| 4362 else if (PyString_Check(x)) | |
| 4363 return x; | |
| 4364 else { | |
| 4365 /* wrong return value */ | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4366 PyErr_SetString(PyExc_TypeError, |
| 51216 | 4367 "character mapping must return integer, None or str"); |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4368 Py_DECREF(x); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4369 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4370 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4371 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4372 |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4373 static int |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4374 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
|
4375 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4376 Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4377 /* exponentially overallocate to minimize reallocations */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4378 if (requiredsize < 2*outsize) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4379 requiredsize = 2*outsize; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4380 if (_PyString_Resize(outobj, requiredsize)) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4381 return 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4382 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4383 return 1; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4384 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4385 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4386 typedef enum charmapencode_result { |
| 51216 | 4387 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
|
4388 }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
|
4389 /* 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
|
4390 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
|
4391 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
|
4392 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
|
4393 (in which case no character was written) or NULL, if a |
| 35827 | 4394 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
|
4395 static |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4396 charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 51216 | 4397 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
|
4398 { |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4399 PyObject *rep; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4400 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
|
4401 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
|
4402 |
|
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
|
4403 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
|
4404 int res = encoding_map_lookup(c, mapping); |
| 51216 | 4405 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
|
4406 if (res == -1) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4407 return enc_FAILED; |
| 51216 | 4408 if (outsize<requiredsize) |
| 4409 if (!charmapencode_resize(outobj, outpos, requiredsize)) | |
| 4410 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
|
4411 outstart = PyString_AS_STRING(*outobj); |
| 51216 | 4412 outstart[(*outpos)++] = (char)res; |
| 4413 return enc_SUCCESS; | |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4414 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4415 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4416 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
|
4417 if (rep==NULL) |
| 51216 | 4418 return enc_EXCEPTION; |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4419 else if (rep==Py_None) { |
| 51216 | 4420 Py_DECREF(rep); |
| 4421 return enc_FAILED; | |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4422 } else { |
| 51216 | 4423 if (PyInt_Check(rep)) { |
| 4424 Py_ssize_t requiredsize = *outpos+1; | |
| 4425 if (outsize<requiredsize) | |
| 4426 if (!charmapencode_resize(outobj, outpos, requiredsize)) { | |
| 4427 Py_DECREF(rep); | |
| 4428 return enc_EXCEPTION; | |
| 4429 } | |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
4430 outstart = PyString_AS_STRING(*outobj); |
| 51216 | 4431 outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 4432 } | |
| 4433 else { | |
| 4434 const char *repchars = PyString_AS_STRING(rep); | |
| 4435 Py_ssize_t repsize = PyString_GET_SIZE(rep); | |
| 4436 Py_ssize_t requiredsize = *outpos+repsize; | |
| 4437 if (outsize<requiredsize) | |
| 4438 if (!charmapencode_resize(outobj, outpos, requiredsize)) { | |
| 4439 Py_DECREF(rep); | |
| 4440 return enc_EXCEPTION; | |
| 4441 } | |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
4442 outstart = PyString_AS_STRING(*outobj); |
| 51216 | 4443 memcpy(outstart + *outpos, repchars, repsize); |
| 4444 *outpos += repsize; | |
| 4445 } | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4446 } |
|
38587
21969b71c36d
Repair refleaks in unicodeobject.
Georg Brandl <georg@python.org>
parents:
38586
diff
changeset
|
4447 Py_DECREF(rep); |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4448 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
|
4449 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4450 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4451 /* 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
|
4452 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
|
4453 static |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4454 int charmap_encoding_error( |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4455 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
|
4456 PyObject **exceptionObject, |
|
30149
59bad7efff02
Fix refcount leak in PyUnicode_EncodeCharmap(). The bug surfaces
Walter Dörwald <walter@livinglogic.de>
parents:
29706
diff
changeset
|
4457 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
|
4458 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
|
4459 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4460 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
|
4461 Py_ssize_t repsize; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4462 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
|
4463 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
|
4464 /* startpos for collecting unencodable chars */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4465 Py_ssize_t collstartpos = *inpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4466 Py_ssize_t collendpos = *inpos+1; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4467 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
|
4468 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
|
4469 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
|
4470 charmapencode_result x; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4471 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4472 /* 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
|
4473 while (collendpos < size) { |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4474 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
|
4475 if (Py_TYPE(mapping) == &EncodingMapType) { |
| 51216 | 4476 int res = encoding_map_lookup(p[collendpos], mapping); |
| 4477 if (res != -1) | |
| 4478 break; | |
| 4479 ++collendpos; | |
| 4480 continue; | |
| 4481 } | |
| 4482 | |
| 4483 rep = charmapencode_lookup(p[collendpos], mapping); | |
| 4484 if (rep==NULL) | |
| 4485 return -1; | |
| 4486 else if (rep!=Py_None) { | |
| 4487 Py_DECREF(rep); | |
| 4488 break; | |
| 4489 } | |
| 4490 Py_DECREF(rep); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4491 ++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
|
4492 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4493 /* 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
|
4494 * (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
|
4495 if (*known_errorHandler==-1) { |
| 51216 | 4496 if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4497 *known_errorHandler = 1; | |
| 4498 else if (!strcmp(errors, "replace")) | |
| 4499 *known_errorHandler = 2; | |
| 4500 else if (!strcmp(errors, "ignore")) | |
| 4501 *known_errorHandler = 3; | |
| 4502 else if (!strcmp(errors, "xmlcharrefreplace")) | |
| 4503 *known_errorHandler = 4; | |
| 4504 else | |
| 4505 *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
|
4506 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4507 switch (*known_errorHandler) { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4508 case 1: /* strict */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4509 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
|
4510 return -1; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4511 case 2: /* replace */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4512 for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 51216 | 4513 x = charmapencode_output('?', mapping, res, respos); |
| 4514 if (x==enc_EXCEPTION) { | |
| 4515 return -1; | |
| 4516 } | |
| 4517 else if (x==enc_FAILED) { | |
| 4518 raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | |
| 4519 return -1; | |
| 4520 } | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4521 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4522 /* fall through */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4523 case 3: /* ignore */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4524 *inpos = collendpos; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4525 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4526 case 4: /* xmlcharrefreplace */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4527 /* generate replacement (temporarily (mis)uses p) */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4528 for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 51216 | 4529 char buffer[2+29+1+1]; |
| 4530 char *cp; | |
| 4531 sprintf(buffer, "&#%d;", (int)p[collpos]); | |
| 4532 for (cp = buffer; *cp; ++cp) { | |
| 4533 x = charmapencode_output(*cp, mapping, res, respos); | |
| 4534 if (x==enc_EXCEPTION) | |
| 4535 return -1; | |
| 4536 else if (x==enc_FAILED) { | |
| 4537 raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | |
| 4538 return -1; | |
| 4539 } | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4540 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4541 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4542 *inpos = collendpos; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4543 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4544 default: |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4545 repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
| 51216 | 4546 encoding, reason, p, size, exceptionObject, |
| 4547 collstartpos, collendpos, &newpos); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4548 if (repunicode == NULL) |
| 51216 | 4549 return -1; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4550 /* generate replacement */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4551 repsize = PyUnicode_GET_SIZE(repunicode); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4552 for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 51216 | 4553 x = charmapencode_output(*uni2, mapping, res, respos); |
| 4554 if (x==enc_EXCEPTION) { | |
| 4555 return -1; | |
| 4556 } | |
| 4557 else if (x==enc_FAILED) { | |
| 4558 Py_DECREF(repunicode); | |
| 4559 raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | |
| 4560 return -1; | |
| 4561 } | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4562 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4563 *inpos = newpos; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4564 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
|
4565 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4566 return 0; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4567 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4568 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4569 PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 51216 | 4570 Py_ssize_t size, |
| 4571 PyObject *mapping, | |
| 4572 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
|
4573 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4574 /* 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
|
4575 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
|
4576 /* current input position */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4577 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
|
4578 /* current output position */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4579 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
|
4580 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
|
4581 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
|
4582 /* 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
|
4583 * -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
|
4584 * 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
|
4585 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
|
4586 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4587 /* 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
|
4588 if (mapping == NULL) |
| 51216 | 4589 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
|
4590 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4591 /* 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
|
4592 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
|
4593 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
|
4594 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
|
4595 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
|
4596 if (size == 0) |
| 51216 | 4597 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
|
4598 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4599 while (inpos<size) { |
| 51216 | 4600 /* try to encode it */ |
| 4601 charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); | |
| 4602 if (x==enc_EXCEPTION) /* error */ | |
| 4603 goto onError; | |
| 4604 if (x==enc_FAILED) { /* unencodable character */ | |
| 4605 if (charmap_encoding_error(p, size, &inpos, mapping, | |
| 4606 &exc, | |
| 4607 &known_errorHandler, &errorHandler, errors, | |
| 4608 &res, &respos)) { | |
| 4609 goto onError; | |
| 4610 } | |
| 4611 } | |
| 4612 else | |
| 4613 /* done with this character => adjust input position */ | |
| 4614 ++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
|
4615 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4616 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4617 /* 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
|
4618 if (respos<PyString_GET_SIZE(res)) { |
| 51216 | 4619 if (_PyString_Resize(&res, respos)) |
| 4620 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
|
4621 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4622 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
|
4623 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
|
4624 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
|
4625 |
| 51216 | 4626 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
|
4627 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
|
4628 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
|
4629 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
|
4630 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4631 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4632 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4633 PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 51216 | 4634 PyObject *mapping) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4635 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4636 if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 51216 | 4637 PyErr_BadArgument(); |
| 4638 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4639 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4640 return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 4641 PyUnicode_GET_SIZE(unicode), |
| 4642 mapping, | |
| 4643 NULL); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4644 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4645 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4646 /* 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
|
4647 static void make_translate_exception(PyObject **exceptionObject, |
| 51216 | 4648 const Py_UNICODE *unicode, Py_ssize_t size, |
| 4649 Py_ssize_t startpos, Py_ssize_t endpos, | |
| 4650 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
|
4651 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4652 if (*exceptionObject == NULL) { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4653 *exceptionObject = PyUnicodeTranslateError_Create( |
| 51216 | 4654 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
|
4655 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4656 else { |
| 51216 | 4657 if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 4658 goto onError; | |
| 4659 if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) | |
| 4660 goto onError; | |
| 4661 if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) | |
| 4662 goto onError; | |
| 4663 return; | |
| 4664 onError: | |
| 4665 Py_DECREF(*exceptionObject); | |
| 4666 *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
|
4667 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4668 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4669 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4670 /* 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
|
4671 static void raise_translate_exception(PyObject **exceptionObject, |
| 51216 | 4672 const Py_UNICODE *unicode, Py_ssize_t size, |
| 4673 Py_ssize_t startpos, Py_ssize_t endpos, | |
| 4674 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
|
4675 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4676 make_translate_exception(exceptionObject, |
| 51216 | 4677 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
|
4678 if (*exceptionObject != NULL) |
| 51216 | 4679 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
|
4680 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4681 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4682 /* 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
|
4683 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
|
4684 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
|
4685 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
|
4686 static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 51216 | 4687 PyObject **errorHandler, |
| 4688 const char *reason, | |
| 4689 const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, | |
| 4690 Py_ssize_t startpos, Py_ssize_t endpos, | |
| 4691 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
|
4692 { |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
4693 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
|
4694 |
|
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
4695 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
|
4696 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
|
4697 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
|
4698 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4699 if (*errorHandler == NULL) { |
| 51216 | 4700 *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
|
4701 if (*errorHandler == NULL) |
| 51216 | 4702 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
|
4703 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4704 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4705 make_translate_exception(exceptionObject, |
| 51216 | 4706 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
|
4707 if (*exceptionObject == NULL) |
| 51216 | 4708 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
|
4709 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4710 restuple = PyObject_CallFunctionObjArgs( |
| 51216 | 4711 *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
|
4712 if (restuple == NULL) |
| 51216 | 4713 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
|
4714 if (!PyTuple_Check(restuple)) { |
|
52909
f3b26a729e82
Merged revisions 69578-69580,69901,69907,69994,70022-70023,70025-70026,70166,70273,70275,70342,70386-70387,70389-70390,70392-70393,70395,70397,70400,70418 via svnmerge
Georg Brandl <georg@python.org>
parents:
52411
diff
changeset
|
4715 PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 51216 | 4716 Py_DECREF(restuple); |
| 4717 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
|
4718 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4719 if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 51216 | 4720 &resunicode, &i_newpos)) { |
| 4721 Py_DECREF(restuple); | |
| 4722 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
|
4723 } |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4724 if (i_newpos<0) |
| 51216 | 4725 *newpos = size+i_newpos; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4726 else |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4727 *newpos = i_newpos; |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
4728 if (*newpos<0 || *newpos>size) { |
| 51216 | 4729 PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 4730 Py_DECREF(restuple); | |
| 4731 return NULL; | |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
4732 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4733 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
|
4734 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
|
4735 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
|
4736 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4737 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4738 /* 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
|
4739 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
|
4740 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
|
4741 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
|
4742 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
|
4743 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4744 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
|
4745 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
|
4746 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4747 if (w == NULL) |
| 51216 | 4748 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
|
4749 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
|
4750 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
|
4751 if (x == NULL) { |
| 51216 | 4752 if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4753 /* No mapping found means: use 1:1 mapping. */ | |
| 4754 PyErr_Clear(); | |
| 4755 *result = NULL; | |
| 4756 return 0; | |
| 4757 } else | |
| 4758 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
|
4759 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4760 else if (x == Py_None) { |
| 51216 | 4761 *result = x; |
| 4762 return 0; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4763 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4764 else if (PyInt_Check(x)) { |
| 51216 | 4765 long value = PyInt_AS_LONG(x); |
| 4766 long max = PyUnicode_GetMax(); | |
| 4767 if (value < 0 || value > max) { | |
| 4768 PyErr_Format(PyExc_TypeError, | |
| 4769 "character mapping must be in range(0x%lx)", max+1); | |
| 4770 Py_DECREF(x); | |
| 4771 return -1; | |
| 4772 } | |
| 4773 *result = x; | |
| 4774 return 0; | |
| 4775 } | |
| 4776 else if (PyUnicode_Check(x)) { | |
| 4777 *result = x; | |
| 4778 return 0; | |
| 4779 } | |
| 4780 else { | |
| 4781 /* wrong return value */ | |
| 4782 PyErr_SetString(PyExc_TypeError, | |
| 4783 "character mapping must return integer, None or unicode"); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4784 Py_DECREF(x); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4785 return -1; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4786 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4787 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4788 /* ensure that *outobj is at least requiredsize characters long, |
| 51216 | 4789 if not reallocate and adjust various state variables. |
| 4790 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
|
4791 static |
|
30765
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4792 int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
| 51216 | 4793 Py_ssize_t requiredsize) |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4794 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4795 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
|
4796 if (requiredsize > oldsize) { |
| 51216 | 4797 /* remember old output position */ |
| 4798 Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); | |
| 4799 /* exponentially overallocate to minimize reallocations */ | |
| 4800 if (requiredsize < 2 * oldsize) | |
| 4801 requiredsize = 2 * oldsize; | |
| 4802 if (PyUnicode_Resize(outobj, requiredsize) < 0) | |
| 4803 return -1; | |
| 4804 *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
|
4805 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4806 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
|
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 /* 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
|
4809 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
|
4810 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
|
4811 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
|
4812 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
|
4813 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
|
4814 static |
|
30765
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4815 int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
| 51216 | 4816 Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 4817 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
|
4818 { |
|
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4819 if (charmaptranslate_lookup(*curinp, mapping, res)) |
| 51216 | 4820 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
|
4821 if (*res==NULL) { |
| 51216 | 4822 /* not found => default to 1:1 mapping */ |
| 4823 *(*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
|
4824 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4825 else if (*res==Py_None) |
| 51216 | 4826 ; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4827 else if (PyInt_Check(*res)) { |
| 51216 | 4828 /* no overflow check, because we know that the space is enough */ |
| 4829 *(*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
|
4830 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4831 else if (PyUnicode_Check(*res)) { |
| 51216 | 4832 Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
| 4833 if (repsize==1) { | |
| 4834 /* no overflow check, because we know that the space is enough */ | |
| 4835 *(*outp)++ = *PyUnicode_AS_UNICODE(*res); | |
| 4836 } | |
| 4837 else if (repsize!=0) { | |
| 4838 /* more than one character */ | |
| 4839 Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + | |
| 4840 (insize - (curinp-startinp)) + | |
| 4841 repsize - 1; | |
| 4842 if (charmaptranslate_makespace(outobj, outp, requiredsize)) | |
| 4843 return -1; | |
| 4844 memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); | |
| 4845 *outp += repsize; | |
| 4846 } | |
|
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 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4848 else |
| 51216 | 4849 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
|
4850 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
|
4851 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4852 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4853 PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 51216 | 4854 Py_ssize_t size, |
| 4855 PyObject *mapping, | |
| 4856 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
|
4857 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4858 /* 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
|
4859 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
|
4860 /* 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
|
4861 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
|
4862 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
|
4863 /* 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
|
4864 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
|
4865 /* current output position */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4866 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
|
4867 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
|
4868 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
|
4869 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
|
4870 /* 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
|
4871 * -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
|
4872 * 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
|
4873 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
|
4874 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4875 if (mapping == NULL) { |
| 51216 | 4876 PyErr_BadArgument(); |
| 4877 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4878 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4879 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4880 /* 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
|
4881 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
|
4882 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
|
4883 if (res == NULL) |
| 51216 | 4884 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4885 if (size == 0) |
| 51216 | 4886 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
|
4887 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
|
4888 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4889 while (p<endp) { |
| 51216 | 4890 /* try to encode it */ |
| 4891 PyObject *x = NULL; | |
| 4892 if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { | |
| 4893 Py_XDECREF(x); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4894 goto onError; |
| 51216 | 4895 } |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4896 Py_XDECREF(x); |
| 51216 | 4897 if (x!=Py_None) /* it worked => adjust input pointer */ |
| 4898 ++p; | |
| 4899 else { /* untranslatable character */ | |
| 4900 PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ | |
| 4901 Py_ssize_t repsize; | |
| 4902 Py_ssize_t newpos; | |
| 4903 Py_UNICODE *uni2; | |
| 4904 /* startpos for collecting untranslatable chars */ | |
| 4905 const Py_UNICODE *collstart = p; | |
| 4906 const Py_UNICODE *collend = p+1; | |
| 4907 const Py_UNICODE *coll; | |
| 4908 | |
| 4909 /* find all untranslatable characters */ | |
| 4910 while (collend < endp) { | |
| 4911 if (charmaptranslate_lookup(*collend, mapping, &x)) | |
| 4912 goto onError; | |
| 4913 Py_XDECREF(x); | |
| 4914 if (x!=Py_None) | |
| 4915 break; | |
| 4916 ++collend; | |
| 4917 } | |
| 4918 /* cache callback name lookup | |
| 4919 * (if not done yet, i.e. it's the first error) */ | |
| 4920 if (known_errorHandler==-1) { | |
| 4921 if ((errors==NULL) || (!strcmp(errors, "strict"))) | |
| 4922 known_errorHandler = 1; | |
| 4923 else if (!strcmp(errors, "replace")) | |
| 4924 known_errorHandler = 2; | |
| 4925 else if (!strcmp(errors, "ignore")) | |
| 4926 known_errorHandler = 3; | |
| 4927 else if (!strcmp(errors, "xmlcharrefreplace")) | |
| 4928 known_errorHandler = 4; | |
| 4929 else | |
| 4930 known_errorHandler = 0; | |
| 4931 } | |
| 4932 switch (known_errorHandler) { | |
| 4933 case 1: /* strict */ | |
| 4934 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
|
4935 goto onError; |
| 51216 | 4936 case 2: /* replace */ |
| 4937 /* No need to check for space, this is a 1:1 replacement */ | |
| 4938 for (coll = collstart; coll<collend; ++coll) | |
| 4939 *str++ = '?'; | |
| 4940 /* fall through */ | |
| 4941 case 3: /* ignore */ | |
| 4942 p = collend; | |
| 4943 break; | |
| 4944 case 4: /* xmlcharrefreplace */ | |
| 4945 /* generate replacement (temporarily (mis)uses p) */ | |
| 4946 for (p = collstart; p < collend; ++p) { | |
| 4947 char buffer[2+29+1+1]; | |
| 4948 char *cp; | |
| 4949 sprintf(buffer, "&#%d;", (int)*p); | |
| 4950 if (charmaptranslate_makespace(&res, &str, | |
| 4951 (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) | |
| 4952 goto onError; | |
| 4953 for (cp = buffer; *cp; ++cp) | |
| 4954 *str++ = *cp; | |
| 4955 } | |
| 4956 p = collend; | |
| 4957 break; | |
| 4958 default: | |
| 4959 repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, | |
| 4960 reason, startp, size, &exc, | |
| 4961 collstart-startp, collend-startp, &newpos); | |
| 4962 if (repunicode == NULL) | |
| 4963 goto onError; | |
| 4964 /* generate replacement */ | |
| 4965 repsize = PyUnicode_GET_SIZE(repunicode); | |
| 4966 if (charmaptranslate_makespace(&res, &str, | |
| 4967 (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { | |
| 4968 Py_DECREF(repunicode); | |
| 4969 goto onError; | |
| 4970 } | |
| 4971 for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) | |
| 4972 *str++ = *uni2; | |
| 4973 p = startp + newpos; | |
| 4974 Py_DECREF(repunicode); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4975 } |
| 51216 | 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 /* 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
|
4979 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
|
4980 if (respos<PyUnicode_GET_SIZE(res)) { |
| 51216 | 4981 if (PyUnicode_Resize(&res, respos) < 0) |
| 4982 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
|
4983 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4984 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
|
4985 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
|
4986 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
|
4987 |
| 51216 | 4988 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
|
4989 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
|
4990 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
|
4991 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
|
4992 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4993 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4994 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4995 PyObject *PyUnicode_Translate(PyObject *str, |
| 51216 | 4996 PyObject *mapping, |
| 4997 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
|
4998 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4999 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
|
5000 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5001 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
|
5002 if (str == NULL) |
| 51216 | 5003 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5004 result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 51216 | 5005 PyUnicode_GET_SIZE(str), |
| 5006 mapping, | |
| 5007 errors); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5008 Py_DECREF(str); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5009 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
|
5010 |
| 51216 | 5011 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5012 Py_XDECREF(str); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5013 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5014 } |
|
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
|
5015 |
|
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
|
5016 /* --- 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
|
5017 |
|
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
|
5018 int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 51216 | 5019 Py_ssize_t length, |
| 5020 char *output, | |
| 5021 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
|
5022 { |
|
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
|
5023 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
|
5024 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
|
5025 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
|
5026 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
|
5027 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
|
5028 /* 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
|
5029 * -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
|
5030 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
|
5031 |
|
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
|
5032 if (output == NULL) { |
| 51216 | 5033 PyErr_BadArgument(); |
| 5034 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
|
5035 } |
|
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
|
5036 |
|
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
|
5037 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
|
5038 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
|
5039 while (p < end) { |
| 51216 | 5040 register Py_UNICODE ch = *p; |
| 5041 int decimal; | |
| 5042 PyObject *repunicode; | |
| 5043 Py_ssize_t repsize; | |
| 5044 Py_ssize_t newpos; | |
| 5045 Py_UNICODE *uni2; | |
| 5046 Py_UNICODE *collstart; | |
| 5047 Py_UNICODE *collend; | |
| 5048 | |
| 5049 if (Py_UNICODE_ISSPACE(ch)) { | |
| 5050 *output++ = ' '; | |
| 5051 ++p; | |
| 5052 continue; | |
| 5053 } | |
| 5054 decimal = Py_UNICODE_TODECIMAL(ch); | |
| 5055 if (decimal >= 0) { | |
| 5056 *output++ = '0' + decimal; | |
| 5057 ++p; | |
| 5058 continue; | |
| 5059 } | |
| 5060 if (0 < ch && ch < 256) { | |
| 5061 *output++ = (char)ch; | |
| 5062 ++p; | |
| 5063 continue; | |
| 5064 } | |
| 5065 /* All other characters are considered unencodable */ | |
| 5066 collstart = p; | |
| 5067 collend = p+1; | |
| 5068 while (collend < end) { | |
| 5069 if ((0 < *collend && *collend < 256) || | |
| 5070 !Py_UNICODE_ISSPACE(*collend) || | |
| 5071 Py_UNICODE_TODECIMAL(*collend)) | |
| 5072 break; | |
| 5073 } | |
| 5074 /* cache callback name lookup | |
| 5075 * (if not done yet, i.e. it's the first error) */ | |
| 5076 if (known_errorHandler==-1) { | |
| 5077 if ((errors==NULL) || (!strcmp(errors, "strict"))) | |
| 5078 known_errorHandler = 1; | |
| 5079 else if (!strcmp(errors, "replace")) | |
| 5080 known_errorHandler = 2; | |
| 5081 else if (!strcmp(errors, "ignore")) | |
| 5082 known_errorHandler = 3; | |
| 5083 else if (!strcmp(errors, "xmlcharrefreplace")) | |
| 5084 known_errorHandler = 4; | |
| 5085 else | |
| 5086 known_errorHandler = 0; | |
| 5087 } | |
| 5088 switch (known_errorHandler) { | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5089 case 1: /* strict */ |
| 51216 | 5090 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
|
5091 goto onError; |
| 51216 | 5092 case 2: /* replace */ |
| 5093 for (p = collstart; p < collend; ++p) | |
| 5094 *output++ = '?'; | |
| 5095 /* fall through */ | |
| 5096 case 3: /* ignore */ | |
| 5097 p = collend; | |
| 5098 break; | |
| 5099 case 4: /* xmlcharrefreplace */ | |
| 5100 /* generate replacement (temporarily (mis)uses p) */ | |
| 5101 for (p = collstart; p < collend; ++p) | |
| 5102 output += sprintf(output, "&#%d;", (int)*p); | |
| 5103 p = collend; | |
| 5104 break; | |
| 5105 default: | |
| 5106 repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, | |
| 5107 encoding, reason, s, length, &exc, | |
| 5108 collstart-s, collend-s, &newpos); | |
| 5109 if (repunicode == NULL) | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5110 goto onError; |
| 51216 | 5111 /* generate replacement */ |
| 5112 repsize = PyUnicode_GET_SIZE(repunicode); | |
| 5113 for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { | |
| 5114 Py_UNICODE ch = *uni2; | |
| 5115 if (Py_UNICODE_ISSPACE(ch)) | |
| 5116 *output++ = ' '; | |
| 5117 else { | |
| 5118 decimal = Py_UNICODE_TODECIMAL(ch); | |
| 5119 if (decimal >= 0) | |
| 5120 *output++ = '0' + decimal; | |
| 5121 else if (0 < ch && ch < 256) | |
| 5122 *output++ = (char)ch; | |
| 5123 else { | |
| 5124 Py_DECREF(repunicode); | |
| 5125 raise_encode_exception(&exc, encoding, | |
| 5126 s, length, collstart-s, collend-s, reason); | |
| 5127 goto onError; | |
| 5128 } | |
| 5129 } | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5130 } |
| 51216 | 5131 p = s + newpos; |
| 5132 Py_DECREF(repunicode); | |
| 5133 } | |
|
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
|
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 /* 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
|
5136 *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
|
5137 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
|
5138 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
|
5139 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
|
5140 |
| 51216 | 5141 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
|
5142 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
|
5143 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
|
5144 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
|
5145 } |
|
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
|
5146 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5147 /* --- Helpers ------------------------------------------------------------ */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5148 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
5149 #include "stringlib/unicodedefs.h" |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
5150 |
|
43888
1fb353776dfb
Made _ParseTupleFinds only defined to unicodeobject.c
Facundo Batista <facundobatista@gmail.com>
parents:
43886
diff
changeset
|
5151 #define FROM_UNICODE |
|
38383
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
5152 |
|
38381
b32f9bb2e077
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38347
diff
changeset
|
5153 #include "stringlib/fastsearch.h" |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5154 |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5155 #include "stringlib/count.h" |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5156 #include "stringlib/find.h" |
|
38383
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
5157 #include "stringlib/partition.h" |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
5158 |
|
38402
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5159 /* 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
|
5160 #define FIX_START_END(obj) \ |
|
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5161 if (start < 0) \ |
|
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5162 start += (obj)->length; \ |
|
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5163 if (start < 0) \ |
|
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5164 start = 0; \ |
|
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5165 if (end > (obj)->length) \ |
|
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5166 end = (obj)->length; \ |
|
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5167 if (end < 0) \ |
|
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5168 end += (obj)->length; \ |
|
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5169 if (end < 0) \ |
|
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5170 end = 0; |
|
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5171 |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5172 Py_ssize_t PyUnicode_Count(PyObject *str, |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5173 PyObject *substr, |
|
38347
6d8615f9f1cc
use Py_LOCAL also for string and unicode objects
Fredrik Lundh <fredrik@pythonware.com>
parents:
38339
diff
changeset
|
5174 Py_ssize_t start, |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5175 Py_ssize_t end) |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5176 { |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5177 Py_ssize_t result; |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5178 PyUnicodeObject* str_obj; |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5179 PyUnicodeObject* sub_obj; |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5180 |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5181 str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5182 if (!str_obj) |
| 51216 | 5183 return -1; |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5184 sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5185 if (!sub_obj) { |
| 51216 | 5186 Py_DECREF(str_obj); |
| 5187 return -1; | |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5188 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5189 |
|
38402
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5190 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
|
5191 |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5192 result = stringlib_count( |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5193 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
|
5194 ); |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
5195 |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5196 Py_DECREF(sub_obj); |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5197 Py_DECREF(str_obj); |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5198 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5199 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5200 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5201 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5202 Py_ssize_t PyUnicode_Find(PyObject *str, |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5203 PyObject *sub, |
|
38401
adc214fc1e1c
needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
38400
diff
changeset
|
5204 Py_ssize_t start, |
|
adc214fc1e1c
needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
38400
diff
changeset
|
5205 Py_ssize_t end, |
|
adc214fc1e1c
needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
38400
diff
changeset
|
5206 int direction) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5207 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5208 Py_ssize_t result; |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5209 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5210 str = PyUnicode_FromObject(str); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5211 if (!str) |
| 51216 | 5212 return -2; |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5213 sub = PyUnicode_FromObject(sub); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5214 if (!sub) { |
| 51216 | 5215 Py_DECREF(str); |
| 5216 return -2; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5217 } |
|
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
|
5218 |
|
38401
adc214fc1e1c
needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
38400
diff
changeset
|
5219 if (direction > 0) |
|
38441
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
5220 result = stringlib_find_slice( |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
5221 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
|
5222 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
|
5223 start, end |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
5224 ); |
|
38401
adc214fc1e1c
needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
38400
diff
changeset
|
5225 else |
|
38441
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
5226 result = stringlib_rfind_slice( |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
5227 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
|
5228 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
|
5229 start, end |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
5230 ); |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5231 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5232 Py_DECREF(str); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5233 Py_DECREF(sub); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5234 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5235 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5236 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5237 |
|
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
|
5238 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5239 int tailmatch(PyUnicodeObject *self, |
| 51216 | 5240 PyUnicodeObject *substring, |
| 5241 Py_ssize_t start, | |
| 5242 Py_ssize_t end, | |
| 5243 int direction) | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5244 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5245 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
|
5246 return 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5247 |
|
38402
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5248 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
|
5249 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5250 end -= substring->length; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5251 if (end < start) |
| 51216 | 5252 return 0; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5253 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5254 if (direction > 0) { |
| 51216 | 5255 if (Py_UNICODE_MATCH(self, end, substring)) |
| 5256 return 1; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5257 } else { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5258 if (Py_UNICODE_MATCH(self, start, substring)) |
| 51216 | 5259 return 1; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5260 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5261 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5262 return 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5263 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5264 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5265 Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
| 51216 | 5266 PyObject *substr, |
| 5267 Py_ssize_t start, | |
| 5268 Py_ssize_t end, | |
| 5269 int direction) | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5270 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5271 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
|
5272 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5273 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
|
5274 if (str == NULL) |
| 51216 | 5275 return -1; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5276 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
|
5277 if (substr == NULL) { |
| 51216 | 5278 Py_DECREF(str); |
| 5279 return -1; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5280 } |
|
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
|
5281 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5282 result = tailmatch((PyUnicodeObject *)str, |
| 51216 | 5283 (PyUnicodeObject *)substr, |
| 5284 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
|
5285 Py_DECREF(str); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5286 Py_DECREF(substr); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5287 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5288 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5289 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5290 /* 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
|
5291 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
|
5292 |
|
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
|
5293 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5294 PyObject *fixup(PyUnicodeObject *self, |
| 51216 | 5295 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
|
5296 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5297 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5298 PyUnicodeObject *u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5299 |
|
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
|
5300 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
|
5301 if (u == NULL) |
| 51216 | 5302 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
|
5303 |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
5304 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
|
5305 |
|
19885
72e6eb653312
More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19849
diff
changeset
|
5306 if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
| 51216 | 5307 /* fixfct should return TRUE if it modified the buffer. If |
| 5308 FALSE, return a reference to the original buffer instead | |
| 5309 (to save space, not time) */ | |
| 5310 Py_INCREF(self); | |
| 5311 Py_DECREF(u); | |
| 5312 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
|
5313 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5314 return (PyObject*) u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5315 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5316 |
|
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
|
5317 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5318 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
|
5319 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5320 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
|
5321 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
|
5322 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
|
5323 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5324 while (len-- > 0) { |
| 51216 | 5325 register Py_UNICODE ch; |
| 5326 | |
| 5327 ch = Py_UNICODE_TOUPPER(*s); | |
| 5328 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
|
5329 status = 1; |
| 51216 | 5330 *s = ch; |
| 5331 } | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5332 s++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5333 } |
|
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 return status; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5336 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5337 |
|
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
|
5338 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5339 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
|
5340 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5341 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
|
5342 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
|
5343 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
|
5344 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5345 while (len-- > 0) { |
| 51216 | 5346 register Py_UNICODE ch; |
| 5347 | |
| 5348 ch = Py_UNICODE_TOLOWER(*s); | |
| 5349 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
|
5350 status = 1; |
| 51216 | 5351 *s = ch; |
| 5352 } | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5353 s++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5354 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5355 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5356 return status; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5357 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5358 |
|
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
|
5359 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5360 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
|
5361 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5362 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
|
5363 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
|
5364 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
|
5365 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5366 while (len-- > 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5367 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
|
5368 *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
|
5369 status = 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5370 } 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
|
5371 *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
|
5372 status = 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5373 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5374 s++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5375 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5376 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5377 return status; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5378 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5379 |
|
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
|
5380 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5381 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
|
5382 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5383 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
|
5384 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
|
5385 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
|
5386 |
|
16332
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5387 if (len == 0) |
| 51216 | 5388 return 0; |
|
16332
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5389 if (Py_UNICODE_ISLOWER(*s)) { |
| 51216 | 5390 *s = Py_UNICODE_TOUPPER(*s); |
| 5391 status = 1; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5392 } |
|
16332
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5393 s++; |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5394 while (--len > 0) { |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5395 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
|
5396 *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
|
5397 status = 1; |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5398 } |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5399 s++; |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5400 } |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5401 return status; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5402 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5403 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5404 static |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5405 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
|
5406 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5407 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
|
5408 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
|
5409 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
|
5410 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5411 /* 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
|
5412 if (PyUnicode_GET_SIZE(self) == 1) { |
| 51216 | 5413 Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 5414 if (*p != ch) { | |
| 5415 *p = ch; | |
| 5416 return 1; | |
| 5417 } | |
| 5418 else | |
| 5419 return 0; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5420 } |
|
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 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
|
5423 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
|
5424 for (; p < e; p++) { |
| 51216 | 5425 register const Py_UNICODE ch = *p; |
| 5426 | |
| 5427 if (previous_is_cased) | |
| 5428 *p = Py_UNICODE_TOLOWER(ch); | |
| 5429 else | |
| 5430 *p = Py_UNICODE_TOTITLE(ch); | |
| 5431 | |
| 5432 if (Py_UNICODE_ISLOWER(ch) || | |
| 5433 Py_UNICODE_ISUPPER(ch) || | |
| 5434 Py_UNICODE_ISTITLE(ch)) | |
| 5435 previous_is_cased = 1; | |
| 5436 else | |
| 5437 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
|
5438 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5439 return 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5440 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5441 |
|
33300
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5442 PyObject * |
|
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5443 PyUnicode_Join(PyObject *separator, PyObject *seq) |
|
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5444 { |
|
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5445 PyObject *internal_separator = NULL; |
|
33499
a4d3673bcd66
Initialize sep and seplen to suppress warning from gcc.
Skip Montanaro <skip@pobox.com>
parents:
33495
diff
changeset
|
5446 const Py_UNICODE blank = ' '; |
|
a4d3673bcd66
Initialize sep and seplen to suppress warning from gcc.
Skip Montanaro <skip@pobox.com>
parents:
33495
diff
changeset
|
5447 const Py_UNICODE *sep = ␣ |
|
38240
0373a074004c
PyUnicode_Join(): Recent code changes introduced new
Tim Peters <tim.peters@gmail.com>
parents:
38239
diff
changeset
|
5448 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
|
5449 PyUnicodeObject *res = NULL; /* the result */ |
|
38240
0373a074004c
PyUnicode_Join(): Recent code changes introduced new
Tim Peters <tim.peters@gmail.com>
parents:
38239
diff
changeset
|
5450 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
|
5451 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
|
5452 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
|
5453 PyObject *fseq; /* PySequence_Fast(seq) */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5454 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
|
5455 PyObject *item; |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5456 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
|
5457 |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5458 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
|
5459 if (fseq == NULL) { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5460 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
|
5461 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5462 |
|
33311
59bd121eeeb7
PyUnicode_Join(): Bozo Alert. While this is chugging along, it may
Tim Peters <tim.peters@gmail.com>
parents:
33310
diff
changeset
|
5463 /* 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
|
5464 * 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
|
5465 * 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
|
5466 * 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
|
5467 * 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
|
5468 * is invariant. |
|
59bd121eeeb7
PyUnicode_Join(): Bozo Alert. While this is chugging along, it may
Tim Peters <tim.peters@gmail.com>
parents:
33310
diff
changeset
|
5469 */ |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5470 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
|
5471 /* 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
|
5472 if (seqlen == 0) { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5473 res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5474 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
|
5475 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5476 /* 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
|
5477 if (seqlen == 1) { |
| 51216 | 5478 item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 5479 if (PyUnicode_CheckExact(item)) { | |
| 5480 Py_INCREF(item); | |
| 5481 res = (PyUnicodeObject *)item; | |
| 5482 goto Done; | |
| 5483 } | |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5484 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5485 |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5486 /* 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
|
5487 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
|
5488 /* Set up sep and seplen -- they're needed. */ |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5489 if (separator == NULL) { |
| 51216 | 5490 sep = ␣ |
| 5491 seplen = 1; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5492 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5493 else { |
| 51216 | 5494 internal_separator = PyUnicode_FromObject(separator); |
| 5495 if (internal_separator == NULL) | |
| 5496 goto onError; | |
| 5497 sep = PyUnicode_AS_UNICODE(internal_separator); | |
| 5498 seplen = PyUnicode_GET_SIZE(internal_separator); | |
| 5499 /* In case PyUnicode_FromObject() mutated seq. */ | |
| 5500 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
|
5501 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5502 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5503 |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5504 /* Get space. */ |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5505 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
|
5506 if (res == NULL) |
|
33300
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5507 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
|
5508 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
|
5509 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
|
5510 |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5511 for (i = 0; i < seqlen; ++i) { |
| 51216 | 5512 Py_ssize_t itemlen; |
| 5513 Py_ssize_t new_res_used; | |
| 5514 | |
| 5515 item = PySequence_Fast_GET_ITEM(fseq, i); | |
| 5516 /* Convert item to Unicode. */ | |
| 5517 if (! PyUnicode_Check(item) && ! PyString_Check(item)) { | |
| 5518 PyErr_Format(PyExc_TypeError, | |
| 5519 "sequence item %zd: expected string or Unicode," | |
| 5520 " %.80s found", | |
| 5521 i, Py_TYPE(item)->tp_name); | |
| 5522 goto onError; | |
| 5523 } | |
| 5524 item = PyUnicode_FromObject(item); | |
| 5525 if (item == NULL) | |
| 5526 goto onError; | |
| 5527 /* We own a reference to item from here on. */ | |
| 5528 | |
| 5529 /* In case PyUnicode_FromObject() mutated seq. */ | |
| 5530 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
|
5531 |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5532 /* Make sure we have enough space for the separator and the item. */ |
| 51216 | 5533 itemlen = PyUnicode_GET_SIZE(item); |
| 5534 new_res_used = res_used + itemlen; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5535 if (new_res_used < 0) |
| 51216 | 5536 goto Overflow; |
| 5537 if (i < seqlen - 1) { | |
| 5538 new_res_used += seplen; | |
| 5539 if (new_res_used < 0) | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5540 goto Overflow; |
| 51216 | 5541 } |
| 5542 if (new_res_used > res_alloc) { | |
| 5543 /* double allocated size until it's big enough */ | |
| 5544 do { | |
| 5545 res_alloc += res_alloc; | |
| 5546 if (res_alloc <= 0) | |
| 5547 goto Overflow; | |
| 5548 } while (new_res_used > res_alloc); | |
| 5549 if (_PyUnicode_Resize(&res, res_alloc) < 0) { | |
| 5550 Py_DECREF(item); | |
| 5551 goto onError; | |
| 5552 } | |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5553 res_p = PyUnicode_AS_UNICODE(res) + res_used; |
| 51216 | 5554 } |
| 5555 | |
| 5556 /* Copy item, and maybe the separator. */ | |
| 5557 Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); | |
| 5558 res_p += itemlen; | |
| 5559 if (i < seqlen - 1) { | |
| 5560 Py_UNICODE_COPY(res_p, sep, seplen); | |
| 5561 res_p += seplen; | |
| 5562 } | |
| 5563 Py_DECREF(item); | |
| 5564 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
|
5565 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5566 |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5567 /* 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
|
5568 * 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
|
5569 */ |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5570 if (_PyUnicode_Resize(&res, res_used) < 0) |
| 51216 | 5571 goto onError; |
| 5572 | |
| 5573 Done: | |
|
33300
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5574 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
|
5575 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
|
5576 return (PyObject *)res; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5577 |
| 51216 | 5578 Overflow: |
|
33300
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5579 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
|
5580 "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
|
5581 Py_DECREF(item); |
|
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5582 /* fall through */ |
|
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5583 |
| 51216 | 5584 onError: |
|
33300
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5585 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
|
5586 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
|
5587 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
|
5588 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5589 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5590 |
|
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
|
5591 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
|
5592 PyUnicodeObject *pad(PyUnicodeObject *self, |
| 51216 | 5593 Py_ssize_t left, |
| 5594 Py_ssize_t right, | |
| 5595 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
|
5596 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5597 PyUnicodeObject *u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5598 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5599 if (left < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5600 left = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5601 if (right < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5602 right = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5603 |
|
19885
72e6eb653312
More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19849
diff
changeset
|
5604 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
|
5605 Py_INCREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5606 return self; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5607 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5608 |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
5609 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
|
5610 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
|
5611 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
|
5612 return NULL; |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
5613 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5614 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
|
5615 if (u) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5616 if (left) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5617 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
|
5618 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
|
5619 if (right) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5620 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
|
5621 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5622 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5623 return u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5624 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5625 |
| 51216 | 5626 #define SPLIT_APPEND(data, left, right) \ |
| 5627 str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ | |
| 5628 if (!str) \ | |
| 5629 goto onError; \ | |
| 5630 if (PyList_Append(list, str)) { \ | |
| 5631 Py_DECREF(str); \ | |
| 5632 goto onError; \ | |
| 5633 } \ | |
| 5634 else \ | |
| 5635 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
|
5636 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5637 static |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5638 PyObject *split_whitespace(PyUnicodeObject *self, |
| 51216 | 5639 PyObject *list, |
| 5640 Py_ssize_t maxcount) | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5641 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5642 register Py_ssize_t i; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5643 register Py_ssize_t j; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5644 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
|
5645 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
|
5646 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
|
5647 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5648 for (i = j = 0; i < len; ) { |
| 51216 | 5649 /* find a token */ |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5650 while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
| 51216 | 5651 i++; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5652 j = i; |
| 51216 | 5653 while (i < len && !Py_UNICODE_ISSPACE(buf[i])) |
| 5654 i++; | |
| 5655 if (j < i) { | |
| 5656 if (maxcount-- <= 0) | |
| 5657 break; | |
| 5658 SPLIT_APPEND(buf, j, i); | |
| 5659 while (i < len && Py_UNICODE_ISSPACE(buf[i])) | |
| 5660 i++; | |
| 5661 j = i; | |
| 5662 } | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5663 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5664 if (j < len) { |
| 51216 | 5665 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
|
5666 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5667 return list; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5668 |
| 51216 | 5669 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5670 Py_DECREF(list); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5671 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5672 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5673 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5674 PyObject *PyUnicode_Splitlines(PyObject *string, |
| 51216 | 5675 int keepends) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5676 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5677 register Py_ssize_t i; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5678 register Py_ssize_t j; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5679 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
|
5680 PyObject *list; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5681 PyObject *str; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5682 Py_UNICODE *data; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5683 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5684 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
|
5685 if (string == NULL) |
| 51216 | 5686 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5687 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
|
5688 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
|
5689 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5690 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
|
5691 if (!list) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5692 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5693 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5694 for (i = j = 0; i < len; ) { |
| 51216 | 5695 Py_ssize_t eol; |
| 5696 | |
| 5697 /* Find a line and append it */ | |
| 5698 while (i < len && !BLOOM_LINEBREAK(data[i])) | |
| 5699 i++; | |
| 5700 | |
| 5701 /* 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
|
5702 eol = i; |
| 51216 | 5703 if (i < len) { |
| 5704 if (data[i] == '\r' && i + 1 < len && | |
| 5705 data[i+1] == '\n') | |
| 5706 i += 2; | |
| 5707 else | |
| 5708 i++; | |
| 5709 if (keepends) | |
| 5710 eol = i; | |
| 5711 } | |
| 5712 SPLIT_APPEND(data, j, eol); | |
| 5713 j = i; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5714 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5715 if (j < len) { |
| 51216 | 5716 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
|
5717 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5718 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5719 Py_DECREF(string); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5720 return list; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5721 |
| 51216 | 5722 onError: |
|
36877
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36675
diff
changeset
|
5723 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
|
5724 Py_DECREF(string); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5725 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5726 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5727 |
|
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
|
5728 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5729 PyObject *split_char(PyUnicodeObject *self, |
| 51216 | 5730 PyObject *list, |
| 5731 Py_UNICODE ch, | |
| 5732 Py_ssize_t maxcount) | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5733 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5734 register Py_ssize_t i; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5735 register Py_ssize_t j; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5736 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
|
5737 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
|
5738 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
|
5739 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5740 for (i = j = 0; i < len; ) { |
| 51216 | 5741 if (buf[i] == ch) { |
| 5742 if (maxcount-- <= 0) | |
| 5743 break; | |
| 5744 SPLIT_APPEND(buf, j, i); | |
| 5745 i = j = i + 1; | |
| 5746 } else | |
| 5747 i++; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5748 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5749 if (j <= len) { |
| 51216 | 5750 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
|
5751 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5752 return list; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5753 |
| 51216 | 5754 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5755 Py_DECREF(list); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5756 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5757 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5758 |
|
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
|
5759 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5760 PyObject *split_substring(PyUnicodeObject *self, |
| 51216 | 5761 PyObject *list, |
| 5762 PyUnicodeObject *substring, | |
| 5763 Py_ssize_t maxcount) | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5764 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5765 register Py_ssize_t i; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5766 register Py_ssize_t j; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5767 Py_ssize_t len = self->length; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5768 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
|
5769 PyObject *str; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5770 |
|
15609
0906c8e2c961
Fix off-by-one error in split_substring(). Fixes SF bug #122162.
Guido van Rossum <guido@python.org>
parents:
15579
diff
changeset
|
5771 for (i = j = 0; i <= len - sublen; ) { |
| 51216 | 5772 if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5773 if (maxcount-- <= 0) | |
| 5774 break; | |
| 5775 SPLIT_APPEND(self->str, j, i); | |
| 5776 i = j = i + sublen; | |
| 5777 } else | |
| 5778 i++; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5779 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5780 if (j <= len) { |
| 51216 | 5781 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
|
5782 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5783 return list; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5784 |
| 51216 | 5785 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5786 Py_DECREF(list); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5787 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5788 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5789 |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5790 static |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5791 PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 51216 | 5792 PyObject *list, |
| 5793 Py_ssize_t maxcount) | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5794 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5795 register Py_ssize_t i; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5796 register Py_ssize_t j; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5797 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
|
5798 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
|
5799 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
|
5800 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5801 for (i = j = len - 1; i >= 0; ) { |
| 51216 | 5802 /* find a token */ |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5803 while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
| 51216 | 5804 i--; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5805 j = i; |
| 51216 | 5806 while (i >= 0 && !Py_UNICODE_ISSPACE(buf[i])) |
| 5807 i--; | |
| 5808 if (j > i) { | |
| 5809 if (maxcount-- <= 0) | |
| 5810 break; | |
| 5811 SPLIT_APPEND(buf, i + 1, j + 1); | |
| 5812 while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) | |
| 5813 i--; | |
| 5814 j = i; | |
| 5815 } | |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5816 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5817 if (j >= 0) { |
| 51216 | 5818 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
|
5819 } |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
5820 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
|
5821 goto onError; |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5822 return list; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5823 |
| 51216 | 5824 onError: |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5825 Py_DECREF(list); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5826 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5827 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5828 |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5829 static |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5830 PyObject *rsplit_char(PyUnicodeObject *self, |
| 51216 | 5831 PyObject *list, |
| 5832 Py_UNICODE ch, | |
| 5833 Py_ssize_t maxcount) | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5834 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5835 register Py_ssize_t i; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5836 register Py_ssize_t j; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5837 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
|
5838 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
|
5839 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
|
5840 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5841 for (i = j = len - 1; i >= 0; ) { |
| 51216 | 5842 if (buf[i] == ch) { |
| 5843 if (maxcount-- <= 0) | |
| 5844 break; | |
| 5845 SPLIT_APPEND(buf, i + 1, j + 1); | |
| 5846 j = i = i - 1; | |
| 5847 } else | |
| 5848 i--; | |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5849 } |
|
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
|
5850 if (j >= -1) { |
| 51216 | 5851 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
|
5852 } |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
5853 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
|
5854 goto onError; |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5855 return list; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5856 |
| 51216 | 5857 onError: |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5858 Py_DECREF(list); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5859 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5860 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5861 |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5862 static |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5863 PyObject *rsplit_substring(PyUnicodeObject *self, |
| 51216 | 5864 PyObject *list, |
| 5865 PyUnicodeObject *substring, | |
| 5866 Py_ssize_t maxcount) | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5867 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5868 register Py_ssize_t i; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5869 register Py_ssize_t j; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5870 Py_ssize_t len = self->length; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5871 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
|
5872 PyObject *str; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5873 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5874 for (i = len - sublen, j = len; i >= 0; ) { |
| 51216 | 5875 if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5876 if (maxcount-- <= 0) | |
| 5877 break; | |
| 5878 SPLIT_APPEND(self->str, i + sublen, j); | |
| 5879 j = i; | |
| 5880 i -= sublen; | |
| 5881 } else | |
| 5882 i--; | |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5883 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5884 if (j >= 0) { |
| 51216 | 5885 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
|
5886 } |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
5887 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
|
5888 goto onError; |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5889 return list; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5890 |
| 51216 | 5891 onError: |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5892 Py_DECREF(list); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5893 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5894 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5895 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5896 #undef SPLIT_APPEND |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5897 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5898 static |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5899 PyObject *split(PyUnicodeObject *self, |
| 51216 | 5900 PyUnicodeObject *substring, |
| 5901 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
|
5902 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5903 PyObject *list; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5904 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5905 if (maxcount < 0) |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5906 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
|
5907 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5908 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
|
5909 if (!list) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5910 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5911 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5912 if (substring == NULL) |
| 51216 | 5913 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
|
5914 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5915 else if (substring->length == 1) |
| 51216 | 5916 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
|
5917 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5918 else if (substring->length == 0) { |
| 51216 | 5919 Py_DECREF(list); |
| 5920 PyErr_SetString(PyExc_ValueError, "empty separator"); | |
| 5921 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5922 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5923 else |
| 51216 | 5924 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
|
5925 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5926 |
|
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
|
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(PyUnicodeObject *self, |
| 51216 | 5929 PyUnicodeObject *substring, |
| 5930 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
|
5931 { |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5932 PyObject *list; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5933 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5934 if (maxcount < 0) |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5935 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
|
5936 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5937 list = PyList_New(0); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5938 if (!list) |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5939 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5940 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5941 if (substring == NULL) |
| 51216 | 5942 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
|
5943 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5944 else if (substring->length == 1) |
| 51216 | 5945 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
|
5946 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5947 else if (substring->length == 0) { |
| 51216 | 5948 Py_DECREF(list); |
| 5949 PyErr_SetString(PyExc_ValueError, "empty separator"); | |
| 5950 return NULL; | |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5951 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5952 else |
| 51216 | 5953 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
|
5954 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5955 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5956 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5957 PyObject *replace(PyUnicodeObject *self, |
| 51216 | 5958 PyUnicodeObject *str1, |
| 5959 PyUnicodeObject *str2, | |
| 5960 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
|
5961 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5962 PyUnicodeObject *u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5963 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5964 if (maxcount < 0) |
| 51216 | 5965 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
|
5966 |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5967 if (str1->length == str2->length) { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5968 /* same length */ |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5969 Py_ssize_t i; |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5970 if (str1->length == 1) { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5971 /* replace characters */ |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5972 Py_UNICODE u1, u2; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5973 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
|
5974 goto nothing; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5975 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
|
5976 if (!u) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5977 return NULL; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5978 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
|
5979 u1 = str1->str[0]; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5980 u2 = str2->str[0]; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5981 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
|
5982 if (u->str[i] == u1) { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5983 if (--maxcount < 0) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5984 break; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5985 u->str[i] = u2; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5986 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5987 } else { |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5988 i = fastsearch( |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5989 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
|
5990 ); |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5991 if (i < 0) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5992 goto nothing; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5993 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
|
5994 if (!u) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5995 return NULL; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5996 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
|
5997 while (i <= self->length - str1->length) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5998 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
|
5999 if (--maxcount < 0) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6000 break; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6001 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
|
6002 i += str1->length; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6003 } else |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6004 i++; |
|
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 } else { |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6007 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
6008 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
|
6009 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
|
6010 Py_UNICODE *p; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6011 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6012 /* replace strings */ |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
6013 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
|
6014 if (n > maxcount) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6015 n = maxcount; |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6016 if (n == 0) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6017 goto nothing; |
|
38299
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
6018 /* 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
|
6019 delta = (str2->length - str1->length); |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
6020 if (delta == 0) { |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
6021 new_size = self->length; |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
6022 } else { |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
6023 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
|
6024 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
|
6025 PyErr_SetString(PyExc_OverflowError, |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
6026 "replace string is too long"); |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
6027 return NULL; |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
6028 } |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
6029 new_size = self->length + product; |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
6030 if (new_size < 0) { |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
6031 PyErr_SetString(PyExc_OverflowError, |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
6032 "replace string is too long"); |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
6033 return NULL; |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
6034 } |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
6035 } |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
6036 u = _PyUnicode_New(new_size); |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6037 if (!u) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6038 return NULL; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6039 i = 0; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6040 p = u->str; |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
6041 e = self->length - str1->length; |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6042 if (str1->length > 0) { |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
6043 while (n-- > 0) { |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
6044 /* look for next match */ |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
6045 j = i; |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
6046 while (j <= e) { |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
6047 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
|
6048 break; |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
6049 j++; |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
6050 } |
| 51216 | 6051 if (j > i) { |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
6052 if (j > e) |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
6053 break; |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
6054 /* copy unchanged part [i:j] */ |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
6055 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
|
6056 p += j - i; |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
6057 } |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
6058 /* copy substitution string */ |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
6059 if (str2->length > 0) { |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6060 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
|
6061 p += str2->length; |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
6062 } |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
6063 i = j + str1->length; |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
6064 } |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
6065 if (i < self->length) |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
6066 /* copy tail [i:] */ |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
6067 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
|
6068 } else { |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
6069 /* interleave */ |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6070 while (n > 0) { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6071 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
|
6072 p += str2->length; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6073 if (--n <= 0) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6074 break; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6075 *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
|
6076 } |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6077 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
|
6078 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6079 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6080 return (PyObject *) u; |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6081 |
| 51216 | 6082 nothing: |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6083 /* 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
|
6084 if (PyUnicode_CheckExact(self)) { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6085 Py_INCREF(self); |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6086 return (PyObject *) self; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6087 } |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6088 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
|
6089 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6090 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6091 /* --- Unicode Object Methods --------------------------------------------- */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6092 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6093 PyDoc_STRVAR(title__doc__, |
| 51216 | 6094 "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
|
6095 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6096 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
|
6097 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
|
6098 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6099 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
|
6100 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
|
6101 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6102 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
|
6103 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6104 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6105 PyDoc_STRVAR(capitalize__doc__, |
| 51216 | 6106 "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
|
6107 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6108 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
|
6109 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
|
6110 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6111 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
|
6112 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
|
6113 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6114 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
|
6115 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6116 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6117 #if 0 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6118 PyDoc_STRVAR(capwords__doc__, |
| 51216 | 6119 "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
|
6120 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6121 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
|
6122 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
|
6123 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6124 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
|
6125 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
|
6126 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6127 PyObject *list; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6128 PyObject *item; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6129 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
|
6130 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6131 /* Split into words */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6132 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
|
6133 if (!list) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6134 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6135 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6136 /* Capitalize each word */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6137 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
|
6138 item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 51216 | 6139 fixcapitalize); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6140 if (item == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6141 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6142 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
|
6143 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
|
6144 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6145 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6146 /* 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
|
6147 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
|
6148 |
| 51216 | 6149 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6150 Py_DECREF(list); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6151 return (PyObject *)item; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6152 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6153 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6154 |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6155 /* 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
|
6156 |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6157 static int |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6158 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
|
6159 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6160 Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6161 PyObject *uniobj; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6162 Py_UNICODE *unistr; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6163 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6164 uniobj = PyUnicode_FromObject(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6165 if (uniobj == NULL) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6166 PyErr_SetString(PyExc_TypeError, |
| 51216 | 6167 "The fill character cannot be converted to Unicode"); |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6168 return 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6169 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6170 if (PyUnicode_GET_SIZE(uniobj) != 1) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6171 PyErr_SetString(PyExc_TypeError, |
| 51216 | 6172 "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
|
6173 Py_DECREF(uniobj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6174 return 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6175 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6176 unistr = PyUnicode_AS_UNICODE(uniobj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6177 *fillcharloc = unistr[0]; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6178 Py_DECREF(uniobj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6179 return 1; |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6180 } |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6181 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6182 PyDoc_STRVAR(center__doc__, |
| 51216 | 6183 "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
|
6184 \n\ |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6185 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
|
6186 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
|
6187 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6188 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6189 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
|
6190 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6191 Py_ssize_t marg, left; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6192 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
|
6193 Py_UNICODE fillchar = ' '; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6194 |
|
36532
0c096be27000
Use correct PyArg_Parse format char for Py_ssize_t in unicode.center().
Thomas Wouters <thomas@python.org>
parents:
36519
diff
changeset
|
6195 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
|
6196 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6197 |
|
19885
72e6eb653312
More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19849
diff
changeset
|
6198 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
|
6199 Py_INCREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6200 return (PyObject*) self; |
|
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 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6203 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
|
6204 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
|
6205 |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6206 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
|
6207 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6208 |
|
13890
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6209 #if 0 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6210 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6211 /* 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
|
6212 module. The basic comparison should compare ordinals on a naive |
|
13946
7a99450da2d6
Clean up warning from Monterey compiler.
Trent Mick <trentm@activestate.com>
parents:
13890
diff
changeset
|
6213 basis (this is what Java does and thus JPython too). */ |
|
13890
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6214 |
| 13337 | 6215 /* speedy UTF-16 code point order comparison */ |
| 6216 /* gleaned from: */ | |
| 6217 /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ | |
| 6218 | |
|
13411
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
6219 static short utf16Fixup[32] = |
| 13337 | 6220 { |
|
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
|
6221 0, 0, 0, 0, 0, 0, 0, 0, |
| 13337 | 6222 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
|
6223 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
|
6224 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
| 13337 | 6225 }; |
| 6226 | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6227 static int |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6228 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
|
6229 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6230 Py_ssize_t len1, len2; |
| 13337 | 6231 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6232 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
|
6233 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
|
6234 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6235 len1 = str1->length; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6236 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
|
6237 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6238 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
|
6239 Py_UNICODE c1, c2; |
| 13337 | 6240 |
| 6241 c1 = *s1++; | |
| 6242 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
|
6243 |
| 51216 | 6244 if (c1 > (1<<11) * 26) |
| 6245 c1 += utf16Fixup[c1>>11]; | |
| 6246 if (c2 > (1<<11) * 26) | |
| 13337 | 6247 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
|
6248 /* 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
|
6249 |
|
8b7ff44b4669
experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents:
18278
diff
changeset
|
6250 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
|
6251 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
|
6252 |
| 13337 | 6253 len1--; len2--; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6254 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6255 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6256 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
|
6257 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6258 |
|
13890
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6259 #else |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6260 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6261 static int |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6262 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
|
6263 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6264 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
|
6265 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6266 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
|
6267 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
|
6268 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6269 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
|
6270 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
|
6271 |
|
13890
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6272 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
|
6273 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
|
6274 |
|
8b7ff44b4669
experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents:
18278
diff
changeset
|
6275 c1 = *s1++; |
|
8b7ff44b4669
experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents:
18278
diff
changeset
|
6276 c2 = *s2++; |
|
8b7ff44b4669
experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents:
18278
diff
changeset
|
6277 |
|
8b7ff44b4669
experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents:
18278
diff
changeset
|
6278 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
|
6279 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
|
6280 |
|
13890
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6281 len1--; len2--; |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6282 } |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6283 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6284 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
|
6285 } |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6286 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6287 #endif |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6288 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6289 int PyUnicode_Compare(PyObject *left, |
| 51216 | 6290 PyObject *right) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6291 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6292 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
|
6293 int result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6294 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6295 /* 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
|
6296 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
|
6297 if (u == NULL) |
| 51216 | 6298 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6299 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
|
6300 if (v == NULL) |
| 51216 | 6301 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6302 |
|
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
|
6303 /* 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
|
6304 if (v == u) { |
| 51216 | 6305 Py_DECREF(u); |
| 6306 Py_DECREF(v); | |
| 6307 return 0; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6308 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6309 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6310 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
|
6311 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6312 Py_DECREF(u); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6313 Py_DECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6314 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6315 |
| 51216 | 6316 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6317 Py_XDECREF(u); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6318 Py_XDECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6319 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6320 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6321 |
|
39487
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6322 PyObject *PyUnicode_RichCompare(PyObject *left, |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6323 PyObject *right, |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6324 int op) |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6325 { |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6326 int result; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6327 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6328 result = PyUnicode_Compare(left, right); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6329 if (result == -1 && PyErr_Occurred()) |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6330 goto onError; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6331 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6332 /* Convert the return value to a Boolean */ |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6333 switch (op) { |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6334 case Py_EQ: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6335 result = (result == 0); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6336 break; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6337 case Py_NE: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6338 result = (result != 0); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6339 break; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6340 case Py_LE: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6341 result = (result <= 0); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6342 break; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6343 case Py_GE: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6344 result = (result >= 0); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6345 break; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6346 case Py_LT: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6347 result = (result == -1); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6348 break; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6349 case Py_GT: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6350 result = (result == 1); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6351 break; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6352 } |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6353 return PyBool_FromLong(result); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6354 |
| 51216 | 6355 onError: |
|
39487
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6356 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6357 /* Standard case |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6358 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6359 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
|
6360 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
|
6361 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
|
6362 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
|
6363 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
|
6364 chance. |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6365 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6366 */ |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6367 if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6368 PyErr_Clear(); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6369 Py_INCREF(Py_NotImplemented); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6370 return Py_NotImplemented; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6371 } |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6372 if (op != Py_EQ && op != Py_NE) |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6373 return NULL; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6374 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6375 /* Equality comparison. |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6376 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6377 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
|
6378 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
|
6379 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6380 */ |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6381 if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6382 return NULL; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6383 PyErr_Clear(); |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6384 if (PyErr_Warn(PyExc_UnicodeWarning, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6385 (op == Py_EQ) ? |
|
39487
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6386 "Unicode equal comparison " |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6387 "failed to convert both arguments to Unicode - " |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6388 "interpreting them as being unequal" : |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6389 "Unicode unequal comparison " |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6390 "failed to convert both arguments to Unicode - " |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6391 "interpreting them as being unequal" |
| 51216 | 6392 ) < 0) |
|
39487
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6393 return NULL; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6394 result = (op == Py_NE); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6395 return PyBool_FromLong(result); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6396 } |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6397 |
|
11997
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6398 int PyUnicode_Contains(PyObject *container, |
| 51216 | 6399 PyObject *element) |
|
11997
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6400 { |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6401 PyObject *str, *sub; |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6402 int result; |
|
11997
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6403 |
|
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6404 /* Coerce the two arguments */ |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6405 sub = PyUnicode_FromObject(element); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6406 if (!sub) { |
| 51216 | 6407 PyErr_SetString(PyExc_TypeError, |
| 6408 "'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
|
6409 return -1; |
|
0ba711a496e6
needforspeed: fixed unicode "in" operator to use same implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38241
diff
changeset
|
6410 } |
|
0ba711a496e6
needforspeed: fixed unicode "in" operator to use same implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38241
diff
changeset
|
6411 |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6412 str = PyUnicode_FromObject(container); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6413 if (!str) { |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6414 Py_DECREF(sub); |
|
38243
0ba711a496e6
needforspeed: fixed unicode "in" operator to use same implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38241
diff
changeset
|
6415 return -1; |
|
0ba711a496e6
needforspeed: fixed unicode "in" operator to use same implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38241
diff
changeset
|
6416 } |
|
11997
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6417 |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6418 result = stringlib_contains_obj(str, sub); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6419 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6420 Py_DECREF(str); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6421 Py_DECREF(sub); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6422 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6423 return result; |
|
11997
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6424 } |
|
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6425 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6426 /* 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
|
6427 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6428 PyObject *PyUnicode_Concat(PyObject *left, |
| 51216 | 6429 PyObject *right) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6430 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6431 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
|
6432 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6433 /* 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
|
6434 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
|
6435 if (u == NULL) |
| 51216 | 6436 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6437 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
|
6438 if (v == NULL) |
| 51216 | 6439 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6440 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6441 /* Shortcuts */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6442 if (v == unicode_empty) { |
| 51216 | 6443 Py_DECREF(v); |
| 6444 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
|
6445 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6446 if (u == unicode_empty) { |
| 51216 | 6447 Py_DECREF(u); |
| 6448 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
|
6449 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6450 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6451 /* 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
|
6452 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
|
6453 if (w == NULL) |
| 51216 | 6454 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6455 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
|
6456 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
|
6457 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6458 Py_DECREF(u); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6459 Py_DECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6460 return (PyObject *)w; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6461 |
| 51216 | 6462 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6463 Py_XDECREF(u); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6464 Py_XDECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6465 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6466 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6467 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6468 PyDoc_STRVAR(count__doc__, |
| 51216 | 6469 "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
|
6470 \n\ |
|
38236
7e16fcce34a1
docstring tweaks: count counts non-overlapping substrings, not
Fredrik Lundh <fredrik@pythonware.com>
parents:
38182
diff
changeset
|
6471 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
|
6472 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
|
6473 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
|
6474 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6475 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6476 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
|
6477 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6478 PyUnicodeObject *substring; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6479 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
|
6480 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
|
6481 PyObject *result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6482 |
| 12629 | 6483 if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 51216 | 6484 _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
|
6485 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6486 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6487 substring = (PyUnicodeObject *)PyUnicode_FromObject( |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
6488 (PyObject *)substring); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6489 if (substring == NULL) |
| 51216 | 6490 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
|
6491 |
|
38402
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
6492 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
|
6493 |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
6494 result = PyInt_FromSsize_t( |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
6495 stringlib_count(self->str + start, end - start, |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
6496 substring->str, substring->length) |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
6497 ); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6498 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6499 Py_DECREF(substring); |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
6500 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6501 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6502 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6503 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6504 PyDoc_STRVAR(encode__doc__, |
| 51216 | 6505 "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
|
6506 \n\ |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6507 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
|
6508 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
|
6509 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
|
6510 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
|
6511 '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
|
6512 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
|
6513 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6514 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6515 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
|
6516 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6517 char *encoding = NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6518 char *errors = NULL; |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6519 PyObject *v; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6520 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6521 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
|
6522 return NULL; |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6523 v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6524 if (v == NULL) |
|
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6525 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
|
6526 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
|
6527 PyErr_Format(PyExc_TypeError, |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6528 "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
|
6529 "(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
|
6530 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
|
6531 Py_DECREF(v); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6532 return NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6533 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6534 return v; |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6535 |
| 51216 | 6536 onError: |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6537 return NULL; |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6538 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6539 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6540 PyDoc_STRVAR(decode__doc__, |
| 51216 | 6541 "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
|
6542 \n\ |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6543 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
|
6544 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
|
6545 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
|
6546 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
|
6547 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
|
6548 able to handle UnicodeDecodeErrors."); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6549 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6550 static PyObject * |
|
32571
bec0c7618625
Fix a copy&paste typo.
Marc-André Lemburg <mal@egenix.com>
parents:
32552
diff
changeset
|
6551 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
|
6552 { |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6553 char *encoding = NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6554 char *errors = NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6555 PyObject *v; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6556 |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6557 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
|
6558 return NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6559 v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6560 if (v == NULL) |
|
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6561 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
|
6562 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
|
6563 PyErr_Format(PyExc_TypeError, |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6564 "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
|
6565 "(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
|
6566 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
|
6567 Py_DECREF(v); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6568 return NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6569 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6570 return v; |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6571 |
| 51216 | 6572 onError: |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6573 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6574 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6575 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6576 PyDoc_STRVAR(expandtabs__doc__, |
| 51216 | 6577 "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
|
6578 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6579 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
|
6580 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
|
6581 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6582 static PyObject* |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6583 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
|
6584 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6585 Py_UNICODE *e; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6586 Py_UNICODE *p; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6587 Py_UNICODE *q; |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6588 Py_UNICODE *qe; |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6589 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
|
6590 PyUnicodeObject *u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6591 int tabsize = 8; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6592 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6593 if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 51216 | 6594 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6595 |
|
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
|
6596 /* 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
|
6597 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
|
6598 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
|
6599 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
|
6600 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
|
6601 if (*p == '\t') { |
| 51216 | 6602 if (tabsize > 0) { |
| 6603 incr = tabsize - (j % tabsize); /* cannot overflow */ | |
| 6604 if (j > PY_SSIZE_T_MAX - incr) | |
| 6605 goto overflow1; | |
| 6606 j += incr; | |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6607 } |
| 51216 | 6608 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6609 else { |
| 51216 | 6610 if (j > PY_SSIZE_T_MAX - 1) |
| 6611 goto overflow1; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6612 j++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6613 if (*p == '\n' || *p == '\r') { |
| 51216 | 6614 if (i > PY_SSIZE_T_MAX - j) |
| 6615 goto overflow1; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6616 i += j; |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6617 j = 0; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6618 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6619 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6620 |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6621 if (i > PY_SSIZE_T_MAX - j) |
| 51216 | 6622 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
|
6623 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6624 /* 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
|
6625 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
|
6626 if (!u) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6627 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6628 |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6629 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
|
6630 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
|
6631 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
|
6632 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6633 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
|
6634 if (*p == '\t') { |
| 51216 | 6635 if (tabsize > 0) { |
| 6636 i = tabsize - (j % tabsize); | |
| 6637 j += i; | |
| 6638 while (i--) { | |
| 6639 if (q >= qe) | |
| 6640 goto overflow2; | |
| 6641 *q++ = ' '; | |
| 6642 } | |
| 6643 } | |
| 6644 } | |
| 6645 else { | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6646 if (q >= qe) |
| 51216 | 6647 goto overflow2; |
| 6648 *q++ = *p; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6649 j++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6650 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
|
6651 j = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6652 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6653 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6654 return (PyObject*) u; |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6655 |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6656 overflow2: |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6657 Py_DECREF(u); |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6658 overflow1: |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6659 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
|
6660 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6661 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6662 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6663 PyDoc_STRVAR(find__doc__, |
| 51216 | 6664 "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
|
6665 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6666 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
|
6667 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
|
6668 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
|
6669 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6670 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
|
6671 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6672 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6673 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
|
6674 { |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6675 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
|
6676 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
|
6677 Py_ssize_t end; |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6678 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
|
6679 |
|
43886
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43710
diff
changeset
|
6680 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
|
6681 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6682 |
|
38441
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
6683 result = stringlib_find_slice( |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
6684 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
|
6685 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
|
6686 start, end |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
6687 ); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6688 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6689 Py_DECREF(substring); |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6690 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6691 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
|
6692 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6693 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6694 static PyObject * |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6695 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
|
6696 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6697 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
|
6698 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
|
6699 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6700 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6701 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6702 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
|
6703 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6704 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6705 static long |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6706 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
|
6707 { |
|
13522
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6708 /* 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
|
6709 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
|
6710 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
|
6711 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
|
6712 dictionary keys. */ |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6713 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6714 register Py_ssize_t len; |
|
13522
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6715 register Py_UNICODE *p; |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6716 register long x; |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6717 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6718 if (self->hash != -1) |
| 51216 | 6719 return self->hash; |
|
13522
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6720 len = PyUnicode_GET_SIZE(self); |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6721 p = PyUnicode_AS_UNICODE(self); |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6722 x = *p << 7; |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6723 while (--len >= 0) |
| 51216 | 6724 x = (1000003*x) ^ *p++; |
|
13522
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6725 x ^= PyUnicode_GET_SIZE(self); |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6726 if (x == -1) |
| 51216 | 6727 x = -2; |
|
13522
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6728 self->hash = x; |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6729 return x; |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6732 PyDoc_STRVAR(index__doc__, |
| 51216 | 6733 "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
|
6734 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6735 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
|
6736 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6737 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6738 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
|
6739 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6740 Py_ssize_t result; |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6741 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
|
6742 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
|
6743 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
|
6744 |
|
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43710
diff
changeset
|
6745 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
|
6746 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6747 |
|
38441
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
6748 result = stringlib_find_slice( |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
6749 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
|
6750 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
|
6751 start, end |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
6752 ); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6753 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6754 Py_DECREF(substring); |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6755 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6756 if (result < 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6757 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
|
6758 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6759 } |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6760 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6761 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
|
6762 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6763 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6764 PyDoc_STRVAR(islower__doc__, |
| 51216 | 6765 "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
|
6766 \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
|
6767 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
|
6768 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
|
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* |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19121
diff
changeset
|
6771 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
|
6772 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6773 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
|
6774 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
|
6775 int cased; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6776 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6777 /* 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
|
6778 if (PyUnicode_GET_SIZE(self) == 1) |
| 51216 | 6779 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
|
6780 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6781 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6782 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6783 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6784 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6785 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
|
6786 cased = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6787 for (; p < e; p++) { |
| 51216 | 6788 register const Py_UNICODE ch = *p; |
| 6789 | |
| 6790 if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) | |
| 6791 return PyBool_FromLong(0); | |
| 6792 else if (!cased && Py_UNICODE_ISLOWER(ch)) | |
| 6793 cased = 1; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6794 } |
|
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
|
6795 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
|
6796 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6797 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6798 PyDoc_STRVAR(isupper__doc__, |
| 51216 | 6799 "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
|
6800 \n\ |
|
30707
9e21e56bf5f3
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30653
diff
changeset
|
6801 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
|
6802 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
|
6803 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6804 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
|
6805 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
|
6806 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6807 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
|
6808 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
|
6809 int cased; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6810 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6811 /* 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
|
6812 if (PyUnicode_GET_SIZE(self) == 1) |
| 51216 | 6813 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
|
6814 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6815 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6816 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6817 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6818 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6819 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
|
6820 cased = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6821 for (; p < e; p++) { |
| 51216 | 6822 register const Py_UNICODE ch = *p; |
| 6823 | |
| 6824 if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) | |
| 6825 return PyBool_FromLong(0); | |
| 6826 else if (!cased && Py_UNICODE_ISUPPER(ch)) | |
| 6827 cased = 1; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6828 } |
|
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
|
6829 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
|
6830 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6831 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6832 PyDoc_STRVAR(istitle__doc__, |
| 51216 | 6833 "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
|
6834 \n\ |
|
30707
9e21e56bf5f3
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30653
diff
changeset
|
6835 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
|
6836 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
|
6837 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
|
6838 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
|
6839 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6840 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
|
6841 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
|
6842 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6843 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
|
6844 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
|
6845 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
|
6846 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6847 /* 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
|
6848 if (PyUnicode_GET_SIZE(self) == 1) |
| 51216 | 6849 return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 6850 (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
|
6851 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6852 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6853 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6854 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6855 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6856 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
|
6857 cased = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6858 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
|
6859 for (; p < e; p++) { |
| 51216 | 6860 register const Py_UNICODE ch = *p; |
| 6861 | |
| 6862 if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { | |
| 6863 if (previous_is_cased) | |
| 6864 return PyBool_FromLong(0); | |
| 6865 previous_is_cased = 1; | |
| 6866 cased = 1; | |
| 6867 } | |
| 6868 else if (Py_UNICODE_ISLOWER(ch)) { | |
| 6869 if (!previous_is_cased) | |
| 6870 return PyBool_FromLong(0); | |
| 6871 previous_is_cased = 1; | |
| 6872 cased = 1; | |
| 6873 } | |
| 6874 else | |
| 6875 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
|
6876 } |
|
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
|
6877 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
|
6878 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6879 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6880 PyDoc_STRVAR(isspace__doc__, |
| 51216 | 6881 "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
|
6882 \n\ |
|
30707
9e21e56bf5f3
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30653
diff
changeset
|
6883 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
|
6884 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
|
6885 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6886 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
|
6887 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
|
6888 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6889 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
|
6890 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
|
6891 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6892 /* 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
|
6893 if (PyUnicode_GET_SIZE(self) == 1 && |
| 51216 | 6894 Py_UNICODE_ISSPACE(*p)) |
| 6895 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
|
6896 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6897 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6898 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6899 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6900 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6901 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
|
6902 for (; p < e; p++) { |
| 51216 | 6903 if (!Py_UNICODE_ISSPACE(*p)) |
| 6904 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
|
6905 } |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6906 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
|
6907 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6908 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6909 PyDoc_STRVAR(isalpha__doc__, |
| 51216 | 6910 "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
|
6911 \n\ |
|
30707
9e21e56bf5f3
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30653
diff
changeset
|
6912 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
|
6913 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
|
6914 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6915 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
|
6916 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
|
6917 { |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6918 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
|
6919 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
|
6920 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6921 /* 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
|
6922 if (PyUnicode_GET_SIZE(self) == 1 && |
| 51216 | 6923 Py_UNICODE_ISALPHA(*p)) |
| 6924 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
|
6925 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6926 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6927 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6928 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
|
6929 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6930 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
|
6931 for (; p < e; p++) { |
| 51216 | 6932 if (!Py_UNICODE_ISALPHA(*p)) |
| 6933 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
|
6934 } |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6935 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
|
6936 } |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6937 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6938 PyDoc_STRVAR(isalnum__doc__, |
| 51216 | 6939 "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
|
6940 \n\ |
|
30707
9e21e56bf5f3
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30653
diff
changeset
|
6941 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
|
6942 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
|
6943 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6944 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
|
6945 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
|
6946 { |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6947 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
|
6948 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
|
6949 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6950 /* 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
|
6951 if (PyUnicode_GET_SIZE(self) == 1 && |
| 51216 | 6952 Py_UNICODE_ISALNUM(*p)) |
| 6953 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
|
6954 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6955 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6956 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6957 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
|
6958 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6959 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
|
6960 for (; p < e; p++) { |
| 51216 | 6961 if (!Py_UNICODE_ISALNUM(*p)) |
| 6962 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
|
6963 } |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6964 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
|
6965 } |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6966 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6967 PyDoc_STRVAR(isdecimal__doc__, |
| 51216 | 6968 "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
|
6969 \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
|
6970 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
|
6971 False otherwise."); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6972 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6973 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
|
6974 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
|
6975 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6976 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
|
6977 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
|
6978 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6979 /* 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
|
6980 if (PyUnicode_GET_SIZE(self) == 1 && |
| 51216 | 6981 Py_UNICODE_ISDECIMAL(*p)) |
| 6982 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
|
6983 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6984 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6985 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6986 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6987 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6988 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
|
6989 for (; p < e; p++) { |
| 51216 | 6990 if (!Py_UNICODE_ISDECIMAL(*p)) |
| 6991 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
|
6992 } |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
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 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6995 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6996 PyDoc_STRVAR(isdigit__doc__, |
| 51216 | 6997 "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
|
6998 \n\ |
|
30707
9e21e56bf5f3
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30653
diff
changeset
|
6999 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
|
7000 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
|
7001 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7002 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
|
7003 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
|
7004 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7005 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
|
7006 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
|
7007 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7008 /* 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
|
7009 if (PyUnicode_GET_SIZE(self) == 1 && |
| 51216 | 7010 Py_UNICODE_ISDIGIT(*p)) |
| 7011 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
|
7012 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
7013 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
7014 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 7015 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
7016 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7017 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
|
7018 for (; p < e; p++) { |
| 51216 | 7019 if (!Py_UNICODE_ISDIGIT(*p)) |
| 7020 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
|
7021 } |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
7022 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
|
7023 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7024 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7025 PyDoc_STRVAR(isnumeric__doc__, |
| 51216 | 7026 "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
|
7027 \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
|
7028 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
|
7029 False otherwise."); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7030 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7031 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
|
7032 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
|
7033 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7034 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
|
7035 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
|
7036 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7037 /* 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
|
7038 if (PyUnicode_GET_SIZE(self) == 1 && |
| 51216 | 7039 Py_UNICODE_ISNUMERIC(*p)) |
| 7040 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
|
7041 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
7042 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
7043 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 7044 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
7045 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7046 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
|
7047 for (; p < e; p++) { |
| 51216 | 7048 if (!Py_UNICODE_ISNUMERIC(*p)) |
| 7049 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
|
7050 } |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
7051 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
|
7052 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7053 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7054 PyDoc_STRVAR(join__doc__, |
| 51216 | 7055 "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
|
7056 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7057 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
|
7058 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
|
7059 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7060 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
|
7061 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
|
7062 { |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19121
diff
changeset
|
7063 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
|
7064 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7065 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7066 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
|
7067 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
|
7068 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7069 return self->length; |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7072 PyDoc_STRVAR(ljust__doc__, |
| 51216 | 7073 "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
|
7074 \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
|
7075 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
|
7076 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
|
7077 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7078 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7079 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
|
7080 { |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
7081 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
|
7082 Py_UNICODE fillchar = ' '; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
7083 |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
7084 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
|
7085 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7086 |
|
19885
72e6eb653312
More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19849
diff
changeset
|
7087 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
|
7088 Py_INCREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7089 return (PyObject*) self; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7090 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7091 |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
7092 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
|
7093 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7094 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7095 PyDoc_STRVAR(lower__doc__, |
| 51216 | 7096 "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
|
7097 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7098 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
|
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_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
|
7102 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7103 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
|
7104 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7105 |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7106 #define LEFTSTRIP 0 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7107 #define RIGHTSTRIP 1 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7108 #define BOTHSTRIP 2 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7109 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7110 /* Arrays indexed by above */ |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7111 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
|
7112 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7113 #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
|
7114 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7115 /* 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
|
7116 PyObject * |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7117 _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
|
7118 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7119 Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7120 Py_ssize_t len = PyUnicode_GET_SIZE(self); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7121 Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7122 Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7123 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
|
7124 |
| 51216 | 7125 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
|
7126 |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7127 i = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7128 if (striptype != RIGHTSTRIP) { |
| 51216 | 7129 while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7130 i++; | |
| 7131 } | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7132 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7133 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7134 j = len; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7135 if (striptype != LEFTSTRIP) { |
| 51216 | 7136 do { |
| 7137 j--; | |
| 7138 } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); | |
| 7139 j++; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7140 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7141 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7142 if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 51216 | 7143 Py_INCREF(self); |
| 7144 return (PyObject*)self; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7145 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7146 else |
| 51216 | 7147 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
|
7148 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7149 |
|
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 static PyObject * |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7152 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
|
7153 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7154 Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7155 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
|
7156 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7157 i = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7158 if (striptype != RIGHTSTRIP) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7159 while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7160 i++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7161 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7162 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7163 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7164 j = len; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7165 if (striptype != LEFTSTRIP) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7166 do { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7167 j--; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7168 } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7169 j++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7170 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7171 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7172 if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7173 Py_INCREF(self); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7174 return (PyObject*)self; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7175 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7176 else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7177 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
|
7178 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7179 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7180 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7181 static PyObject * |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7182 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
|
7183 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7184 PyObject *sep = NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7185 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7186 if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7187 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7188 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7189 if (sep != NULL && sep != Py_None) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7190 if (PyUnicode_Check(sep)) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7191 return _PyUnicode_XStrip(self, striptype, sep); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7192 else if (PyString_Check(sep)) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7193 PyObject *res; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7194 sep = PyUnicode_FromObject(sep); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7195 if (sep==NULL) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7196 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7197 res = _PyUnicode_XStrip(self, striptype, sep); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7198 Py_DECREF(sep); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7199 return res; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7200 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7201 else { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7202 PyErr_Format(PyExc_TypeError, |
| 51216 | 7203 "%s arg must be None, unicode or str", |
| 7204 STRIPNAME(striptype)); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7205 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7206 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7207 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7208 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7209 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
|
7210 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7211 |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7212 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7213 PyDoc_STRVAR(strip__doc__, |
| 51216 | 7214 "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
|
7215 \n\ |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7216 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
|
7217 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
|
7218 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
|
7219 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
|
7220 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7221 static PyObject * |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7222 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
|
7223 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7224 if (PyTuple_GET_SIZE(args) == 0) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7225 return do_strip(self, BOTHSTRIP); /* Common case */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7226 else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7227 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
|
7228 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7229 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7230 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7231 PyDoc_STRVAR(lstrip__doc__, |
| 51216 | 7232 "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
|
7233 \n\ |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7234 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
|
7235 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
|
7236 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
|
7237 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7238 static PyObject * |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7239 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
|
7240 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7241 if (PyTuple_GET_SIZE(args) == 0) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7242 return do_strip(self, LEFTSTRIP); /* Common case */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7243 else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7244 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
|
7245 } |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7248 PyDoc_STRVAR(rstrip__doc__, |
| 51216 | 7249 "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
|
7250 \n\ |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7251 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
|
7252 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
|
7253 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
|
7254 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7255 static PyObject * |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7256 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
|
7257 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7258 if (PyTuple_GET_SIZE(args) == 0) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7259 return do_strip(self, RIGHTSTRIP); /* Common case */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7260 else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7261 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
|
7262 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7263 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7264 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7265 static PyObject* |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7266 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
|
7267 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7268 PyUnicodeObject *u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7269 Py_UNICODE *p; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7270 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
|
7271 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
|
7272 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7273 if (len < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7274 len = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7275 |
|
19885
72e6eb653312
More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19849
diff
changeset
|
7276 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
|
7277 /* 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
|
7278 Py_INCREF(str); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7279 return (PyObject*) str; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7280 } |
|
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
|
7281 |
|
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
|
7282 /* 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
|
7283 * 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
|
7284 */ |
|
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
|
7285 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
|
7286 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
|
7287 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
|
7288 "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
|
7289 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
|
7290 } |
|
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
|
7291 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
|
7292 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
|
7293 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
|
7294 "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
|
7295 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
|
7296 } |
|
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
|
7297 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
|
7298 if (!u) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7299 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7300 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7301 p = u->str; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7302 |
|
38238
feb00df91922
needforspeed: speed up unicode repeat, unicode string copy
Fredrik Lundh <fredrik@pythonware.com>
parents:
38236
diff
changeset
|
7303 if (str->length == 1 && len > 0) { |
|
feb00df91922
needforspeed: speed up unicode repeat, unicode string copy
Fredrik Lundh <fredrik@pythonware.com>
parents:
38236
diff
changeset
|
7304 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
|
7305 } else { |
| 51216 | 7306 Py_ssize_t done = 0; /* number of characters copied this far */ |
| 7307 if (done < nchars) { | |
|
38238
feb00df91922
needforspeed: speed up unicode repeat, unicode string copy
Fredrik Lundh <fredrik@pythonware.com>
parents:
38236
diff
changeset
|
7308 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
|
7309 done = str->length; |
| 51216 | 7310 } |
| 7311 while (done < nchars) { | |
|
46062
09adff3d6691
Try to fix a bunch of compiler warnings on Win64.
Neal Norwitz <nnorwitz@gmail.com>
parents:
46059
diff
changeset
|
7312 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
|
7313 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
|
7314 done += n; |
| 51216 | 7315 } |
|
38239
604af867c65f
needforspeed: use memcpy for "long" strings; use a better algorithm
Fredrik Lundh <fredrik@pythonware.com>
parents:
38238
diff
changeset
|
7316 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7317 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7318 return (PyObject*) u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7319 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7320 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7321 PyObject *PyUnicode_Replace(PyObject *obj, |
| 51216 | 7322 PyObject *subobj, |
| 7323 PyObject *replobj, | |
| 7324 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
|
7325 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7326 PyObject *self; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7327 PyObject *str1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7328 PyObject *str2; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7329 PyObject *result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7330 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7331 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
|
7332 if (self == NULL) |
| 51216 | 7333 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7334 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
|
7335 if (str1 == NULL) { |
| 51216 | 7336 Py_DECREF(self); |
| 7337 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7338 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7339 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
|
7340 if (str2 == NULL) { |
| 51216 | 7341 Py_DECREF(self); |
| 7342 Py_DECREF(str1); | |
| 7343 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7344 } |
|
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
|
7345 result = replace((PyUnicodeObject *)self, |
| 51216 | 7346 (PyUnicodeObject *)str1, |
| 7347 (PyUnicodeObject *)str2, | |
| 7348 maxcount); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7349 Py_DECREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7350 Py_DECREF(str1); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7351 Py_DECREF(str2); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7352 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7353 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7354 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7355 PyDoc_STRVAR(replace__doc__, |
| 51216 | 7356 "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
|
7357 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7358 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
|
7359 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
|
7360 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
|
7361 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7362 static PyObject* |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7363 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
|
7364 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7365 PyUnicodeObject *str1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7366 PyUnicodeObject *str2; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7367 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
|
7368 PyObject *result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7369 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7370 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
|
7371 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7372 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
|
7373 if (str1 == NULL) |
| 51216 | 7374 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7375 str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
|
27774
bfc524379307
Fix two refcounting bugs
Walter Dörwald <walter@livinglogic.de>
parents:
27555
diff
changeset
|
7376 if (str2 == NULL) { |
| 51216 | 7377 Py_DECREF(str1); |
| 7378 return NULL; | |
|
27774
bfc524379307
Fix two refcounting bugs
Walter Dörwald <walter@livinglogic.de>
parents:
27555
diff
changeset
|
7379 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7380 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7381 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
|
7382 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7383 Py_DECREF(str1); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7384 Py_DECREF(str2); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7385 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7386 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7387 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7388 static |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7389 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
|
7390 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7391 return unicodeescape_string(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 7392 PyUnicode_GET_SIZE(unicode), |
| 7393 1); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7394 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7395 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7396 PyDoc_STRVAR(rfind__doc__, |
| 51216 | 7397 "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
|
7398 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7399 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
|
7400 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
|
7401 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
|
7402 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7403 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
|
7404 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7405 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7406 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
|
7407 { |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
7408 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
|
7409 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
|
7410 Py_ssize_t end; |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
7411 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
|
7412 |
|
43886
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43710
diff
changeset
|
7413 if (!_ParseTupleFinds(args, &substring, &start, &end)) |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7414 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7415 |
|
38441
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
7416 result = stringlib_rfind_slice( |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
7417 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
|
7418 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
|
7419 start, end |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
7420 ); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7421 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7422 Py_DECREF(substring); |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
7423 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
7424 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
|
7425 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7426 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7427 PyDoc_STRVAR(rindex__doc__, |
| 51216 | 7428 "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
|
7429 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7430 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
|
7431 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7432 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7433 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
|
7434 { |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
7435 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
|
7436 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
|
7437 Py_ssize_t end; |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
7438 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
|
7439 |
|
43886
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43710
diff
changeset
|
7440 if (!_ParseTupleFinds(args, &substring, &start, &end)) |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
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 |
|
38441
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
7443 result = stringlib_rfind_slice( |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
7444 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
|
7445 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
|
7446 start, end |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
7447 ); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7448 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7449 Py_DECREF(substring); |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
7450 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7451 if (result < 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7452 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
|
7453 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7454 } |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7455 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
|
7456 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7457 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7458 PyDoc_STRVAR(rjust__doc__, |
| 51216 | 7459 "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
|
7460 \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
|
7461 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
|
7462 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
|
7463 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7464 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7465 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
|
7466 { |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
7467 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
|
7468 Py_UNICODE fillchar = ' '; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
7469 |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
7470 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
|
7471 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7472 |
|
19885
72e6eb653312
More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19849
diff
changeset
|
7473 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
|
7474 Py_INCREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7475 return (PyObject*) self; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7476 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7477 |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
7478 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
|
7479 } |
|
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 static PyObject* |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7482 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
|
7483 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7484 /* standard clamping */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7485 if (start < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7486 start = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7487 if (end < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7488 end = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7489 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
|
7490 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
|
7491 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
|
7492 /* 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
|
7493 Py_INCREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7494 return (PyObject*) self; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7495 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7496 if (start > end) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7497 start = end; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7498 /* copy slice */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7499 return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
| 51216 | 7500 end - start); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7501 } |
|
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 PyObject *PyUnicode_Split(PyObject *s, |
| 51216 | 7504 PyObject *sep, |
| 7505 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
|
7506 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7507 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
|
7508 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7509 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
|
7510 if (s == NULL) |
| 51216 | 7511 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7512 if (sep != NULL) { |
| 51216 | 7513 sep = PyUnicode_FromObject(sep); |
| 7514 if (sep == NULL) { | |
| 7515 Py_DECREF(s); | |
| 7516 return NULL; | |
| 7517 } | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7518 } |
|
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 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
|
7521 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7522 Py_DECREF(s); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7523 Py_XDECREF(sep); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7524 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7525 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7526 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7527 PyDoc_STRVAR(split__doc__, |
| 51216 | 7528 "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
|
7529 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7530 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
|
7531 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
|
7532 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
|
7533 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
|
7534 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
|
7535 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7536 static PyObject* |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7537 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
|
7538 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7539 PyObject *substring = Py_None; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7540 Py_ssize_t maxcount = -1; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7541 |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7542 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
|
7543 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7544 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7545 if (substring == Py_None) |
| 51216 | 7546 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
|
7547 else if (PyUnicode_Check(substring)) |
| 51216 | 7548 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
|
7549 else |
| 51216 | 7550 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
|
7551 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7552 |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7553 PyObject * |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7554 PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7555 { |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7556 PyObject* str_obj; |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7557 PyObject* sep_obj; |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7558 PyObject* out; |
|
38383
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7559 |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7560 str_obj = PyUnicode_FromObject(str_in); |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7561 if (!str_obj) |
| 51216 | 7562 return NULL; |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7563 sep_obj = PyUnicode_FromObject(sep_in); |
|
38383
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7564 if (!sep_obj) { |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7565 Py_DECREF(str_obj); |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7566 return NULL; |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7567 } |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7568 |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
7569 out = stringlib_partition( |
|
38383
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7570 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
|
7571 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
|
7572 ); |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7573 |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7574 Py_DECREF(sep_obj); |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7575 Py_DECREF(str_obj); |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7576 |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7577 return out; |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7578 } |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7579 |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7580 |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7581 PyObject * |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7582 PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7583 { |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7584 PyObject* str_obj; |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7585 PyObject* sep_obj; |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7586 PyObject* out; |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7587 |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7588 str_obj = PyUnicode_FromObject(str_in); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7589 if (!str_obj) |
| 51216 | 7590 return NULL; |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7591 sep_obj = PyUnicode_FromObject(sep_in); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7592 if (!sep_obj) { |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7593 Py_DECREF(str_obj); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7594 return NULL; |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7595 } |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7596 |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
7597 out = stringlib_rpartition( |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7598 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
|
7599 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
|
7600 ); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7601 |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7602 Py_DECREF(sep_obj); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7603 Py_DECREF(str_obj); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7604 |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7605 return out; |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7606 } |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7607 |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7608 PyDoc_STRVAR(partition__doc__, |
| 51216 | 7609 "S.partition(sep) -> (head, sep, tail)\n\ |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7610 \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
|
7611 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
|
7612 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
|
7613 found, return S and two empty strings."); |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7614 |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7615 static PyObject* |
|
38338
958f91684209
needforspeed: use METH_O for argument handling, which made partition some
Fredrik Lundh <fredrik@pythonware.com>
parents:
38335
diff
changeset
|
7616 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
|
7617 { |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7618 return PyUnicode_Partition((PyObject *)self, separator); |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7619 } |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7620 |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7621 PyDoc_STRVAR(rpartition__doc__, |
| 51216 | 7622 "S.rpartition(sep) -> (tail, sep, head)\n\ |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7623 \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
|
7624 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
|
7625 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
|
7626 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
|
7627 |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7628 static PyObject* |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7629 unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7630 { |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7631 return PyUnicode_RPartition((PyObject *)self, separator); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7632 } |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7633 |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7634 PyObject *PyUnicode_RSplit(PyObject *s, |
| 51216 | 7635 PyObject *sep, |
| 7636 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
|
7637 { |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7638 PyObject *result; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7639 |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7640 s = PyUnicode_FromObject(s); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7641 if (s == NULL) |
| 51216 | 7642 return NULL; |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7643 if (sep != NULL) { |
| 51216 | 7644 sep = PyUnicode_FromObject(sep); |
| 7645 if (sep == NULL) { | |
| 7646 Py_DECREF(s); | |
| 7647 return NULL; | |
| 7648 } | |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7649 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7650 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7651 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
|
7652 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7653 Py_DECREF(s); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7654 Py_XDECREF(sep); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7655 return result; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7656 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7657 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7658 PyDoc_STRVAR(rsplit__doc__, |
| 51216 | 7659 "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
|
7660 \n\ |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7661 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
|
7662 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
|
7663 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
|
7664 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
|
7665 is a separator."); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7666 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7667 static PyObject* |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7668 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
|
7669 { |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7670 PyObject *substring = Py_None; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7671 Py_ssize_t maxcount = -1; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7672 |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7673 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
|
7674 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7675 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7676 if (substring == Py_None) |
| 51216 | 7677 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
|
7678 else if (PyUnicode_Check(substring)) |
| 51216 | 7679 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
|
7680 else |
| 51216 | 7681 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
|
7682 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7683 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7684 PyDoc_STRVAR(splitlines__doc__, |
| 51216 | 7685 "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
|
7686 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7687 Return a list of the lines in S, breaking at line boundaries.\n\ |
| 12369 | 7688 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
|
7689 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
|
7690 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7691 static PyObject* |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7692 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
|
7693 { |
| 12369 | 7694 int keepends = 0; |
| 7695 | |
| 7696 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
|
7697 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7698 |
| 12369 | 7699 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
|
7700 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7701 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7702 static |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7703 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
|
7704 { |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
7705 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
|
7706 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7707 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7708 PyDoc_STRVAR(swapcase__doc__, |
| 51216 | 7709 "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
|
7710 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7711 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
|
7712 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
|
7713 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7714 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
|
7715 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
|
7716 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7717 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
|
7718 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7719 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7720 PyDoc_STRVAR(translate__doc__, |
| 51216 | 7721 "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
|
7722 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7723 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
|
7724 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
|
7725 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
|
7726 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
|
7727 are deleted."); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7728 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7729 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
|
7730 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
|
7731 { |
|
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
|
7732 return PyUnicode_TranslateCharmap(self->str, |
| 51216 | 7733 self->length, |
| 7734 table, | |
| 7735 "ignore"); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7736 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7737 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7738 PyDoc_STRVAR(upper__doc__, |
| 51216 | 7739 "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
|
7740 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7741 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
|
7742 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7743 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
|
7744 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
|
7745 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7746 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
|
7747 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7748 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7749 PyDoc_STRVAR(zfill__doc__, |
| 51216 | 7750 "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
|
7751 \n\ |
|
49115
6f08278bf8b1
Fix varname in docstring. #3822.
Georg Brandl <georg@python.org>
parents:
48117
diff
changeset
|
7752 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
|
7753 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
|
7754 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7755 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7756 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
|
7757 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7758 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
|
7759 PyUnicodeObject *u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7760 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7761 Py_ssize_t width; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7762 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
|
7763 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7764 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7765 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
|
7766 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
|
7767 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
|
7768 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
|
7769 } |
|
19ea00feda33
Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents:
23045
diff
changeset
|
7770 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
|
7771 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
|
7772 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
|
7773 PyUnicode_GET_SIZE(self) |
| 51216 | 7774 ); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7775 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7776 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7777 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
|
7778 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7779 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
|
7780 |
|
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
|
7781 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
|
7782 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
|
7783 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7784 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
|
7785 /* 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
|
7786 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
|
7787 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
|
7788 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7789 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7790 return (PyObject*) u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7791 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7792 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7793 #if 0 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7794 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
|
7795 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
|
7796 { |
|
920a7633ae08
Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents:
45053
diff
changeset
|
7797 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
|
7798 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7799 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7800 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7801 PyDoc_STRVAR(startswith__doc__, |
| 51216 | 7802 "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
|
7803 \n\ |
|
28559
aff080f2efb4
Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents:
28494
diff
changeset
|
7804 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
|
7805 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
|
7806 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
|
7807 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
|
7808 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7809 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7810 unicode_startswith(PyUnicodeObject *self, |
| 51216 | 7811 PyObject *args) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7812 { |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7813 PyObject *subobj; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7814 PyUnicodeObject *substring; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7815 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
|
7816 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
|
7817 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
|
7818 |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7819 if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
| 51216 | 7820 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 7821 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
|
7822 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
|
7823 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
|
7824 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
|
7825 substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 51216 | 7826 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
|
7827 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
|
7828 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
|
7829 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
|
7830 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
|
7831 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
|
7832 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
|
7833 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7834 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7835 /* 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
|
7836 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
|
7837 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7838 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
|
7839 if (substring == NULL) |
| 51216 | 7840 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
|
7841 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
|
7842 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
|
7843 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
|
7844 } |
|
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(endswith__doc__, |
| 51216 | 7848 "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
|
7849 \n\ |
|
28559
aff080f2efb4
Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents:
28494
diff
changeset
|
7850 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
|
7851 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
|
7852 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
|
7853 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
|
7854 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7855 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7856 unicode_endswith(PyUnicodeObject *self, |
| 51216 | 7857 PyObject *args) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7858 { |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7859 PyObject *subobj; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7860 PyUnicodeObject *substring; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7861 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
|
7862 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
|
7863 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
|
7864 |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7865 if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 51216 | 7866 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 7867 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
|
7868 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
|
7869 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
|
7870 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
|
7871 substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 51216 | 7872 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
|
7873 if (substring == NULL) |
| 51216 | 7874 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
|
7875 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
|
7876 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
|
7877 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
|
7878 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
|
7879 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7880 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7881 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
|
7882 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7883 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
|
7884 if (substring == NULL) |
| 51216 | 7885 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
|
7886 |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7887 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
|
7888 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
|
7889 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
|
7890 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7891 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7892 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7893 /* 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
|
7894 #include "stringlib/string_format.h" |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7895 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7896 PyDoc_STRVAR(format__doc__, |
| 51216 | 7897 "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
|
7898 \n\ |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7899 "); |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7900 |
|
47392
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7901 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
|
7902 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
|
7903 { |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7904 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
|
7905 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
|
7906 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
|
7907 |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7908 /* 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
|
7909 /* 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
|
7910 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
|
7911 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
|
7912 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
|
7913 PyErr_Format(PyExc_TypeError, "__format__ arg must be str " |
| 51216 | 7914 "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
|
7915 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
|
7916 } |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7917 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
|
7918 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
|
7919 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
|
7920 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
|
7921 |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7922 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
|
7923 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
|
7924 PyUnicode_GET_SIZE(format_spec)); |
| 51216 | 7925 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
|
7926 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
|
7927 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
|
7928 } |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7929 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7930 PyDoc_STRVAR(p_format__doc__, |
| 51216 | 7931 "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
|
7932 \n\ |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7933 "); |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7934 |
|
47543
296ab1038b7f
Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47527
diff
changeset
|
7935 static PyObject * |
|
296ab1038b7f
Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47527
diff
changeset
|
7936 unicode__sizeof__(PyUnicodeObject *v) |
|
296ab1038b7f
Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47527
diff
changeset
|
7937 { |
|
47922
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47543
diff
changeset
|
7938 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
|
7939 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
|
7940 } |
|
296ab1038b7f
Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47527
diff
changeset
|
7941 |
|
296ab1038b7f
Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47527
diff
changeset
|
7942 PyDoc_STRVAR(sizeof__doc__, |
| 51216 | 7943 "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
|
7944 \n\ |
|
296ab1038b7f
Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47527
diff
changeset
|
7945 "); |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7946 |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7947 static PyObject * |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7948 unicode_getnewargs(PyUnicodeObject *v) |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7949 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7950 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
|
7951 } |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7952 |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7953 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7954 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
|
7955 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7956 /* 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
|
7957 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
|
7958 |
| 37306 | 7959 {"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
|
7960 {"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
|
7961 {"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
|
7962 {"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
|
7963 {"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
|
7964 {"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
|
7965 {"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
|
7966 {"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
|
7967 {"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
|
7968 {"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
|
7969 {"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
|
7970 {"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
|
7971 {"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
|
7972 {"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
|
7973 {"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
|
7974 {"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
|
7975 {"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
|
7976 /* {"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
|
7977 {"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
|
7978 {"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
|
7979 {"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
|
7980 {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7981 {"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
|
7982 {"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
|
7983 {"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
|
7984 {"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
|
7985 {"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
|
7986 {"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
|
7987 {"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
|
7988 {"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
|
7989 {"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
|
7990 {"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
|
7991 {"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
|
7992 {"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
|
7993 {"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
|
7994 {"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
|
7995 {"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
|
7996 {"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
|
7997 {"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
|
7998 {"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
|
7999 {"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
|
8000 {"__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
|
8001 {"_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
|
8002 {"_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
|
8003 {"__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
|
8004 #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
|
8005 {"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
|
8006 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8007 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8008 #if 0 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8009 /* 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
|
8010 {"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
|
8011 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8012 |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8013 {"__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
|
8014 {NULL, NULL} |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8015 }; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8016 |
|
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
|
8017 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
|
8018 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
|
8019 { |
| 51216 | 8020 if (!PyUnicode_Check(v)) { |
| 8021 Py_INCREF(Py_NotImplemented); | |
| 8022 return Py_NotImplemented; | |
| 8023 } | |
| 8024 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
|
8025 } |
|
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
|
8026 |
|
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
|
8027 static PyNumberMethods unicode_as_number = { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8028 0, /*nb_add*/ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8029 0, /*nb_subtract*/ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8030 0, /*nb_multiply*/ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8031 0, /*nb_divide*/ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8032 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
|
8033 }; |
|
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
|
8034 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8035 static PySequenceMethods unicode_as_sequence = { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8036 (lenfunc) unicode_length, /* sq_length */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8037 PyUnicode_Concat, /* sq_concat */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8038 (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8039 (ssizeargfunc) unicode_getitem, /* sq_item */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8040 (ssizessizeargfunc) unicode_slice, /* sq_slice */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8041 0, /* sq_ass_item */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8042 0, /* sq_ass_slice */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8043 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
|
8044 }; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8045 |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8046 static PyObject* |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8047 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
|
8048 { |
|
39491
2312e178b34d
Correct an accidentally removed previous patch.
Marc-André Lemburg <mal@egenix.com>
parents:
39487
diff
changeset
|
8049 if (PyIndex_Check(item)) { |
|
2312e178b34d
Correct an accidentally removed previous patch.
Marc-André Lemburg <mal@egenix.com>
parents:
39487
diff
changeset
|
8050 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
|
8051 if (i == -1 && PyErr_Occurred()) |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8052 return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8053 if (i < 0) |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
8054 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
|
8055 return unicode_getitem(self, i); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8056 } else if (PySlice_Check(item)) { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8057 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
|
8058 Py_UNICODE* source_buf; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8059 Py_UNICODE* result_buf; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8060 PyObject* result; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8061 |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
8062 if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
| 51216 | 8063 &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
|
8064 return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8065 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8066 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8067 if (slicelength <= 0) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8068 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
|
8069 } 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
|
8070 PyUnicode_CheckExact(self)) { |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42652
diff
changeset
|
8071 Py_INCREF(self); |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42652
diff
changeset
|
8072 return (PyObject *)self; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42652
diff
changeset
|
8073 } else if (step == 1) { |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42652
diff
changeset
|
8074 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
|
8075 } else { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8076 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
|
8077 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
|
8078 sizeof(Py_UNICODE)); |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8079 |
| 51216 | 8080 if (result_buf == NULL) |
| 8081 return PyErr_NoMemory(); | |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8082 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8083 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
|
8084 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
|
8085 } |
|
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
|
8086 |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8087 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
|
8088 PyObject_FREE(result_buf); |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8089 return result; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8090 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8091 } else { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8092 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
|
8093 return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8094 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8095 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8096 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8097 static PyMappingMethods unicode_as_mapping = { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8098 (lenfunc)unicode_length, /* mp_length */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8099 (binaryfunc)unicode_subscript, /* mp_subscript */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8100 (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
|
8101 }; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8102 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8103 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
|
8104 unicode_buffer_getreadbuf(PyUnicodeObject *self, |
| 51216 | 8105 Py_ssize_t index, |
| 8106 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
|
8107 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8108 if (index != 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8109 PyErr_SetString(PyExc_SystemError, |
| 51216 | 8110 "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
|
8111 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8112 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8113 *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
|
8114 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
|
8115 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8116 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8117 static Py_ssize_t |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8118 unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index, |
| 51216 | 8119 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
|
8120 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8121 PyErr_SetString(PyExc_TypeError, |
| 51216 | 8122 "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
|
8123 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8124 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8125 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8126 static int |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8127 unicode_buffer_getsegcount(PyUnicodeObject *self, |
| 51216 | 8128 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
|
8129 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8130 if (lenp) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8131 *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
|
8132 return 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8133 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8134 |
|
36519
d6d415df0df0
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36510
diff
changeset
|
8135 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
|
8136 unicode_buffer_getcharbuf(PyUnicodeObject *self, |
| 51216 | 8137 Py_ssize_t index, |
| 8138 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
|
8139 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8140 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
|
8141 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8142 if (index != 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8143 PyErr_SetString(PyExc_SystemError, |
| 51216 | 8144 "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
|
8145 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8146 } |
|
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
|
8147 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
|
8148 if (str == NULL) |
| 51216 | 8149 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
|
8150 *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
|
8151 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
|
8152 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8153 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8154 /* 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
|
8155 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8156 static PyObject * |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8157 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
|
8158 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8159 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
|
8160 if (argidx < arglen) { |
| 51216 | 8161 (*p_argidx)++; |
| 8162 if (arglen < 0) | |
| 8163 return args; | |
| 8164 else | |
| 8165 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
|
8166 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8167 PyErr_SetString(PyExc_TypeError, |
| 51216 | 8168 "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
|
8169 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8170 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8171 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8172 #define F_LJUST (1<<0) |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8173 #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
|
8174 #define F_BLANK (1<<2) |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8175 #define F_ALT (1<<3) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8176 #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
|
8177 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8178 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
|
8179 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
|
8180 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8181 register Py_ssize_t i; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8182 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
|
8183 for (i = len - 1; i >= 0; i--) |
| 51216 | 8184 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
|
8185 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8186 return len; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8187 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8188 |
|
36261
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8189 static int |
|
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8190 doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
|
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8191 { |
|
36508
7322dccba224
doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents:
36506
diff
changeset
|
8192 Py_ssize_t result; |
|
7322dccba224
doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents:
36506
diff
changeset
|
8193 |
|
36261
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8194 PyOS_ascii_formatd((char *)buffer, len, format, x); |
|
36508
7322dccba224
doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents:
36506
diff
changeset
|
8195 result = strtounicode(buffer, (char *)buffer); |
|
7322dccba224
doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents:
36506
diff
changeset
|
8196 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
|
8197 } |
|
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8198 |
|
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8199 static int |
|
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8200 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
|
8201 { |
|
36508
7322dccba224
doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents:
36506
diff
changeset
|
8202 Py_ssize_t result; |
|
7322dccba224
doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents:
36506
diff
changeset
|
8203 |
|
36261
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8204 PyOS_snprintf((char *)buffer, len, format, x); |
|
36508
7322dccba224
doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents:
36506
diff
changeset
|
8205 result = strtounicode(buffer, (char *)buffer); |
|
7322dccba224
doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents:
36506
diff
changeset
|
8206 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
|
8207 } |
|
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8208 |
|
24736
ff05eef3a6d1
Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents:
24703
diff
changeset
|
8209 /* 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
|
8210 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
|
8211 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
|
8212 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8213 static int |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8214 formatfloat(Py_UNICODE *buf, |
| 51216 | 8215 size_t buflen, |
| 8216 int flags, | |
| 8217 int prec, | |
| 8218 int type, | |
| 8219 PyObject *v) | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8220 { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8221 /* fmt = '%#.' + `prec` + `type` |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8222 worst case length = 3 + 10 (len of INT_MAX) + 1 = 14 (use 20)*/ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8223 char fmt[20]; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8224 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
|
8225 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8226 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
|
8227 if (x == -1.0 && PyErr_Occurred()) |
| 51216 | 8228 return -1; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8229 if (prec < 0) |
| 51216 | 8230 prec = 6; |
|
52411
bafad61cf302
Merged revisions 70684 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
52405
diff
changeset
|
8231 /* make sure that the decimal representation of precision really does |
|
bafad61cf302
Merged revisions 70684 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
52405
diff
changeset
|
8232 need at most 10 digits: platforms with sizeof(int) == 8 exist! */ |
|
bafad61cf302
Merged revisions 70684 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
52405
diff
changeset
|
8233 if (prec > 0x7fffffffL) { |
|
bafad61cf302
Merged revisions 70684 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
52405
diff
changeset
|
8234 PyErr_SetString(PyExc_OverflowError, |
|
bafad61cf302
Merged revisions 70684 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
52405
diff
changeset
|
8235 "outrageously large precision " |
|
bafad61cf302
Merged revisions 70684 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
52405
diff
changeset
|
8236 "for formatted float"); |
|
bafad61cf302
Merged revisions 70684 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
52405
diff
changeset
|
8237 return -1; |
|
bafad61cf302
Merged revisions 70684 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
52405
diff
changeset
|
8238 } |
|
bafad61cf302
Merged revisions 70684 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
52405
diff
changeset
|
8239 |
|
52405
423c9477d6ef
Merged revisions 70678 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
52254
diff
changeset
|
8240 if (type == 'f' && fabs(x) >= 1e50) |
| 51216 | 8241 type = 'g'; |
|
26825
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8242 /* 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
|
8243 |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8244 'g' formats: |
| 51216 | 8245 fmt = %#.<prec>g |
| 8246 buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp | |
| 8247 for any double rep.) | |
| 8248 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
|
8249 |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8250 'f' formats: |
| 51216 | 8251 buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 8252 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
|
8253 |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8254 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
|
8255 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
|
8256 |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8257 */ |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8258 if (((type == 'g' || type == 'G') && |
| 51216 | 8259 buflen <= (size_t)10 + (size_t)prec) || |
| 8260 (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { | |
| 8261 PyErr_SetString(PyExc_OverflowError, | |
| 8262 "formatted float is too long (precision too large?)"); | |
| 8263 return -1; | |
|
26825
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8264 } |
|
21082
c8d775b9ff92
formatfloat(), formatint(): Conversion of sprintf() to PyOS_snprintf()
Barry Warsaw <barry@python.org>
parents:
21067
diff
changeset
|
8265 PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 51216 | 8266 (flags&F_ALT) ? "#" : "", |
| 8267 prec, type); | |
|
36261
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8268 return doubletounicode(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
|
8269 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8270 |
|
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
|
8271 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
|
8272 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
|
8273 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8274 char *buf; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8275 int i, len; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8276 PyObject *str; /* temporary string object. */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8277 PyUnicodeObject *result; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8278 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8279 str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8280 if (!str) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8281 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8282 result = _PyUnicode_New(len); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8283 if (!result) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8284 Py_DECREF(str); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8285 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8286 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8287 for (i = 0; i < len; i++) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8288 result->str[i] = buf[i]; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8289 result->str[len] = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8290 Py_DECREF(str); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8291 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
|
8292 } |
|
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
|
8293 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8294 static int |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8295 formatint(Py_UNICODE *buf, |
| 51216 | 8296 size_t buflen, |
| 8297 int flags, | |
| 8298 int prec, | |
| 8299 int type, | |
| 8300 PyObject *v) | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8301 { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8302 /* 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
|
8303 * 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
|
8304 * + 1 + 1 |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8305 * = 24 |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8306 */ |
|
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
|
8307 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
|
8308 char *sign; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8309 long x; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8310 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8311 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
|
8312 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
|
8313 return -1; |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8314 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
|
8315 type = 'd'; |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8316 } |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8317 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
|
8318 sign = "-"; |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8319 else |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8320 sign = ""; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8321 if (prec < 0) |
|
22087
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8322 prec = 1; |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8323 |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8324 /* 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
|
8325 * 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
|
8326 */ |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8327 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
|
8328 PyErr_SetString(PyExc_OverflowError, |
| 51216 | 8329 "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
|
8330 return -1; |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8331 } |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8332 |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8333 if ((flags & F_ALT) && |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8334 (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
|
8335 /* 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
|
8336 * 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
|
8337 * - 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
|
8338 * 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
|
8339 * %#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
|
8340 * hex() function |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8341 * - 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
|
8342 * 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
|
8343 * (Metrowerks, Compaq Tru64) |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8344 * - 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
|
8345 * 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
|
8346 * 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
|
8347 * |
|
22087
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8348 * 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
|
8349 * 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
|
8350 * of %#x/%#X. |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8351 * |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8352 * 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
|
8353 * 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
|
8354 */ |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8355 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
|
8356 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
|
8357 } |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8358 else { |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8359 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
|
8360 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
|
8361 prec, type); |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8362 } |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8363 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
|
8364 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
|
8365 else |
|
36261
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8366 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
|
8367 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8368 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8369 static int |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8370 formatchar(Py_UNICODE *buf, |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8371 size_t buflen, |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8372 PyObject *v) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8373 { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8374 /* 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
|
8375 if (PyUnicode_Check(v)) { |
| 51216 | 8376 if (PyUnicode_GET_SIZE(v) != 1) |
| 8377 goto onError; | |
| 8378 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
|
8379 } |
|
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8380 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
8381 else if (PyString_Check(v)) { |
| 51216 | 8382 if (PyString_GET_SIZE(v) != 1) |
| 8383 goto onError; | |
| 8384 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
|
8385 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8386 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8387 else { |
| 51216 | 8388 /* 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
|
8389 long x; |
| 51216 | 8390 x = PyInt_AsLong(v); |
| 8391 if (x == -1 && PyErr_Occurred()) | |
| 8392 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
|
8393 #ifdef Py_UNICODE_WIDE |
| 51216 | 8394 if (x < 0 || x > 0x10ffff) { |
| 8395 PyErr_SetString(PyExc_OverflowError, | |
| 8396 "%c arg not in range(0x110000) " | |
| 8397 "(wide Python build)"); | |
| 8398 return -1; | |
| 8399 } | |
|
24737
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
8400 #else |
| 51216 | 8401 if (x < 0 || x > 0xffff) { |
| 8402 PyErr_SetString(PyExc_OverflowError, | |
| 8403 "%c arg not in range(0x10000) " | |
| 8404 "(narrow Python build)"); | |
| 8405 return -1; | |
| 8406 } | |
|
24737
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
8407 #endif |
| 51216 | 8408 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
|
8409 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8410 buf[1] = '\0'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8411 return 1; |
|
12899
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8412 |
| 51216 | 8413 onError: |
|
12899
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8414 PyErr_SetString(PyExc_TypeError, |
| 51216 | 8415 "%c requires int or char"); |
|
12899
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8416 return -1; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8417 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8418 |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8419 /* 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
|
8420 |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8421 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
|
8422 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
|
8423 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
|
8424 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
|
8425 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
|
8426 */ |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8427 #define FORMATBUFLEN (size_t)120 |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8428 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8429 PyObject *PyUnicode_Format(PyObject *format, |
| 51216 | 8430 PyObject *args) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8431 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8432 Py_UNICODE *fmt, *res; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8433 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
|
8434 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
|
8435 PyUnicodeObject *result = NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8436 PyObject *dict = NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8437 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
|
8438 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8439 if (format == NULL || args == NULL) { |
| 51216 | 8440 PyErr_BadInternalCall(); |
| 8441 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8442 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8443 uformat = PyUnicode_FromObject(format); |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
8444 if (uformat == NULL) |
| 51216 | 8445 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8446 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
|
8447 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
|
8448 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8449 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
|
8450 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
|
8451 if (result == NULL) |
| 51216 | 8452 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8453 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
|
8454 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8455 if (PyTuple_Check(args)) { |
| 51216 | 8456 arglen = PyTuple_Size(args); |
| 8457 argidx = 0; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8458 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8459 else { |
| 51216 | 8460 arglen = -1; |
| 8461 argidx = -2; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8462 } |
|
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
|
8463 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
|
8464 !PyObject_TypeCheck(args, &PyBaseString_Type)) |
| 51216 | 8465 dict = args; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8466 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8467 while (--fmtcnt >= 0) { |
| 51216 | 8468 if (*fmt != '%') { |
| 8469 if (--rescnt < 0) { | |
| 8470 rescnt = fmtcnt + 100; | |
| 8471 reslen += rescnt; | |
| 8472 if (_PyUnicode_Resize(&result, reslen) < 0) | |
| 8473 goto onError; | |
| 8474 res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; | |
| 8475 --rescnt; | |
| 8476 } | |
| 8477 *res++ = *fmt++; | |
| 8478 } | |
| 8479 else { | |
| 8480 /* Got a format specifier */ | |
| 8481 int flags = 0; | |
| 8482 Py_ssize_t width = -1; | |
| 8483 int prec = -1; | |
| 8484 Py_UNICODE c = '\0'; | |
| 8485 Py_UNICODE fill; | |
| 8486 int isnumok; | |
| 8487 PyObject *v = NULL; | |
| 8488 PyObject *temp = NULL; | |
| 8489 Py_UNICODE *pbuf; | |
| 8490 Py_UNICODE sign; | |
| 8491 Py_ssize_t len; | |
| 8492 Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ | |
| 8493 | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8494 fmt++; |
| 51216 | 8495 if (*fmt == '(') { |
| 8496 Py_UNICODE *keystart; | |
| 8497 Py_ssize_t keylen; | |
| 8498 PyObject *key; | |
| 8499 int pcount = 1; | |
| 8500 | |
| 8501 if (dict == NULL) { | |
| 8502 PyErr_SetString(PyExc_TypeError, | |
| 8503 "format requires a mapping"); | |
| 8504 goto onError; | |
| 8505 } | |
| 8506 ++fmt; | |
| 8507 --fmtcnt; | |
| 8508 keystart = fmt; | |
| 8509 /* Skip over balanced parentheses */ | |
| 8510 while (pcount > 0 && --fmtcnt >= 0) { | |
| 8511 if (*fmt == ')') | |
| 8512 --pcount; | |
| 8513 else if (*fmt == '(') | |
| 8514 ++pcount; | |
| 8515 fmt++; | |
| 8516 } | |
| 8517 keylen = fmt - keystart - 1; | |
| 8518 if (fmtcnt < 0 || pcount > 0) { | |
| 8519 PyErr_SetString(PyExc_ValueError, | |
| 8520 "incomplete format key"); | |
| 8521 goto onError; | |
| 8522 } | |
|
21028
3dfbbf42bf16
Fix for bug #438164: %-formatting using Unicode objects.
Marc-André Lemburg <mal@egenix.com>
parents:
20563
diff
changeset
|
8523 #if 0 |
| 51216 | 8524 /* keys are converted to strings using UTF-8 and |
| 8525 then looked up since Python uses strings to hold | |
| 8526 variables names etc. in its namespaces and we | |
| 8527 wouldn't want to break common idioms. */ | |
| 8528 key = PyUnicode_EncodeUTF8(keystart, | |
| 8529 keylen, | |
| 8530 NULL); | |
|
21028
3dfbbf42bf16
Fix for bug #438164: %-formatting using Unicode objects.
Marc-André Lemburg <mal@egenix.com>
parents:
20563
diff
changeset
|
8531 #else |
| 51216 | 8532 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
|
8533 #endif |
| 51216 | 8534 if (key == NULL) |
| 8535 goto onError; | |
| 8536 if (args_owned) { | |
| 8537 Py_DECREF(args); | |
| 8538 args_owned = 0; | |
| 8539 } | |
| 8540 args = PyObject_GetItem(dict, key); | |
| 8541 Py_DECREF(key); | |
| 8542 if (args == NULL) { | |
| 8543 goto onError; | |
| 8544 } | |
| 8545 args_owned = 1; | |
| 8546 arglen = -1; | |
| 8547 argidx = -2; | |
| 8548 } | |
| 8549 while (--fmtcnt >= 0) { | |
| 8550 switch (c = *fmt++) { | |
| 8551 case '-': flags |= F_LJUST; continue; | |
| 8552 case '+': flags |= F_SIGN; continue; | |
| 8553 case ' ': flags |= F_BLANK; continue; | |
| 8554 case '#': flags |= F_ALT; continue; | |
| 8555 case '0': flags |= F_ZERO; continue; | |
| 8556 } | |
| 8557 break; | |
| 8558 } | |
| 8559 if (c == '*') { | |
| 8560 v = getnextarg(args, arglen, &argidx); | |
| 8561 if (v == NULL) | |
| 8562 goto onError; | |
| 8563 if (!PyInt_Check(v)) { | |
| 8564 PyErr_SetString(PyExc_TypeError, | |
| 8565 "* wants int"); | |
| 8566 goto onError; | |
| 8567 } | |
| 8568 width = PyInt_AsLong(v); | |
| 8569 if (width < 0) { | |
| 8570 flags |= F_LJUST; | |
| 8571 width = -width; | |
| 8572 } | |
| 8573 if (--fmtcnt >= 0) | |
| 8574 c = *fmt++; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8575 } |
| 51216 | 8576 else if (c >= '0' && c <= '9') { |
| 8577 width = c - '0'; | |
| 8578 while (--fmtcnt >= 0) { | |
| 8579 c = *fmt++; | |
| 8580 if (c < '0' || c > '9') | |
| 8581 break; | |
| 8582 if ((width*10) / 10 != width) { | |
| 8583 PyErr_SetString(PyExc_ValueError, | |
| 8584 "width too big"); | |
| 8585 goto onError; | |
| 8586 } | |
| 8587 width = width*10 + (c - '0'); | |
| 8588 } | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8589 } |
| 51216 | 8590 if (c == '.') { |
| 8591 prec = 0; | |
| 8592 if (--fmtcnt >= 0) | |
| 8593 c = *fmt++; | |
| 8594 if (c == '*') { | |
| 8595 v = getnextarg(args, arglen, &argidx); | |
| 8596 if (v == NULL) | |
| 8597 goto onError; | |
| 8598 if (!PyInt_Check(v)) { | |
| 8599 PyErr_SetString(PyExc_TypeError, | |
| 8600 "* wants int"); | |
| 8601 goto onError; | |
| 8602 } | |
| 8603 prec = PyInt_AsLong(v); | |
| 8604 if (prec < 0) | |
| 8605 prec = 0; | |
| 8606 if (--fmtcnt >= 0) | |
| 8607 c = *fmt++; | |
| 8608 } | |
| 8609 else if (c >= '0' && c <= '9') { | |
| 8610 prec = c - '0'; | |
| 8611 while (--fmtcnt >= 0) { | |
| 8612 c = Py_CHARMASK(*fmt++); | |
| 8613 if (c < '0' || c > '9') | |
| 8614 break; | |
| 8615 if ((prec*10) / 10 != prec) { | |
| 8616 PyErr_SetString(PyExc_ValueError, | |
| 8617 "prec too big"); | |
| 8618 goto onError; | |
| 8619 } | |
| 8620 prec = prec*10 + (c - '0'); | |
| 8621 } | |
| 8622 } | |
| 8623 } /* prec */ | |
| 8624 if (fmtcnt >= 0) { | |
| 8625 if (c == 'h' || c == 'l' || c == 'L') { | |
| 8626 if (--fmtcnt >= 0) | |
| 8627 c = *fmt++; | |
| 8628 } | |
| 8629 } | |
| 8630 if (fmtcnt < 0) { | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8631 PyErr_SetString(PyExc_ValueError, |
| 51216 | 8632 "incomplete format"); |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8633 goto onError; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8634 } |
| 51216 | 8635 if (c != '%') { |
| 8636 v = getnextarg(args, arglen, &argidx); | |
| 8637 if (v == NULL) | |
| 8638 goto onError; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8639 } |
| 51216 | 8640 sign = 0; |
| 8641 fill = ' '; | |
| 8642 switch (c) { | |
| 8643 | |
| 8644 case '%': | |
| 8645 pbuf = formatbuf; | |
| 8646 /* presume that buffer length is at least 1 */ | |
| 8647 pbuf[0] = '%'; | |
| 8648 len = 1; | |
| 8649 break; | |
| 8650 | |
| 8651 case 's': | |
| 8652 case 'r': | |
| 8653 if (PyUnicode_Check(v) && c == 's') { | |
| 8654 temp = v; | |
| 8655 Py_INCREF(temp); | |
| 8656 } | |
| 8657 else { | |
| 8658 PyObject *unicode; | |
| 8659 if (c == 's') | |
| 8660 temp = PyObject_Unicode(v); | |
| 8661 else | |
| 8662 temp = PyObject_Repr(v); | |
| 8663 if (temp == NULL) | |
| 8664 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
|
8665 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
|
8666 /* 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
|
8667 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
|
8668 /* convert to string to Unicode */ |
| 51216 | 8669 unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
| 8670 PyString_GET_SIZE(temp), | |
| 8671 NULL, | |
| 8672 "strict"); | |
| 8673 Py_DECREF(temp); | |
| 8674 temp = unicode; | |
| 8675 if (temp == NULL) | |
| 8676 goto onError; | |
| 8677 } | |
| 8678 else { | |
| 8679 Py_DECREF(temp); | |
| 8680 PyErr_SetString(PyExc_TypeError, | |
| 8681 "%s argument has non-string str()"); | |
| 8682 goto onError; | |
| 8683 } | |
| 8684 } | |
| 8685 pbuf = PyUnicode_AS_UNICODE(temp); | |
| 8686 len = PyUnicode_GET_SIZE(temp); | |
| 8687 if (prec >= 0 && len > prec) | |
| 8688 len = prec; | |
| 8689 break; | |
| 8690 | |
| 8691 case 'i': | |
| 8692 case 'd': | |
| 8693 case 'u': | |
| 8694 case 'o': | |
| 8695 case 'x': | |
| 8696 case 'X': | |
| 8697 if (c == 'i') | |
| 8698 c = 'd'; | |
| 8699 isnumok = 0; | |
| 8700 if (PyNumber_Check(v)) { | |
| 8701 PyObject *iobj=NULL; | |
| 8702 | |
| 8703 if (PyInt_Check(v) || (PyLong_Check(v))) { | |
| 8704 iobj = v; | |
| 8705 Py_INCREF(iobj); | |
| 8706 } | |
| 8707 else { | |
| 8708 iobj = PyNumber_Int(v); | |
| 8709 if (iobj==NULL) iobj = PyNumber_Long(v); | |
| 8710 } | |
| 8711 if (iobj!=NULL) { | |
| 8712 if (PyInt_Check(iobj)) { | |
| 8713 isnumok = 1; | |
| 8714 pbuf = formatbuf; | |
| 8715 len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), | |
| 8716 flags, prec, c, iobj); | |
| 8717 Py_DECREF(iobj); | |
| 8718 if (len < 0) | |
| 8719 goto onError; | |
| 8720 sign = 1; | |
| 8721 } | |
| 8722 else if (PyLong_Check(iobj)) { | |
| 8723 isnumok = 1; | |
| 8724 temp = formatlong(iobj, flags, prec, c); | |
| 8725 Py_DECREF(iobj); | |
| 8726 if (!temp) | |
| 8727 goto onError; | |
| 8728 pbuf = PyUnicode_AS_UNICODE(temp); | |
| 8729 len = PyUnicode_GET_SIZE(temp); | |
| 8730 sign = 1; | |
| 8731 } | |
| 8732 else { | |
| 8733 Py_DECREF(iobj); | |
| 8734 } | |
| 8735 } | |
| 8736 } | |
| 8737 if (!isnumok) { | |
| 8738 PyErr_Format(PyExc_TypeError, | |
| 8739 "%%%c format: a number is required, " | |
| 8740 "not %.200s", (char)c, Py_TYPE(v)->tp_name); | |
| 8741 goto onError; | |
| 8742 } | |
| 8743 if (flags & F_ZERO) | |
| 8744 fill = '0'; | |
| 8745 break; | |
| 8746 | |
| 8747 case 'e': | |
| 8748 case 'E': | |
| 8749 case 'f': | |
| 8750 case 'F': | |
| 8751 case 'g': | |
| 8752 case 'G': | |
| 8753 if (c == 'F') | |
| 8754 c = 'f'; | |
| 8755 pbuf = formatbuf; | |
| 8756 len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), | |
| 8757 flags, prec, c, v); | |
| 8758 if (len < 0) | |
| 8759 goto onError; | |
| 8760 sign = 1; | |
| 8761 if (flags & F_ZERO) | |
| 8762 fill = '0'; | |
| 8763 break; | |
| 8764 | |
| 8765 case 'c': | |
| 8766 pbuf = formatbuf; | |
| 8767 len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); | |
| 8768 if (len < 0) | |
| 8769 goto onError; | |
| 8770 break; | |
| 8771 | |
| 8772 default: | |
| 8773 PyErr_Format(PyExc_ValueError, | |
| 8774 "unsupported format character '%c' (0x%x) " | |
| 8775 "at index %zd", | |
| 8776 (31<=c && c<=126) ? (char)c : '?', | |
| 8777 (int)c, | |
| 8778 (Py_ssize_t)(fmt - 1 - | |
| 8779 PyUnicode_AS_UNICODE(uformat))); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8780 goto onError; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8781 } |
| 51216 | 8782 if (sign) { |
| 8783 if (*pbuf == '-' || *pbuf == '+') { | |
| 8784 sign = *pbuf++; | |
| 8785 len--; | |
| 8786 } | |
| 8787 else if (flags & F_SIGN) | |
| 8788 sign = '+'; | |
| 8789 else if (flags & F_BLANK) | |
| 8790 sign = ' '; | |
| 8791 else | |
| 8792 sign = 0; | |
| 8793 } | |
| 8794 if (width < len) | |
| 8795 width = len; | |
| 8796 if (rescnt - (sign != 0) < width) { | |
| 8797 reslen -= rescnt; | |
| 8798 rescnt = width + fmtcnt + 100; | |
| 8799 reslen += rescnt; | |
| 8800 if (reslen < 0) { | |
| 8801 Py_XDECREF(temp); | |
| 8802 PyErr_NoMemory(); | |
| 8803 goto onError; | |
| 8804 } | |
| 8805 if (_PyUnicode_Resize(&result, reslen) < 0) { | |
| 8806 Py_XDECREF(temp); | |
| 8807 goto onError; | |
| 8808 } | |
| 8809 res = PyUnicode_AS_UNICODE(result) | |
| 8810 + reslen - rescnt; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8811 } |
| 51216 | 8812 if (sign) { |
| 8813 if (fill != ' ') | |
| 8814 *res++ = sign; | |
| 8815 rescnt--; | |
| 8816 if (width > len) | |
| 8817 width--; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8818 } |
| 51216 | 8819 if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 8820 assert(pbuf[0] == '0'); | |
| 8821 assert(pbuf[1] == c); | |
| 8822 if (fill != ' ') { | |
| 8823 *res++ = *pbuf++; | |
| 8824 *res++ = *pbuf++; | |
| 8825 } | |
| 8826 rescnt -= 2; | |
| 8827 width -= 2; | |
| 8828 if (width < 0) | |
| 8829 width = 0; | |
| 8830 len -= 2; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8831 } |
| 51216 | 8832 if (width > len && !(flags & F_LJUST)) { |
| 8833 do { | |
| 8834 --rescnt; | |
| 8835 *res++ = fill; | |
| 8836 } while (--width > len); | |
| 8837 } | |
| 8838 if (fill == ' ') { | |
| 8839 if (sign) | |
| 8840 *res++ = sign; | |
| 8841 if ((flags & F_ALT) && (c == 'x' || c == 'X')) { | |
| 8842 assert(pbuf[0] == '0'); | |
| 8843 assert(pbuf[1] == c); | |
| 8844 *res++ = *pbuf++; | |
| 8845 *res++ = *pbuf++; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8846 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8847 } |
| 51216 | 8848 Py_UNICODE_COPY(res, pbuf, len); |
| 8849 res += len; | |
| 8850 rescnt -= len; | |
| 8851 while (--width >= len) { | |
| 8852 --rescnt; | |
| 8853 *res++ = ' '; | |
| 8854 } | |
| 8855 if (dict && (argidx < arglen) && c != '%') { | |
| 8856 PyErr_SetString(PyExc_TypeError, | |
| 8857 "not all arguments converted during string formatting"); | |
| 8858 Py_XDECREF(temp); | |
| 8859 goto onError; | |
| 8860 } | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8861 Py_XDECREF(temp); |
| 51216 | 8862 } /* '%' */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8863 } /* until end */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8864 if (argidx < arglen && !dict) { |
| 51216 | 8865 PyErr_SetString(PyExc_TypeError, |
| 8866 "not all arguments converted during string formatting"); | |
| 8867 goto onError; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8868 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8869 |
|
36945
0f19a6fc9756
- Reindent a confusingly indented piece of code (no intended code changes
Thomas Wouters <thomas@python.org>
parents:
36924
diff
changeset
|
8870 if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 51216 | 8871 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8872 if (args_owned) { |
| 51216 | 8873 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
|
8874 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8875 Py_DECREF(uformat); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8876 return (PyObject *)result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8877 |
| 51216 | 8878 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8879 Py_XDECREF(result); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8880 Py_DECREF(uformat); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8881 if (args_owned) { |
| 51216 | 8882 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
|
8883 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8884 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8885 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8886 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8887 static PyBufferProcs unicode_as_buffer = { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8888 (readbufferproc) unicode_buffer_getreadbuf, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8889 (writebufferproc) unicode_buffer_getwritebuf, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8890 (segcountproc) unicode_buffer_getsegcount, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8891 (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
|
8892 }; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8893 |
|
24280
1598a1e473e2
staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24071
diff
changeset
|
8894 static PyObject * |
|
19593
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8895 unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
|
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8896 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8897 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8898 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
|
8899 { |
| 51216 | 8900 PyObject *x = NULL; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8901 static char *kwlist[] = {"string", "encoding", "errors", 0}; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8902 char *encoding = NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8903 char *errors = NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8904 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8905 if (type != &PyUnicode_Type) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8906 return unicode_subtype_new(type, args, kwds); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8907 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 51216 | 8908 kwlist, &x, &encoding, &errors)) |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8909 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8910 if (x == NULL) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8911 return (PyObject *)_PyUnicode_New(0); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8912 if (encoding == NULL && errors == NULL) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8913 return PyObject_Unicode(x); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8914 else |
| 51216 | 8915 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
|
8916 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8917 |
|
19593
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8918 static PyObject * |
|
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8919 unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8920 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8921 PyUnicodeObject *tmp, *pnew; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8922 Py_ssize_t n; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8923 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8924 assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8925 tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8926 if (tmp == NULL) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8927 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8928 assert(PyUnicode_Check(tmp)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8929 pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8930 if (pnew == NULL) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8931 Py_DECREF(tmp); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8932 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8933 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8934 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
|
8935 if (pnew->str == NULL) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8936 _Py_ForgetReference((PyObject *)pnew); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8937 PyObject_Del(pnew); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8938 Py_DECREF(tmp); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8939 return PyErr_NoMemory(); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8940 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8941 Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8942 pnew->length = n; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8943 pnew->hash = tmp->hash; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8944 Py_DECREF(tmp); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8945 return (PyObject *)pnew; |
|
19593
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8946 } |
|
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8947 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
8948 PyDoc_STRVAR(unicode_doc, |
| 51216 | 8949 "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
|
8950 \n\ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8951 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
|
8952 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
|
8953 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
|
8954 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8955 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
|
8956 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8957 "unicode", /* tp_name */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8958 sizeof(PyUnicodeObject), /* tp_size */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8959 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
|
8960 /* Slots */ |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8961 (destructor)unicode_dealloc, /* tp_dealloc */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8962 0, /* tp_print */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8963 0, /* tp_getattr */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8964 0, /* tp_setattr */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8965 0, /* tp_compare */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8966 unicode_repr, /* tp_repr */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8967 &unicode_as_number, /* tp_as_number */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8968 &unicode_as_sequence, /* tp_as_sequence */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8969 &unicode_as_mapping, /* tp_as_mapping */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8970 (hashfunc) unicode_hash, /* tp_hash*/ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8971 0, /* tp_call*/ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8972 (reprfunc) unicode_str, /* tp_str */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8973 PyObject_GenericGetAttr, /* tp_getattro */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8974 0, /* tp_setattro */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8975 &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
|
8976 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
| 51216 | 8977 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
|
8978 unicode_doc, /* tp_doc */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8979 0, /* tp_traverse */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8980 0, /* tp_clear */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8981 PyUnicode_RichCompare, /* tp_richcompare */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8982 0, /* tp_weaklistoffset */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8983 0, /* tp_iter */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8984 0, /* tp_iternext */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8985 unicode_methods, /* tp_methods */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8986 0, /* tp_members */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8987 0, /* tp_getset */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8988 &PyBaseString_Type, /* tp_base */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8989 0, /* tp_dict */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8990 0, /* tp_descr_get */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8991 0, /* tp_descr_set */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8992 0, /* tp_dictoffset */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8993 0, /* tp_init */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8994 0, /* tp_alloc */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8995 unicode_new, /* tp_new */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8996 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
|
8997 }; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8998 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8999 /* 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
|
9000 |
|
13660
a1ce07218781
Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents:
13635
diff
changeset
|
9001 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
|
9002 { |
|
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
|
9003 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
|
9004 |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
9005 /* 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
|
9006 Py_UNICODE linebreak[] = { |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
9007 0x000A, /* LINE FEED */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
9008 0x000D, /* CARRIAGE RETURN */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
9009 0x001C, /* FILE SEPARATOR */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
9010 0x001D, /* GROUP SEPARATOR */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
9011 0x001E, /* RECORD SEPARATOR */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
9012 0x0085, /* NEXT LINE */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
9013 0x2028, /* LINE SEPARATOR */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
9014 0x2029, /* PARAGRAPH SEPARATOR */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
9015 }; |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
9016 |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
9017 /* 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
|
9018 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
|
9019 numfree = 0; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9020 unicode_empty = _PyUnicode_New(0); |
|
39129
f2031f15d99d
Handle allocation failures gracefully. Found with failmalloc.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38817
diff
changeset
|
9021 if (!unicode_empty) |
| 51216 | 9022 return; |
|
39129
f2031f15d99d
Handle allocation failures gracefully. Found with failmalloc.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38817
diff
changeset
|
9023 |
|
12876
243593f11fcf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12646
diff
changeset
|
9024 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
|
9025 for (i = 0; i < 256; i++) |
| 51216 | 9026 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
|
9027 if (PyType_Ready(&PyUnicode_Type) < 0) |
| 51216 | 9028 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
|
9029 |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
9030 /* 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
|
9031 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
|
9032 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
|
9033 ); |
|
38786
9c52efcaff65
Initialize the type object so pychecker can't crash the interpreter.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38709
diff
changeset
|
9034 |
|
9c52efcaff65
Initialize the type object so pychecker can't crash the interpreter.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38709
diff
changeset
|
9035 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
|
9036 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9037 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9038 /* 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
|
9039 |
|
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
|
9040 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
|
9041 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
|
9042 { |
|
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
|
9043 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
|
9044 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
|
9045 |
|
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
|
9046 for (u = free_list; u != NULL;) { |
| 51216 | 9047 PyUnicodeObject *v = u; |
| 9048 u = *(PyUnicodeObject **)u; | |
| 9049 if (v->str) | |
| 9050 PyObject_DEL(v->str); | |
| 9051 Py_XDECREF(v->defenc); | |
| 9052 PyObject_Del(v); | |
| 9053 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
|
9054 } |
|
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
|
9055 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
|
9056 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
|
9057 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
|
9058 } |
|
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
|
9059 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9060 void |
|
13660
a1ce07218781
Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents:
13635
diff
changeset
|
9061 _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
|
9062 { |
|
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
|
9063 int i; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9064 |
|
14998
c16bdf555c79
In _PyUnicode_Fini(), decref unicode_empty before tearng down the free
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
9065 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
|
9066 unicode_empty = NULL; |
|
15001
a349b89bf10e
_PyUnicode_Fini(): Initialize the local freelist walking variable `u'
Barry Warsaw <barry@python.org>
parents:
14998
diff
changeset
|
9067 |
|
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
|
9068 for (i = 0; i < 256; i++) { |
| 51216 | 9069 if (unicode_latin1[i]) { |
| 9070 Py_DECREF(unicode_latin1[i]); | |
| 9071 unicode_latin1[i] = NULL; | |
| 9072 } | |
|
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
|
9073 } |
|
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
|
9074 (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
|
9075 } |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
9076 |
|
37655
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
9077 #ifdef __cplusplus |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
9078 } |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
9079 #endif |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
9080 |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
9081 |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
9082 /* |
| 51216 | 9083 Local variables: |
| 9084 c-basic-offset: 4 | |
| 9085 indent-tabs-mode: nil | |
| 9086 End: | |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
9087 */ |
