Mercurial > cpython-lru_cache-weakref
annotate Objects/unicodeobject.c @ 71583:cdff35544530 2.7
Minor doc addition for clarity
| author | Éric Araujo <merwok@netwok.org> |
|---|---|
| date | Fri, 27 May 2011 04:42:47 +0200 |
| parents | e266415cf42c |
| children | eab17979a586 |
| 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, |
|
60074
66e757d8ea93
#7643: Unicode codepoints VT (0x0B) and FF (0x0C) are linebreaks according to Unicode Standard Annex #14.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59998
diff
changeset
|
118 /* case 0x0009: * CHARACTER TABULATION */ |
|
49386
642f20b22577
Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
49115
diff
changeset
|
119 /* case 0x000A: * LINE FEED */ |
|
60074
66e757d8ea93
#7643: Unicode codepoints VT (0x0B) and FF (0x0C) are linebreaks according to Unicode Standard Annex #14.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59998
diff
changeset
|
120 /* case 0x000B: * LINE TABULATION */ |
|
49386
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 */ |
|
60074
66e757d8ea93
#7643: Unicode codepoints VT (0x0B) and FF (0x0C) are linebreaks according to Unicode Standard Annex #14.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59998
diff
changeset
|
150 /* 0x000B, * LINE TABULATION */ |
|
66e757d8ea93
#7643: Unicode codepoints VT (0x0B) and FF (0x0C) are linebreaks according to Unicode Standard Annex #14.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59998
diff
changeset
|
151 /* 0x000C, * FORM FEED */ |
|
49386
642f20b22577
Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
49115
diff
changeset
|
152 /* 0x000D, * CARRIAGE RETURN */ |
|
60074
66e757d8ea93
#7643: Unicode codepoints VT (0x0B) and FF (0x0C) are linebreaks according to Unicode Standard Annex #14.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59998
diff
changeset
|
153 0, 0, 1, 1, 1, 1, 0, 0, |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
154 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
|
155 /* 0x001C, * FILE SEPARATOR */ |
|
642f20b22577
Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
49115
diff
changeset
|
156 /* 0x001D, * GROUP SEPARATOR */ |
|
642f20b22577
Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
49115
diff
changeset
|
157 /* 0x001E, * RECORD SEPARATOR */ |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
158 0, 0, 0, 0, 1, 1, 1, 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 0, 0, 0, 0, 0, 0, 0, 0, |
|
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 |
|
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, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
170 0, 0, 0, 0, 0, 0, 0, 0, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
171 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
|
172 }; |
|
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
|
173 |
|
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
|
174 |
|
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 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
|
176 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
|
177 { |
|
18324
3ba7a358ce02
use Py_UNICODE_WIDE instead of USE_UCS4_STORAGE and Py_UNICODE_SIZE
Fredrik Lundh <fredrik@pythonware.com>
parents:
18298
diff
changeset
|
178 #ifdef Py_UNICODE_WIDE |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
179 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
|
180 #else |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
181 /* This is actually an illegal character, so it should |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
182 not be passed to unichr. */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
183 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
|
184 #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
|
185 } |
|
982163c4e9d2
Encode surrogates in UTF-8 even for a wide Py_UNICODE.
Martin v. Löwis <martin@v.loewis.de>
parents:
18295
diff
changeset
|
186 |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
187 /* --- Bloom Filters ----------------------------------------------------- */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
188 |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
189 /* 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
|
190 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
|
191 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
|
192 |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
193 /* 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
|
194 |
|
58297
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
195 #if LONG_BIT >= 128 |
|
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
196 #define BLOOM_WIDTH 128 |
|
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
197 #elif LONG_BIT >= 64 |
|
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
198 #define BLOOM_WIDTH 64 |
|
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
199 #elif LONG_BIT >= 32 |
|
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
200 #define BLOOM_WIDTH 32 |
|
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
201 #else |
|
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
202 #error "LONG_BIT is smaller than 32" |
|
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
203 #endif |
|
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
204 |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
205 #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
|
206 |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
207 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
|
208 |
|
58297
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
209 #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
|
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
210 #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
211 |
| 51216 | 212 #define BLOOM_LINEBREAK(ch) \ |
| 213 ((ch) < 128U ? ascii_linebreak[(ch)] : \ | |
| 214 (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
|
215 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
216 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
|
217 { |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
218 /* 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
|
219 |
|
58297
9225ea677c4b
Sanitize bloom filter macros
Antoine Pitrou <solipsis@pitrou.net>
parents:
58290
diff
changeset
|
220 BLOOM_MASK mask; |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
221 Py_ssize_t i; |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
222 |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
223 mask = 0; |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
224 for (i = 0; i < len; i++) |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
225 BLOOM_ADD(mask, ptr[i]); |
|
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 return mask; |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
228 } |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
229 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
230 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
|
231 { |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
232 Py_ssize_t i; |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
233 |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
234 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
|
235 if (set[i] == chr) |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
236 return 1; |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
237 |
|
38270
1dc5657f1745
return 0 on misses, not -1.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38257
diff
changeset
|
238 return 0; |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
239 } |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
240 |
| 51216 | 241 #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
|
242 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
|
243 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
244 /* --- Unicode Object ----------------------------------------------------- */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 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
|
247 int unicode_resize(register PyUnicodeObject *unicode, |
| 51216 | 248 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
|
249 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
250 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
|
251 |
| 12329 | 252 /* 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
|
253 if (unicode->length == length) |
| 51216 | 254 goto reset; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
255 |
|
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
|
256 /* 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
|
257 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
|
258 instead ! */ |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
259 |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
260 if (unicode == unicode_empty || |
| 51216 | 261 (unicode->length == 1 && |
| 262 unicode->str[0] < 256U && | |
| 263 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
|
264 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
|
265 "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
|
266 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
267 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
268 |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
269 /* 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
|
270 The overallocation is also used by fastsearch, which assumes that it's |
| 38405 | 271 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
|
272 it contains). */ |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
273 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
274 oldstr = unicode->str; |
| 45826 | 275 unicode->str = PyObject_REALLOC(unicode->str, |
| 51216 | 276 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
|
277 if (!unicode->str) { |
| 51216 | 278 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
|
279 PyErr_NoMemory(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
280 return -1; |
|
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 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
|
283 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
|
284 |
| 51216 | 285 reset: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
286 /* 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
|
287 if (unicode->defenc) { |
|
63503
e8fc9a9e1991
Merged revisions 83452,83457,83466,83471,83475,83480,83486 via svnmerge from
Georg Brandl <georg@python.org>
parents:
63106
diff
changeset
|
288 Py_CLEAR(unicode->defenc); |
|
11968
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 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
|
291 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
292 return 0; |
|
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 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
295 /* We allocate one more byte to make sure the string is |
|
63462
75b3edda3c2b
Recorded merge of revisions 83444 via svnmerge from
Georg Brandl <georg@python.org>
parents:
63434
diff
changeset
|
296 Ux0000 terminated; some code relies on that. |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
297 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
298 XXX This allocator could further be enhanced by assuring that the |
| 51216 | 299 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
|
300 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
301 */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
303 static |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
304 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
|
305 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
306 register PyUnicodeObject *unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
307 |
| 38426 | 308 /* 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
|
309 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
|
310 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
|
311 return unicode_empty; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
312 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
313 |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
314 /* Ensure we won't overflow the size. */ |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
315 if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
316 return (PyUnicodeObject *)PyErr_NoMemory(); |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
317 } |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
318 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
319 /* 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
|
320 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
|
321 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
|
322 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
|
323 numfree--; |
| 51216 | 324 if (unicode->str) { |
| 325 /* Keep-Alive optimization: we only upsize the buffer, | |
| 326 never downsize it. */ | |
| 327 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
|
328 unicode_resize(unicode, length) < 0) { |
| 51216 | 329 PyObject_DEL(unicode->str); |
| 330 unicode->str = NULL; | |
| 331 } | |
| 332 } | |
|
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
|
333 else { |
| 51216 | 334 size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 335 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
|
336 } |
|
c29f3135a20c
Fix a mis-indentation in _PyUnicode_New() that caused me to stare at
Guido van Rossum <guido@python.org>
parents:
18051
diff
changeset
|
337 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
|
338 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
339 else { |
| 51216 | 340 size_t new_size; |
|
22981
dc1fb19d990d
Remove PyMalloc_*.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22938
diff
changeset
|
341 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
|
342 if (unicode == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
343 return NULL; |
| 51216 | 344 new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 345 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
|
346 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
347 |
| 12511 | 348 if (!unicode->str) { |
| 51216 | 349 PyErr_NoMemory(); |
| 350 goto onError; | |
| 12511 | 351 } |
|
30439
30e495efe90a
Double-fix of crash in Unicode freelist handling.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
30431
diff
changeset
|
352 /* 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
|
353 * 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
|
354 * 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
|
355 * 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
|
356 * 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
|
357 * 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
|
358 */ |
|
30439
30e495efe90a
Double-fix of crash in Unicode freelist handling.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
30431
diff
changeset
|
359 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
|
360 unicode->str[length] = 0; |
|
37668
4342e441b11c
Remove another INT_MAX limitation
Martin v. Löwis <martin@v.loewis.de>
parents:
37662
diff
changeset
|
361 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
|
362 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
|
363 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
|
364 return unicode; |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11997
diff
changeset
|
365 |
| 51216 | 366 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
|
367 /* 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
|
368 _Py_DEC_REFTOTAL; |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11997
diff
changeset
|
369 _Py_ForgetReference((PyObject *)unicode); |
|
22981
dc1fb19d990d
Remove PyMalloc_*.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22938
diff
changeset
|
370 PyObject_Del(unicode); |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11997
diff
changeset
|
371 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
372 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
373 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
374 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
|
375 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
|
376 { |
|
21262
e8fea95a82a4
Fix for #489669 (Neil Norwitz): memory leak in test_descr (unicode).
Guido van Rossum <guido@python.org>
parents:
21082
diff
changeset
|
377 if (PyUnicode_CheckExact(unicode) && |
| 51216 | 378 numfree < PyUnicode_MAXFREELIST) { |
| 12329 | 379 /* Keep-Alive optimization */ |
| 51216 | 380 if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
| 381 PyObject_DEL(unicode->str); | |
| 382 unicode->str = NULL; | |
| 383 unicode->length = 0; | |
| 384 } | |
| 385 if (unicode->defenc) { | |
|
63503
e8fc9a9e1991
Merged revisions 83452,83457,83466,83471,83475,83480,83486 via svnmerge from
Georg Brandl <georg@python.org>
parents:
63106
diff
changeset
|
386 Py_CLEAR(unicode->defenc); |
| 51216 | 387 } |
| 388 /* 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
|
389 *(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
|
390 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
|
391 numfree++; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
392 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
393 else { |
| 51216 | 394 PyObject_DEL(unicode->str); |
| 395 Py_XDECREF(unicode->defenc); | |
| 396 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
|
397 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
398 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
399 |
|
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
|
400 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
|
401 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
|
402 { |
|
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 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
|
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 /* 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
|
406 if (unicode == NULL) { |
| 51216 | 407 PyErr_BadInternalCall(); |
| 408 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
|
409 } |
|
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
|
410 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
|
411 if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) { |
| 51216 | 412 PyErr_BadInternalCall(); |
| 413 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
|
414 } |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
415 |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
416 /* 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
|
417 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
|
418 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
|
419 if (v->length != length && |
| 51216 | 420 (v == unicode_empty || v->length == 1)) { |
| 421 PyUnicodeObject *w = _PyUnicode_New(length); | |
| 422 if (w == NULL) | |
| 423 return -1; | |
| 424 Py_UNICODE_COPY(w->str, v->str, | |
| 425 length < v->length ? length : v->length); | |
| 426 Py_DECREF(*unicode); | |
| 427 *unicode = w; | |
| 428 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
|
429 } |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
430 |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
431 /* 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
|
432 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
|
433 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
|
434 } |
|
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 |
|
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
|
436 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
|
437 { |
|
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
|
438 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
|
439 } |
|
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
|
440 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
441 PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
| 51216 | 442 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
|
443 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
444 PyUnicodeObject *unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
445 |
|
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
|
446 /* 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
|
447 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
|
448 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
|
449 |
| 51216 | 450 /* Optimization for empty strings */ |
| 451 if (size == 0 && unicode_empty != NULL) { | |
| 452 Py_INCREF(unicode_empty); | |
| 453 return (PyObject *)unicode_empty; | |
| 454 } | |
| 455 | |
| 456 /* Single character Unicode objects in the Latin-1 range are | |
| 457 shared when using this constructor */ | |
| 458 if (size == 1 && *u < 256) { | |
| 459 unicode = unicode_latin1[*u]; | |
| 460 if (!unicode) { | |
| 461 unicode = _PyUnicode_New(1); | |
| 462 if (!unicode) | |
| 463 return NULL; | |
| 464 unicode->str[0] = *u; | |
| 465 unicode_latin1[*u] = unicode; | |
| 466 } | |
| 467 Py_INCREF(unicode); | |
| 468 return (PyObject *)unicode; | |
| 469 } | |
|
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
|
470 } |
|
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
|
471 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
472 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
|
473 if (!unicode) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
474 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
475 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
476 /* 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
|
477 if (u != NULL) |
| 51216 | 478 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
|
479 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
480 return (PyObject *)unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
481 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
482 |
|
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
|
483 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
|
484 { |
|
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 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
|
486 |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
487 if (size < 0) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
488 PyErr_SetString(PyExc_SystemError, |
| 51216 | 489 "Negative size passed to PyUnicode_FromStringAndSize"); |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
490 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
491 } |
|
46383
0b55875c1c9c
Raise SystemError when size < 0 is passed into PyString_FromStringAndSize,
Gregory P. Smith <greg@mad-scientist.com>
parents:
46333
diff
changeset
|
492 |
|
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
|
493 /* 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
|
494 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
|
495 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
|
496 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
|
497 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
|
498 |
| 51216 | 499 /* Optimization for empty strings */ |
| 500 if (size == 0 && unicode_empty != NULL) { | |
| 501 Py_INCREF(unicode_empty); | |
| 502 return (PyObject *)unicode_empty; | |
| 503 } | |
| 504 | |
| 505 /* 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
|
506 Restrict to ASCII, since the input must be UTF-8. */ |
| 51216 | 507 if (size == 1 && Py_CHARMASK(*u) < 128) { |
| 508 unicode = unicode_latin1[Py_CHARMASK(*u)]; | |
| 509 if (!unicode) { | |
| 510 unicode = _PyUnicode_New(1); | |
| 511 if (!unicode) | |
| 512 return NULL; | |
| 513 unicode->str[0] = Py_CHARMASK(*u); | |
| 514 unicode_latin1[Py_CHARMASK(*u)] = unicode; | |
| 515 } | |
| 516 Py_INCREF(unicode); | |
| 517 return (PyObject *)unicode; | |
| 518 } | |
|
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
|
519 |
|
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 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
|
521 } |
|
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 |
|
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 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
|
524 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
|
525 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
|
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 (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
|
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 |
|
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
|
530 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
|
531 { |
|
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
|
532 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
|
533 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
|
534 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
|
535 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
|
536 } |
|
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
|
537 |
|
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
|
538 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
|
539 } |
|
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
|
540 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
541 #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
|
542 |
|
52210
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
543 #if (Py_UNICODE_SIZE == 2) && defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
544 # define CONVERT_WCHAR_TO_SURROGATES |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
545 #endif |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
546 |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
547 #ifdef CONVERT_WCHAR_TO_SURROGATES |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
548 |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
549 /* Here sizeof(wchar_t) is 4 but Py_UNICODE_SIZE == 2, so we need |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
550 to convert from UTF32 to UTF16. */ |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
551 |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
552 PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
553 Py_ssize_t size) |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
554 { |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
555 PyUnicodeObject *unicode; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
556 register Py_ssize_t i; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
557 Py_ssize_t alloc; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
558 const wchar_t *orig_w; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
559 |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
560 if (w == NULL) { |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
561 PyErr_BadInternalCall(); |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
562 return NULL; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
563 } |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
564 |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
565 alloc = size; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
566 orig_w = w; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
567 for (i = size; i > 0; i--) { |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
568 if (*w > 0xFFFF) |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
569 alloc++; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
570 w++; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
571 } |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
572 w = orig_w; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
573 unicode = _PyUnicode_New(alloc); |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
574 if (!unicode) |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
575 return NULL; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
576 |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
577 /* Copy the wchar_t data into the new object */ |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
578 { |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
579 register Py_UNICODE *u; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
580 u = PyUnicode_AS_UNICODE(unicode); |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
581 for (i = size; i > 0; i--) { |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
582 if (*w > 0xFFFF) { |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
583 wchar_t ordinal = *w++; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
584 ordinal -= 0x10000; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
585 *u++ = 0xD800 | (ordinal >> 10); |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
586 *u++ = 0xDC00 | (ordinal & 0x3FF); |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
587 } |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
588 else |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
589 *u++ = *w++; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
590 } |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
591 } |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
592 return (PyObject *)unicode; |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
593 } |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
594 |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
595 #else |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
596 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
597 PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
| 51216 | 598 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
|
599 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
600 PyUnicodeObject *unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
601 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
602 if (w == NULL) { |
| 51216 | 603 PyErr_BadInternalCall(); |
| 604 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
605 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
606 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
607 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
|
608 if (!unicode) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
609 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
610 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
611 /* 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
|
612 #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
|
613 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
|
614 #else |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
615 { |
| 51216 | 616 register Py_UNICODE *u; |
| 617 register Py_ssize_t i; | |
| 618 u = PyUnicode_AS_UNICODE(unicode); | |
| 619 for (i = size; i > 0; i--) | |
| 620 *u++ = *w++; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
621 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
622 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
623 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
624 return (PyObject *)unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
625 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
626 |
|
52210
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
627 #endif /* CONVERT_WCHAR_TO_SURROGATES */ |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
628 |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
629 #undef CONVERT_WCHAR_TO_SURROGATES |
|
c73dd25c01c1
Issue 4474: On platforms with sizeof(wchar_t) == 4 and
Mark Dickinson <dickinsm@gmail.com>
parents:
51775
diff
changeset
|
630 |
|
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
|
631 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
|
632 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
|
633 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
634 *fmt++ = '%'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
635 if (width) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
636 if (zeropad) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
637 *fmt++ = '0'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
638 fmt += sprintf(fmt, "%d", width); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
639 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
640 if (precision) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
641 fmt += sprintf(fmt, ".%d", precision); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
642 if (longflag) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
643 *fmt++ = 'l'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
644 else if (size_tflag) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
645 char *f = PY_FORMAT_SIZE_T; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
646 while (*f) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
647 *fmt++ = *f++; |
|
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 *fmt++ = c; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
650 *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
|
651 } |
|
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 |
|
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
|
653 #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
|
654 |
|
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
|
655 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
|
656 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
|
657 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
658 va_list count; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
659 Py_ssize_t callcount = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
660 PyObject **callresults = NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
661 PyObject **callresult = NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
662 Py_ssize_t n = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
663 int width = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
664 int precision = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
665 int zeropad; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
666 const char* f; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
667 Py_UNICODE *s; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
668 PyObject *string; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
669 /* used by sprintf */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
670 char buffer[21]; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
671 /* 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
|
672 * (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
|
673 char *abuffer = NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
674 char *realbuffer; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
675 Py_ssize_t abuffersize = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
676 char fmt[60]; /* should be enough for %0width.precisionld */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
677 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
|
678 |
|
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
|
679 #ifdef VA_LIST_IS_ARRAY |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
680 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
|
681 #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
|
682 #ifdef __va_copy |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
683 __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
|
684 #else |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
685 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
|
686 #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
|
687 #endif |
|
53721
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
688 /* step 1: count the number of %S/%R/%s format specifications |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
689 * (we call PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() for these |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
690 * objects once during step 3 and put the result in an array) */ |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
691 for (f = format; *f; f++) { |
|
53721
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
692 if (*f == '%') { |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
693 if (*(f+1)=='%') |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
694 continue; |
|
53723
5ade0de74b5a
Merged revisions 72263 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
53721
diff
changeset
|
695 if (*(f+1)=='S' || *(f+1)=='R') |
|
53721
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
696 ++callcount; |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
697 while (isdigit((unsigned)*f)) |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
698 width = (width*10) + *f++ - '0'; |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
699 while (*++f && *f != '%' && !isalpha((unsigned)*f)) |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
700 ; |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
701 if (*f == 's') |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
702 ++callcount; |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
703 } |
|
51204
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 /* step 2: allocate memory for the results of |
|
53721
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
706 * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
707 if (callcount) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
708 callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
709 if (!callresults) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
710 PyErr_NoMemory(); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
711 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
712 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
713 callresult = callresults; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
714 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
715 /* 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
|
716 for (f = format; *f; f++) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
717 if (*f == '%') { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
718 const char* p = f; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
719 width = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
720 while (isdigit((unsigned)*f)) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
721 width = (width*10) + *f++ - '0'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
722 while (*++f && *f != '%' && !isalpha((unsigned)*f)) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
723 ; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
724 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
725 /* 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
|
726 * 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
|
727 */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
728 if ((*f == 'l' || *f == 'z') && |
| 51216 | 729 (f[1] == 'd' || f[1] == 'u')) |
| 730 ++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
|
731 |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
732 switch (*f) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
733 case 'c': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
734 (void)va_arg(count, int); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
735 /* fall through... */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
736 case '%': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
737 n++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
738 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
739 case 'd': case 'u': case 'i': case 'x': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
740 (void) va_arg(count, int); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
741 /* 20 bytes is enough to hold a 64-bit |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
742 integer. Decimal takes the most space. |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
743 This isn't enough for octal. |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
744 If a width is specified we need more |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
745 (which we allocate later). */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
746 if (width < 20) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
747 width = 20; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
748 n += width; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
749 if (abuffersize < width) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
750 abuffersize = width; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
751 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
752 case 's': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
753 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
754 /* UTF-8 */ |
|
53780
5878ca784a84
#5929: fix signedness warning.
Georg Brandl <georg@python.org>
parents:
53740
diff
changeset
|
755 const char *s = va_arg(count, const char*); |
|
53721
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
756 PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
757 if (!str) |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
758 goto fail; |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
759 n += PyUnicode_GET_SIZE(str); |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
760 /* Remember the str and switch to the next slot */ |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
761 *callresult++ = str; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
762 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
763 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
764 case 'U': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
765 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
766 PyObject *obj = va_arg(count, PyObject *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
767 assert(obj && PyUnicode_Check(obj)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
768 n += PyUnicode_GET_SIZE(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
769 break; |
|
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 case 'V': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
772 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
773 PyObject *obj = va_arg(count, PyObject *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
774 const char *str = va_arg(count, const char *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
775 assert(obj || str); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
776 assert(!obj || PyUnicode_Check(obj)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
777 if (obj) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
778 n += PyUnicode_GET_SIZE(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
779 else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
780 n += strlen(str); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
781 break; |
|
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 case 'S': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
784 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
785 PyObject *obj = va_arg(count, PyObject *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
786 PyObject *str; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
787 assert(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
788 str = PyObject_Str(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
789 if (!str) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
790 goto fail; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
791 n += PyUnicode_GET_SIZE(str); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
792 /* Remember the str and switch to the next slot */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
793 *callresult++ = str; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
794 break; |
|
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 case 'R': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
797 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
798 PyObject *obj = va_arg(count, PyObject *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
799 PyObject *repr; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
800 assert(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
801 repr = PyObject_Repr(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
802 if (!repr) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
803 goto fail; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
804 n += PyUnicode_GET_SIZE(repr); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
805 /* Remember the repr and switch to the next slot */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
806 *callresult++ = repr; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
807 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
808 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
809 case 'p': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
810 (void) va_arg(count, int); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
811 /* maximum 64-bit pointer representation: |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
812 * 0xffffffffffffffff |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
813 * so 19 characters is enough. |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
814 * XXX I count 18 -- what's the extra for? |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
815 */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
816 n += 19; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
817 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
818 default: |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
819 /* if we stumble upon an unknown |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
820 formatting code, copy the rest of |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
821 the format string to the output |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
822 string. (we cannot just skip the |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
823 code, since there's no way to know |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
824 what's in the argument list) */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
825 n += strlen(p); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
826 goto expand; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
827 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
828 } else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
829 n++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
830 } |
| 51216 | 831 expand: |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
832 if (abuffersize > 20) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
833 abuffer = PyObject_Malloc(abuffersize); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
834 if (!abuffer) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
835 PyErr_NoMemory(); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
836 goto fail; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
837 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
838 realbuffer = abuffer; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
839 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
840 else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
841 realbuffer = buffer; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
842 /* step 4: fill the buffer */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
843 /* 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
|
844 we don't have to resize the string. |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
845 There can be no errors beyond this point. */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
846 string = PyUnicode_FromUnicode(NULL, n); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
847 if (!string) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
848 goto fail; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
849 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
850 s = PyUnicode_AS_UNICODE(string); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
851 callresult = callresults; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
852 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
853 for (f = format; *f; f++) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
854 if (*f == '%') { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
855 const char* p = f++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
856 int longflag = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
857 int size_tflag = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
858 zeropad = (*f == '0'); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
859 /* parse the width.precision part */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
860 width = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
861 while (isdigit((unsigned)*f)) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
862 width = (width*10) + *f++ - '0'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
863 precision = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
864 if (*f == '.') { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
865 f++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
866 while (isdigit((unsigned)*f)) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
867 precision = (precision*10) + *f++ - '0'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
868 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
869 /* 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
|
870 others can be added when necessary. */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
871 if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
872 longflag = 1; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
873 ++f; |
|
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 /* handle the size_t flag. */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
876 if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
877 size_tflag = 1; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
878 ++f; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
879 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
880 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
881 switch (*f) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
882 case 'c': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
883 *s++ = va_arg(vargs, int); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
884 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
885 case 'd': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
886 makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd'); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
887 if (longflag) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
888 sprintf(realbuffer, fmt, va_arg(vargs, long)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
889 else if (size_tflag) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
890 sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
|
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 sprintf(realbuffer, fmt, va_arg(vargs, int)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
893 appendstring(realbuffer); |
|
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 case 'u': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
896 makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u'); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
897 if (longflag) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
898 sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
899 else if (size_tflag) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
900 sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
901 else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
902 sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
903 appendstring(realbuffer); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
904 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
905 case 'i': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
906 makefmt(fmt, 0, 0, zeropad, width, precision, 'i'); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
907 sprintf(realbuffer, fmt, va_arg(vargs, int)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
908 appendstring(realbuffer); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
909 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
910 case 'x': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
911 makefmt(fmt, 0, 0, zeropad, width, precision, 'x'); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
912 sprintf(realbuffer, fmt, va_arg(vargs, int)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
913 appendstring(realbuffer); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
914 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
915 case 's': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
916 { |
|
53721
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
917 /* unused, since we already have the result */ |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
918 (void) va_arg(vargs, char *); |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
919 Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult), |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
920 PyUnicode_GET_SIZE(*callresult)); |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
921 s += PyUnicode_GET_SIZE(*callresult); |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
922 /* We're done with the unicode()/repr() => forget it */ |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
923 Py_DECREF(*callresult); |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
924 /* switch to next unicode()/repr() result */ |
|
18a5ae96ae5c
Merged revisions 72260 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
52909
diff
changeset
|
925 ++callresult; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
926 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
927 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
928 case 'U': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
929 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
930 PyObject *obj = va_arg(vargs, PyObject *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
931 Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
932 Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
933 s += size; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
934 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
935 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
936 case 'V': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
937 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
938 PyObject *obj = va_arg(vargs, PyObject *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
939 const char *str = va_arg(vargs, const char *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
940 if (obj) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
941 Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
942 Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
943 s += size; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
944 } else { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
945 appendstring(str); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
946 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
947 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
948 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
949 case 'S': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
950 case 'R': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
951 { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
952 Py_UNICODE *ucopy; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
953 Py_ssize_t usize; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
954 Py_ssize_t upos; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
955 /* unused, since we already have the result */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
956 (void) va_arg(vargs, PyObject *); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
957 ucopy = PyUnicode_AS_UNICODE(*callresult); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
958 usize = PyUnicode_GET_SIZE(*callresult); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
959 for (upos = 0; upos<usize;) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
960 *s++ = ucopy[upos++]; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
961 /* We're done with the unicode()/repr() => forget it */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
962 Py_DECREF(*callresult); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
963 /* switch to next unicode()/repr() result */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
964 ++callresult; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
965 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
966 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
967 case 'p': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
968 sprintf(buffer, "%p", va_arg(vargs, void*)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
969 /* %p is ill-defined: ensure leading 0x. */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
970 if (buffer[1] == 'X') |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
971 buffer[1] = 'x'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
972 else if (buffer[1] != 'x') { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
973 memmove(buffer+2, buffer, strlen(buffer)+1); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
974 buffer[0] = '0'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
975 buffer[1] = 'x'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
976 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
977 appendstring(buffer); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
978 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
979 case '%': |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
980 *s++ = '%'; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
981 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
982 default: |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
983 appendstring(p); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
984 goto end; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
985 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
986 } else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
987 *s++ = *f; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
988 } |
|
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
|
989 |
| 51216 | 990 end: |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
991 if (callresults) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
992 PyObject_Free(callresults); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
993 if (abuffer) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
994 PyObject_Free(abuffer); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
995 PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
996 return string; |
| 51216 | 997 fail: |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
998 if (callresults) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
999 PyObject **callresult2 = callresults; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1000 while (callresult2 < callresult) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1001 Py_DECREF(*callresult2); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1002 ++callresult2; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1003 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1004 PyObject_Free(callresults); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1005 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1006 if (abuffer) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1007 PyObject_Free(abuffer); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1008 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
|
1009 } |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
1010 |
|
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
|
1011 #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
|
1012 |
|
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
|
1013 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
|
1014 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
|
1015 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1016 PyObject* ret; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1017 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
|
1018 |
|
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
|
1019 #ifdef HAVE_STDARG_PROTOTYPES |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1020 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
|
1021 #else |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1022 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
|
1023 #endif |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1024 ret = PyUnicode_FromFormatV(format, vargs); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1025 va_end(vargs); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1026 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
|
1027 } |
|
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
|
1028 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1029 Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 51216 | 1030 wchar_t *w, |
| 1031 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
|
1032 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1033 if (unicode == NULL) { |
| 51216 | 1034 PyErr_BadInternalCall(); |
| 1035 return -1; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1036 } |
|
34016
264e45baacd5
Correct the handling of 0-termination of PyUnicode_AsWideChar()
Marc-André Lemburg <mal@egenix.com>
parents:
33766
diff
changeset
|
1037 |
|
264e45baacd5
Correct the handling of 0-termination of PyUnicode_AsWideChar()
Marc-André Lemburg <mal@egenix.com>
parents:
33766
diff
changeset
|
1038 /* 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
|
1039 if (size > PyUnicode_GET_SIZE(unicode)) |
| 51216 | 1040 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
|
1041 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1042 #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
|
1043 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
|
1044 #else |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1045 { |
| 51216 | 1046 register Py_UNICODE *u; |
| 1047 register Py_ssize_t i; | |
| 1048 u = PyUnicode_AS_UNICODE(unicode); | |
| 1049 for (i = size; i > 0; i--) | |
| 1050 *w++ = *u++; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1051 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1052 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1053 |
|
34016
264e45baacd5
Correct the handling of 0-termination of PyUnicode_AsWideChar()
Marc-André Lemburg <mal@egenix.com>
parents:
33766
diff
changeset
|
1054 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
|
1055 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
|
1056 else |
| 51216 | 1057 return size; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1058 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1059 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1060 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1061 |
|
24737
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1062 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
|
1063 { |
|
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
|
1064 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
|
1065 |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1066 #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
|
1067 if (ordinal < 0 || ordinal > 0x10ffff) { |
| 51216 | 1068 PyErr_SetString(PyExc_ValueError, |
| 1069 "unichr() arg not in range(0x110000) " | |
| 1070 "(wide Python build)"); | |
| 1071 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
|
1072 } |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1073 #else |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1074 if (ordinal < 0 || ordinal > 0xffff) { |
| 51216 | 1075 PyErr_SetString(PyExc_ValueError, |
| 1076 "unichr() arg not in range(0x10000) " | |
| 1077 "(narrow Python build)"); | |
| 1078 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
|
1079 } |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1080 #endif |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1081 |
|
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
|
1082 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
|
1083 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
|
1084 } |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1085 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1086 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
|
1087 { |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1088 /* XXX Perhaps we should make this API an alias of |
| 51216 | 1089 PyObject_Unicode() instead ?! */ |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1090 if (PyUnicode_CheckExact(obj)) { |
| 51216 | 1091 Py_INCREF(obj); |
| 1092 return obj; | |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1093 } |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1094 if (PyUnicode_Check(obj)) { |
| 51216 | 1095 /* For a Unicode subtype that's not a Unicode object, |
| 1096 return a true Unicode object with the same data. */ | |
| 1097 return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), | |
| 1098 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
|
1099 } |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1100 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
|
1101 } |
|
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1102 |
|
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1103 PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 51216 | 1104 const char *encoding, |
| 1105 const char *errors) | |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1106 { |
|
19999
1f4e2627b627
Implement the changes proposed in patch #413333. unicode(obj) now
Marc-André Lemburg <mal@egenix.com>
parents:
19998
diff
changeset
|
1107 const char *s = NULL; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1108 Py_ssize_t len; |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1109 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
|
1110 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1111 if (obj == NULL) { |
| 51216 | 1112 PyErr_BadInternalCall(); |
| 1113 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1114 } |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1115 |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1116 #if 0 |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1117 /* 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
|
1118 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
|
1119 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
|
1120 Unicode subclasses. |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1121 |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1122 NOTE: This API should really only be used for object which |
| 51216 | 1123 represent *encoded* Unicode ! |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1124 |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1125 */ |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1126 if (PyUnicode_Check(obj)) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1127 if (encoding) { |
| 51216 | 1128 PyErr_SetString(PyExc_TypeError, |
| 1129 "decoding Unicode is not supported"); | |
| 1130 return NULL; | |
| 1131 } | |
| 1132 return PyObject_Unicode(obj); | |
| 1133 } | |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1134 #else |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1135 if (PyUnicode_Check(obj)) { |
| 51216 | 1136 PyErr_SetString(PyExc_TypeError, |
| 1137 "decoding Unicode is not supported"); | |
| 1138 return NULL; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1139 } |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1140 #endif |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1141 |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1142 /* Coerce object */ |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1143 if (PyString_Check(obj)) { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1144 s = PyString_AS_STRING(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1145 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
|
1146 } |
|
47291
04bd3690fb1f
First step of the C API rename:
Christian Heimes <christian@cheimes.de>
parents:
46908
diff
changeset
|
1147 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
|
1148 /* 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
|
1149 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
|
1150 "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
|
1151 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
|
1152 } |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1153 else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 51216 | 1154 /* Overwrite the error message with something more useful in |
| 1155 case of a TypeError. */ | |
| 1156 if (PyErr_ExceptionMatches(PyExc_TypeError)) | |
| 1157 PyErr_Format(PyExc_TypeError, | |
| 1158 "coercing to Unicode: need string or buffer, " | |
| 1159 "%.80s found", | |
| 1160 Py_TYPE(obj)->tp_name); | |
| 1161 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
|
1162 } |
|
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 |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1164 /* 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
|
1165 if (len == 0) { |
| 51216 | 1166 Py_INCREF(unicode_empty); |
| 1167 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
|
1168 } |
|
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
|
1169 else |
| 51216 | 1170 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
|
1171 |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1172 return v; |
|
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1173 |
| 51216 | 1174 onError: |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1175 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1176 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1177 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1178 PyObject *PyUnicode_Decode(const char *s, |
| 51216 | 1179 Py_ssize_t size, |
| 1180 const char *encoding, | |
| 1181 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
|
1182 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1183 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
|
1184 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1185 if (encoding == NULL) |
| 51216 | 1186 encoding = PyUnicode_GetDefaultEncoding(); |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1187 |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1188 /* Shortcuts for common default encodings */ |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1189 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
|
1190 return PyUnicode_DecodeUTF8(s, size, errors); |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1191 else if (strcmp(encoding, "latin-1") == 0) |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1192 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
|
1193 #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
|
1194 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
|
1195 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
|
1196 #endif |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1197 else if (strcmp(encoding, "ascii") == 0) |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1198 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
|
1199 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1200 /* 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
|
1201 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
|
1202 if (buffer == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1203 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1204 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
|
1205 if (unicode == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1206 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1207 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
|
1208 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
|
1209 "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
|
1210 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
|
1211 Py_DECREF(unicode); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1212 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1213 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1214 Py_DECREF(buffer); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1215 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
|
1216 |
| 51216 | 1217 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1218 Py_XDECREF(buffer); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1219 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1220 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1221 |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1222 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
|
1223 const char *encoding, |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1224 const char *errors) |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1225 { |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1226 PyObject *v; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1227 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1228 if (!PyUnicode_Check(unicode)) { |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1229 PyErr_BadArgument(); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1230 goto onError; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1231 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1232 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1233 if (encoding == NULL) |
| 51216 | 1234 encoding = PyUnicode_GetDefaultEncoding(); |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1235 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1236 /* 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
|
1237 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
|
1238 if (v == NULL) |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1239 goto onError; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1240 return v; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1241 |
| 51216 | 1242 onError: |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1243 return NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1244 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1245 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1246 PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
| 51216 | 1247 Py_ssize_t size, |
| 1248 const char *encoding, | |
| 1249 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
|
1250 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1251 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
|
1252 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1253 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
|
1254 if (unicode == NULL) |
| 51216 | 1255 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1256 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
|
1257 Py_DECREF(unicode); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1258 return v; |
|
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 |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1261 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
|
1262 const char *encoding, |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1263 const char *errors) |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1264 { |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1265 PyObject *v; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1266 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1267 if (!PyUnicode_Check(unicode)) { |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1268 PyErr_BadArgument(); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1269 goto onError; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1270 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1271 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1272 if (encoding == NULL) |
| 51216 | 1273 encoding = PyUnicode_GetDefaultEncoding(); |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1274 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1275 /* 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
|
1276 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
|
1277 if (v == NULL) |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1278 goto onError; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1279 return v; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1280 |
| 51216 | 1281 onError: |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1282 return NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1283 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1284 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1285 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
|
1286 const char *encoding, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1287 const char *errors) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1288 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1289 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
|
1290 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1291 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
|
1292 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1293 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1294 } |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1295 |
|
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
|
1296 if (encoding == NULL) |
| 51216 | 1297 encoding = PyUnicode_GetDefaultEncoding(); |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1298 |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1299 /* Shortcuts for common default encodings */ |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1300 if (errors == NULL) { |
| 51216 | 1301 if (strcmp(encoding, "utf-8") == 0) |
| 1302 return PyUnicode_AsUTF8String(unicode); | |
| 1303 else if (strcmp(encoding, "latin-1") == 0) | |
| 1304 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
|
1305 #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 51216 | 1306 else if (strcmp(encoding, "mbcs") == 0) |
| 1307 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
|
1308 #endif |
| 51216 | 1309 else if (strcmp(encoding, "ascii") == 0) |
| 1310 return PyUnicode_AsASCIIString(unicode); | |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1311 } |
|
11968
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 /* 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
|
1314 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
|
1315 if (v == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1316 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1317 if (!PyString_Check(v)) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1318 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
|
1319 "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
|
1320 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
|
1321 Py_DECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1322 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1323 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1324 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
|
1325 |
| 51216 | 1326 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1327 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1328 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1329 |
|
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
|
1330 PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 51216 | 1331 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
|
1332 { |
|
2badb322355f
This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents:
13828
diff
changeset
|
1333 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
|
1334 |
|
2badb322355f
This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents:
13828
diff
changeset
|
1335 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
|
1336 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
|
1337 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
|
1338 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
|
1339 ((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
|
1340 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
|
1341 } |
|
2badb322355f
This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents:
13828
diff
changeset
|
1342 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1343 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
|
1344 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1345 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
|
1346 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1347 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1348 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1349 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
|
1350 |
| 51216 | 1351 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1352 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1353 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1354 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1355 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
|
1356 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1357 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
|
1358 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1359 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1360 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1361 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
|
1362 |
| 51216 | 1363 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1364 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1365 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1366 |
|
13660
a1ce07218781
Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents:
13635
diff
changeset
|
1367 const char *PyUnicode_GetDefaultEncoding(void) |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1368 { |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1369 return unicode_default_encoding; |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1370 } |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1371 |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1372 int PyUnicode_SetDefaultEncoding(const char *encoding) |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1373 { |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1374 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
|
1375 |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1376 /* 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
|
1377 loads the encoding into the codec registry cache. */ |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1378 v = _PyCodec_Lookup(encoding); |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1379 if (v == NULL) |
| 51216 | 1380 goto onError; |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1381 Py_DECREF(v); |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1382 strncpy(unicode_default_encoding, |
| 51216 | 1383 encoding, |
| 1384 sizeof(unicode_default_encoding)); | |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1385 return 0; |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1386 |
| 51216 | 1387 onError: |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1388 return -1; |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1389 } |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1390 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1391 /* 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
|
1392 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
|
1393 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
|
1394 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
|
1395 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
|
1396 */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1397 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1398 static |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1399 int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 51216 | 1400 const char *encoding, const char *reason, |
| 1401 const char *input, Py_ssize_t insize, Py_ssize_t *startinpos, | |
| 1402 Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, | |
| 1403 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
|
1404 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1405 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
|
1406 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1407 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
|
1408 PyObject *repunicode = NULL; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1409 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
|
1410 Py_ssize_t requiredsize; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1411 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
|
1412 Py_UNICODE *repptr; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1413 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
|
1414 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
|
1415 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1416 if (*errorHandler == NULL) { |
| 51216 | 1417 *errorHandler = PyCodec_LookupError(errors); |
| 1418 if (*errorHandler == NULL) | |
| 1419 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
|
1420 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1421 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1422 if (*exceptionObject == NULL) { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1423 *exceptionObject = PyUnicodeDecodeError_Create( |
| 51216 | 1424 encoding, input, insize, *startinpos, *endinpos, reason); |
| 1425 if (*exceptionObject == NULL) | |
| 1426 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
|
1427 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1428 else { |
| 51216 | 1429 if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1430 goto onError; | |
| 1431 if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) | |
| 1432 goto onError; | |
| 1433 if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) | |
| 1434 goto onError; | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1435 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1436 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1437 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
|
1438 if (restuple == NULL) |
| 51216 | 1439 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
|
1440 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
|
1441 PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 51216 | 1442 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
|
1443 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1444 if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 51216 | 1445 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
|
1446 if (newpos<0) |
| 51216 | 1447 newpos = insize+newpos; |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
1448 if (newpos<0 || newpos>insize) { |
| 51216 | 1449 PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 1450 goto onError; | |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
1451 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1452 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1453 /* 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
|
1454 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
|
1455 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
|
1456 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
|
1457 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
|
1458 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
|
1459 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
|
1460 if (requiredsize > outsize) { |
| 51216 | 1461 if (requiredsize<2*outsize) |
| 1462 requiredsize = 2*outsize; | |
| 1463 if (_PyUnicode_Resize(output, requiredsize) < 0) | |
| 1464 goto onError; | |
| 1465 *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
|
1466 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1467 *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
|
1468 *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
|
1469 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
|
1470 *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
|
1471 *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
|
1472 /* 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
|
1473 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
|
1474 |
| 51216 | 1475 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
|
1476 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
|
1477 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
|
1478 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1479 |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1480 /* --- UTF-7 Codec -------------------------------------------------------- */ |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1481 |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1482 /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1483 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1484 /* Three simple macros defining base-64. */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1485 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1486 /* Is c a base-64 character? */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1487 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1488 #define IS_BASE64(c) \ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1489 (isalnum(c) || (c) == '+' || (c) == '/') |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1490 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1491 /* given that c is a base-64 character, what is its base-64 value? */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1492 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1493 #define FROM_BASE64(c) \ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1494 (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1495 ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1496 ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1497 (c) == '+' ? 62 : 63) |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1498 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1499 /* What is the base-64 character of the bottom 6 bits of n? */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1500 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1501 #define TO_BASE64(n) \ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1502 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1503 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1504 /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1505 * decoded as itself. We are permissive on decoding; the only ASCII |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1506 * byte not decoding to itself is the + which begins a base64 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1507 * string. */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1508 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1509 #define DECODE_DIRECT(c) \ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1510 ((c) <= 127 && (c) != '+') |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1511 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1512 /* The UTF-7 encoder treats ASCII characters differently according to |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1513 * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1514 * the above). See RFC2152. This array identifies these different |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1515 * sets: |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1516 * 0 : "Set D" |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1517 * alphanumeric and '(),-./:? |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1518 * 1 : "Set O" |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1519 * !"#$%&*;<=>@[]^_`{|} |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1520 * 2 : "whitespace" |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1521 * ht nl cr sp |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1522 * 3 : special (must be base64 encoded) |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1523 * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1524 */ |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1525 |
|
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
|
1526 static |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1527 char utf7_category[128] = { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1528 /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1529 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1530 /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1531 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1532 /* sp ! " # $ % & ' ( ) * + , - . / */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1533 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1534 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1535 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1536 /* @ A B C D E F G H I J K L M N O */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1537 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1538 /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1539 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1540 /* ` a b c d e f g h i j k l m n o */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1541 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1542 /* p q r s t u v w x y z { | } ~ del */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1543 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 3, 3, |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1544 }; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1545 |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1546 /* ENCODE_DIRECT: this character should be encoded as itself. The |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1547 * answer depends on whether we are encoding set O as itself, and also |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1548 * on whether we are encoding whitespace as itself. RFC2152 makes it |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1549 * clear that the answers to these questions vary between |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1550 * applications, so this code needs to be flexible. */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1551 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1552 #define ENCODE_DIRECT(c, directO, directWS) \ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1553 ((c) < 128 && (c) > 0 && \ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1554 ((utf7_category[(c)] == 0) || \ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1555 (directWS && (utf7_category[(c)] == 2)) || \ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1556 (directO && (utf7_category[(c)] == 1)))) |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1557 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1558 PyObject *PyUnicode_DecodeUTF7(const char *s, |
| 51216 | 1559 Py_ssize_t size, |
| 1560 const char *errors) | |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1561 { |
|
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
|
1562 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
|
1563 } |
|
e8ac1ae4503c
The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43888
diff
changeset
|
1564 |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1565 /* The decoder. The only state we preserve is our read position, |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1566 * i.e. how many characters we have consumed. So if we end in the |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1567 * middle of a shift sequence we have to back off the read position |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1568 * and the output to the beginning of the sequence, otherwise we lose |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1569 * all the shift state (seen bits, number of bits seen, high |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1570 * surrogate). */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1571 |
|
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
|
1572 PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
| 51216 | 1573 Py_ssize_t size, |
| 1574 const char *errors, | |
| 1575 Py_ssize_t *consumed) | |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1576 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1577 const char *starts = s; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1578 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1579 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1580 Py_ssize_t outpos; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1581 const char *e; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1582 PyUnicodeObject *unicode; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1583 Py_UNICODE *p; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1584 const char *errmsg = ""; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1585 int inShift = 0; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1586 Py_UNICODE *shiftOutStart; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1587 unsigned int base64bits = 0; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1588 unsigned long base64buffer = 0; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1589 Py_UNICODE surrogate = 0; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1590 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
|
1591 PyObject *exc = NULL; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1592 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1593 unicode = _PyUnicode_New(size); |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1594 if (!unicode) |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1595 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
|
1596 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
|
1597 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
|
1598 *consumed = 0; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1599 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
|
1600 } |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1601 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1602 p = unicode->str; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1603 shiftOutStart = p; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1604 e = s + size; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1605 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1606 while (s < e) { |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1607 Py_UNICODE ch = (unsigned char) *s; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1608 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1609 if (inShift) { /* in a base-64 section */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1610 if (IS_BASE64(ch)) { /* consume a base-64 character */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1611 base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1612 base64bits += 6; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1613 s++; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1614 if (base64bits >= 16) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1615 /* we have enough bits for a UTF-16 value */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1616 Py_UNICODE outCh = (Py_UNICODE) |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1617 (base64buffer >> (base64bits-16)); |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1618 base64bits -= 16; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1619 base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1620 if (surrogate) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1621 /* expecting a second surrogate */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1622 if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1623 #ifdef Py_UNICODE_WIDE |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1624 *p++ = (((surrogate & 0x3FF)<<10) |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1625 | (outCh & 0x3FF)) + 0x10000; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1626 #else |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1627 *p++ = surrogate; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1628 *p++ = outCh; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1629 #endif |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1630 surrogate = 0; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1631 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1632 else { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1633 surrogate = 0; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1634 errmsg = "second surrogate missing"; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1635 goto utf7Error; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1636 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1637 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1638 else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1639 /* first surrogate */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1640 surrogate = outCh; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1641 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1642 else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1643 errmsg = "unexpected second surrogate"; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1644 goto utf7Error; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1645 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1646 else { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1647 *p++ = outCh; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1648 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1649 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1650 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1651 else { /* now leaving a base-64 section */ |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1652 inShift = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1653 s++; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1654 if (surrogate) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1655 errmsg = "second surrogate missing at end of shift sequence"; |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
1656 goto utf7Error; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1657 } |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1658 if (base64bits > 0) { /* left-over bits */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1659 if (base64bits >= 6) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1660 /* We've seen at least one base-64 character */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1661 errmsg = "partial character in shift sequence"; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1662 goto utf7Error; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1663 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1664 else { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1665 /* Some bits remain; they should be zero */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1666 if (base64buffer != 0) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1667 errmsg = "non-zero padding bits in shift sequence"; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1668 goto utf7Error; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1669 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1670 } |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1671 } |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1672 if (ch != '-') { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1673 /* '-' is absorbed; other terminating |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1674 characters are preserved */ |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1675 *p++ = ch; |
|
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 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1678 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1679 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
|
1680 startinpos = s-starts; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1681 s++; /* consume '+' */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1682 if (s < e && *s == '-') { /* '+-' encodes '+' */ |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1683 s++; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1684 *p++ = '+'; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1685 } |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1686 else { /* begin base64-encoded section */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1687 inShift = 1; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1688 shiftOutStart = p; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1689 base64bits = 0; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1690 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1691 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1692 else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1693 *p++ = ch; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1694 s++; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1695 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1696 else { |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1697 startinpos = s-starts; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1698 s++; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1699 errmsg = "unexpected special character"; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1700 goto utf7Error; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1701 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1702 continue; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1703 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
|
1704 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
|
1705 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
|
1706 if (unicode_decode_call_errorhandler( |
| 51216 | 1707 errors, &errorHandler, |
| 1708 "utf7", errmsg, | |
| 1709 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 1710 &unicode, &outpos, &p)) | |
| 1711 goto onError; | |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1712 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1713 |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1714 /* end of string */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1715 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1716 if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1717 /* if we're in an inconsistent state, that's an error */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1718 if (surrogate || |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1719 (base64bits >= 6) || |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1720 (base64bits > 0 && base64buffer != 0)) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1721 outpos = p-PyUnicode_AS_UNICODE(unicode); |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1722 endinpos = size; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1723 if (unicode_decode_call_errorhandler( |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1724 errors, &errorHandler, |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1725 "utf7", "unterminated shift sequence", |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1726 starts, size, &startinpos, &endinpos, &exc, &s, |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1727 &unicode, &outpos, &p)) |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1728 goto onError; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1729 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1730 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1731 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1732 /* return state */ |
|
43932
e8ac1ae4503c
The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43888
diff
changeset
|
1733 if (consumed) { |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1734 if (inShift) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1735 p = shiftOutStart; /* back off output */ |
|
43932
e8ac1ae4503c
The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43888
diff
changeset
|
1736 *consumed = startinpos; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1737 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1738 else { |
|
43932
e8ac1ae4503c
The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43888
diff
changeset
|
1739 *consumed = s-starts; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1740 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1741 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1742 |
|
30431
cd0c3f916b34
Change checks of PyUnicode_Resize() return value for clarity.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
30216
diff
changeset
|
1743 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
|
1744 goto onError; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1745 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1746 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
|
1747 Py_XDECREF(exc); |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1748 return (PyObject *)unicode; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1749 |
| 51216 | 1750 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
|
1751 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
|
1752 Py_XDECREF(exc); |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1753 Py_DECREF(unicode); |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1754 return NULL; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1755 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1756 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1757 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1758 PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 51216 | 1759 Py_ssize_t size, |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1760 int base64SetO, |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1761 int base64WhiteSpace, |
| 51216 | 1762 const char *errors) |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1763 { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1764 PyObject *v; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1765 /* It might be possible to tighten this worst case */ |
|
55133
38d6e4e51af0
Grow the allocated buffer in PyUnicode_EncodeUTF7 to avoid buffer overrun.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
54600
diff
changeset
|
1766 Py_ssize_t allocated = 8 * size; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1767 int inShift = 0; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1768 Py_ssize_t i = 0; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1769 unsigned int base64bits = 0; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1770 unsigned long base64buffer = 0; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1771 char * out; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1772 char * start; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1773 |
|
55133
38d6e4e51af0
Grow the allocated buffer in PyUnicode_EncodeUTF7 to avoid buffer overrun.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
54600
diff
changeset
|
1774 if (allocated / 8 != size) |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
1775 return PyErr_NoMemory(); |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
1776 |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1777 if (size == 0) |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
1778 return PyString_FromStringAndSize(NULL, 0); |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1779 |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1780 v = PyString_FromStringAndSize(NULL, allocated); |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1781 if (v == NULL) |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1782 return NULL; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1783 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1784 start = out = PyString_AS_STRING(v); |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1785 for (;i < size; ++i) { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1786 Py_UNICODE ch = s[i]; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1787 |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1788 if (inShift) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1789 if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1790 /* shifting out */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1791 if (base64bits) { /* output remaining bits */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1792 *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1793 base64buffer = 0; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1794 base64bits = 0; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1795 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1796 inShift = 0; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1797 /* Characters not in the BASE64 set implicitly unshift the sequence |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1798 so no '-' is required, except if the character is itself a '-' */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1799 if (IS_BASE64(ch) || ch == '-') { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1800 *out++ = '-'; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1801 } |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1802 *out++ = (char) ch; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1803 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1804 else { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1805 goto encode_char; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1806 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1807 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1808 else { /* not in a shift sequence */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1809 if (ch == '+') { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1810 *out++ = '+'; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1811 *out++ = '-'; |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1812 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1813 else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1814 *out++ = (char) ch; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1815 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1816 else { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1817 *out++ = '+'; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1818 inShift = 1; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1819 goto encode_char; |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
1820 } |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1821 } |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1822 continue; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1823 encode_char: |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1824 #ifdef Py_UNICODE_WIDE |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1825 if (ch >= 0x10000) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1826 /* code first surrogate */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1827 base64bits += 16; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1828 base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1829 while (base64bits >= 6) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1830 *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1831 base64bits -= 6; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1832 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1833 /* prepare second surrogate */ |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1834 ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1835 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1836 #endif |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1837 base64bits += 16; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1838 base64buffer = (base64buffer << 16) | ch; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1839 while (base64bits >= 6) { |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1840 *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1841 base64bits -= 6; |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1842 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1843 } |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1844 if (base64bits) |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1845 *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1846 if (inShift) |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1847 *out++ = '-'; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1848 |
|
60203
86f699766016
more _PyString_Resize error checking
Benjamin Peterson <benjamin@python.org>
parents:
60074
diff
changeset
|
1849 if (_PyString_Resize(&v, out - start)) |
|
86f699766016
more _PyString_Resize error checking
Benjamin Peterson <benjamin@python.org>
parents:
60074
diff
changeset
|
1850 return NULL; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1851 return v; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1852 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1853 |
|
53740
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1854 #undef IS_BASE64 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1855 #undef FROM_BASE64 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1856 #undef TO_BASE64 |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1857 #undef DECODE_DIRECT |
|
12d8fd499722
Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
Antoine Pitrou <solipsis@pitrou.net>
parents:
53722
diff
changeset
|
1858 #undef ENCODE_DIRECT |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1859 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1860 /* --- UTF-8 Codec -------------------------------------------------------- */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1861 |
|
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
|
1862 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1863 char utf8_code_length[256] = { |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1864 /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1865 illegal prefix. See RFC 3629 for details */ |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1866 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1867 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1868 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1869 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
|
1870 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
|
1871 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
|
1872 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1873 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1874 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80-8F */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1875 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
|
1876 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1877 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1878 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1879 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1880 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1881 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0-F4 + F5-FF */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1882 }; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1883 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1884 PyObject *PyUnicode_DecodeUTF8(const char *s, |
| 51216 | 1885 Py_ssize_t size, |
| 1886 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
|
1887 { |
|
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
|
1888 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
|
1889 } |
|
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
|
1890 |
|
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
|
1891 PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
| 51216 | 1892 Py_ssize_t size, |
| 1893 const char *errors, | |
| 1894 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
|
1895 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1896 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
|
1897 int n; |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1898 int k; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1899 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1900 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1901 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
|
1902 const char *e; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1903 PyUnicodeObject *unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1904 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
|
1905 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
|
1906 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
|
1907 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
|
1908 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1909 /* 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
|
1910 character count */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1911 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
|
1912 if (!unicode) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1913 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
|
1914 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
|
1915 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
|
1916 *consumed = 0; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1917 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
|
1918 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1919 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1920 /* 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
|
1921 p = unicode->str; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1922 e = s + size; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1923 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1924 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
|
1925 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
|
1926 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1927 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
|
1928 *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
|
1929 s++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1930 continue; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1931 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1932 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1933 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
|
1934 |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1935 if (s + n > e) { |
| 51216 | 1936 if (consumed) |
| 1937 break; | |
| 1938 else { | |
| 1939 errmsg = "unexpected end of data"; | |
| 1940 startinpos = s-starts; | |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1941 endinpos = startinpos+1; |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1942 for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1943 endinpos++; |
| 51216 | 1944 goto utf8Error; |
| 1945 } | |
| 1946 } | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1947 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1948 switch (n) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1949 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1950 case 0: |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1951 errmsg = "invalid start byte"; |
| 51216 | 1952 startinpos = s-starts; |
| 1953 endinpos = startinpos+1; | |
| 1954 goto utf8Error; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1955 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1956 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
|
1957 errmsg = "internal error"; |
| 51216 | 1958 startinpos = s-starts; |
| 1959 endinpos = startinpos+1; | |
| 1960 goto utf8Error; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1961 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1962 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
|
1963 if ((s[1] & 0xc0) != 0x80) { |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1964 errmsg = "invalid continuation byte"; |
| 51216 | 1965 startinpos = s-starts; |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1966 endinpos = startinpos + 1; |
| 51216 | 1967 goto utf8Error; |
| 1968 } | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1969 ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1970 assert ((ch > 0x007F) && (ch <= 0x07FF)); |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1971 *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
|
1972 break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1973 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1974 case 3: |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1975 /* XXX: surrogates shouldn't be valid UTF-8! |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1976 see http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1977 (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1978 Uncomment the 2 lines below to make them invalid, |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1979 codepoints: d800-dfff; UTF-8: \xed\xa0\x80-\xed\xbf\xbf. */ |
|
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
|
1980 if ((s[1] & 0xc0) != 0x80 || |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1981 (s[2] & 0xc0) != 0x80 || |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1982 ((unsigned char)s[0] == 0xE0 && |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1983 (unsigned char)s[1] < 0xA0)/* || |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1984 ((unsigned char)s[0] == 0xED && |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1985 (unsigned char)s[1] > 0x9F)*/) { |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1986 errmsg = "invalid continuation byte"; |
| 51216 | 1987 startinpos = s-starts; |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1988 endinpos = startinpos + 1; |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1989 |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1990 /* if s[1] first two bits are 1 and 0, then the invalid |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1991 continuation byte is s[2], so increment endinpos by 1, |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1992 if not, s[1] is invalid and endinpos doesn't need to |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1993 be incremented. */ |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1994 if ((s[1] & 0xC0) == 0x80) |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1995 endinpos++; |
| 51216 | 1996 goto utf8Error; |
| 1997 } | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1998 ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
1999 assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2000 *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
|
2001 break; |
|
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
2002 |
|
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
2003 case 4: |
|
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
2004 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
|
2005 (s[2] & 0xc0) != 0x80 || |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2006 (s[3] & 0xc0) != 0x80 || |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2007 ((unsigned char)s[0] == 0xF0 && |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2008 (unsigned char)s[1] < 0x90) || |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2009 ((unsigned char)s[0] == 0xF4 && |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2010 (unsigned char)s[1] > 0x8F)) { |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2011 errmsg = "invalid continuation byte"; |
| 51216 | 2012 startinpos = s-starts; |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2013 endinpos = startinpos + 1; |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2014 if ((s[1] & 0xC0) == 0x80) { |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2015 endinpos++; |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2016 if ((s[2] & 0xC0) == 0x80) |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2017 endinpos++; |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2018 } |
| 51216 | 2019 goto utf8Error; |
| 2020 } | |
|
13411
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
2021 ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
|
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
2022 ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
|
62711
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2023 assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
|
4de13feaea01
Merged revisions 81758-81759 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62520
diff
changeset
|
2024 |
|
18324
3ba7a358ce02
use Py_UNICODE_WIDE instead of USE_UCS4_STORAGE and Py_UNICODE_SIZE
Fredrik Lundh <fredrik@pythonware.com>
parents:
18298
diff
changeset
|
2025 #ifdef Py_UNICODE_WIDE |
| 51216 | 2026 *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
|
2027 #else |
|
13411
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
2028 /* 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
|
2029 |
|
13411
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
2030 /* 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
|
2031 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
|
2032 |
|
13411
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
2033 /* 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
|
2034 *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
|
2035 |
|
13411
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
2036 /* 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
|
2037 *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
|
2038 #endif |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2039 break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2040 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2041 s += n; |
| 51216 | 2042 continue; |
| 2043 | |
| 2044 utf8Error: | |
| 2045 outpos = p-PyUnicode_AS_UNICODE(unicode); | |
| 2046 if (unicode_decode_call_errorhandler( | |
| 2047 errors, &errorHandler, | |
| 2048 "utf8", errmsg, | |
| 2049 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 2050 &unicode, &outpos, &p)) | |
| 2051 goto onError; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2052 } |
|
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
|
2053 if (consumed) |
| 51216 | 2054 *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
|
2055 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2056 /* Adjust length */ |
|
30431
cd0c3f916b34
Change checks of PyUnicode_Resize() return value for clarity.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
30216
diff
changeset
|
2057 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
|
2058 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2059 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2060 Py_XDECREF(errorHandler); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2061 Py_XDECREF(exc); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2062 return (PyObject *)unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2063 |
| 51216 | 2064 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
|
2065 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
|
2066 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
|
2067 Py_DECREF(unicode); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2068 return NULL; |
|
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 |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2071 /* 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
|
2072 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
|
2073 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
|
2074 the excess memory at the end. |
| 23203 | 2075 */ |
|
23198
950a22eb8cc8
PyUnicode_EncodeUTF8: squash compiler wng. The difference of two
Tim Peters <tim.peters@gmail.com>
parents:
23192
diff
changeset
|
2076 PyObject * |
|
950a22eb8cc8
PyUnicode_EncodeUTF8: squash compiler wng. The difference of two
Tim Peters <tim.peters@gmail.com>
parents:
23192
diff
changeset
|
2077 PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 51216 | 2078 Py_ssize_t size, |
| 2079 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
|
2080 { |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2081 #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
|
2082 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2083 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
|
2084 PyObject *v; /* result string object */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2085 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
|
2086 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
|
2087 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
|
2088 char stackbuf[MAX_SHORT_UNICHARS * 4]; |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2089 |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2090 assert(s != NULL); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2091 assert(size >= 0); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2092 |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2093 if (size <= MAX_SHORT_UNICHARS) { |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2094 /* 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
|
2095 * 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
|
2096 * turns out we need. |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2097 */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2098 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
|
2099 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
|
2100 p = stackbuf; |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2101 } |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2102 else { |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2103 /* 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
|
2104 nallocated = size * 4; |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2105 if (nallocated / 4 != size) /* overflow! */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2106 return PyErr_NoMemory(); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2107 v = PyString_FromStringAndSize(NULL, nallocated); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2108 if (v == NULL) |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2109 return NULL; |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2110 p = PyString_AS_STRING(v); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2111 } |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2112 |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2113 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
|
2114 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
|
2115 |
| 23203 | 2116 if (ch < 0x80) |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2117 /* Encode ASCII */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2118 *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
|
2119 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2120 else if (ch < 0x0800) { |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2121 /* Encode Latin-1 */ |
| 21884 | 2122 *p++ = (char)(0xc0 | (ch >> 6)); |
| 2123 *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
|
2124 } |
|
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
|
2125 else { |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2126 /* Encode UCS2 Unicode ordinals */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2127 if (ch < 0x10000) { |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2128 /* Special case: check for high surrogate */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2129 if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2130 Py_UCS4 ch2 = s[i]; |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2131 /* 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
|
2132 form a UCS4 value */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2133 if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 23203 | 2134 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
|
2135 i++; |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2136 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
|
2137 } |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2138 /* 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
|
2139 } |
|
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
2140 *p++ = (char)(0xe0 | (ch >> 12)); |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2141 *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2142 *p++ = (char)(0x80 | (ch & 0x3f)); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2143 continue; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
2144 } |
| 51216 | 2145 encodeUCS4: |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2146 /* Encode UCS4 Unicode ordinals */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2147 *p++ = (char)(0xf0 | (ch >> 18)); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2148 *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2149 *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2150 *p++ = (char)(0x80 | (ch & 0x3f)); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2151 } |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2152 } |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2153 |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2154 if (v == NULL) { |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2155 /* 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
|
2156 nneeded = p - stackbuf; |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2157 assert(nneeded <= nallocated); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2158 v = PyString_FromStringAndSize(stackbuf, nneeded); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2159 } |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2160 else { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
2161 /* Cut back to size actually needed. */ |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
2162 nneeded = p - PyString_AS_STRING(v); |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2163 assert(nneeded <= nallocated); |
|
60203
86f699766016
more _PyString_Resize error checking
Benjamin Peterson <benjamin@python.org>
parents:
60074
diff
changeset
|
2164 if (_PyString_Resize(&v, nneeded)) |
|
86f699766016
more _PyString_Resize error checking
Benjamin Peterson <benjamin@python.org>
parents:
60074
diff
changeset
|
2165 return NULL; |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2166 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2167 return v; |
| 23203 | 2168 |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2169 #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
|
2170 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2171 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2172 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
|
2173 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2174 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
|
2175 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2176 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2177 } |
|
14059
d0348a26b093
PyUnicode_AsUTF8String(): Don't need to explicitly incref str since
Barry Warsaw <barry@python.org>
parents:
13966
diff
changeset
|
2178 return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 2179 PyUnicode_GET_SIZE(unicode), |
| 2180 NULL); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2181 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2182 |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2183 /* --- 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
|
2184 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2185 PyObject * |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2186 PyUnicode_DecodeUTF32(const char *s, |
| 51216 | 2187 Py_ssize_t size, |
| 2188 const char *errors, | |
| 2189 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
|
2190 { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2191 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
|
2192 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2193 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2194 PyObject * |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2195 PyUnicode_DecodeUTF32Stateful(const char *s, |
| 51216 | 2196 Py_ssize_t size, |
| 2197 const char *errors, | |
| 2198 int *byteorder, | |
| 2199 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
|
2200 { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2201 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
|
2202 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
|
2203 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
|
2204 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
|
2205 PyUnicodeObject *unicode; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2206 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
|
2207 #ifndef Py_UNICODE_WIDE |
|
62257
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2208 int pairs = 0; |
|
62302
73e5354f92e7
fix warning with ucs4
Benjamin Peterson <benjamin@python.org>
parents:
62255
diff
changeset
|
2209 const unsigned char *qq; |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2210 #else |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2211 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
|
2212 #endif |
|
62302
73e5354f92e7
fix warning with ucs4
Benjamin Peterson <benjamin@python.org>
parents:
62255
diff
changeset
|
2213 const unsigned char *q, *e; |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2214 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
|
2215 const char *errmsg = ""; |
|
42650
f2f0e95a797b
Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42649
diff
changeset
|
2216 /* 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
|
2217 #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
|
f2f0e95a797b
Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42649
diff
changeset
|
2218 int iorder[] = {0, 1, 2, 3}; |
|
f2f0e95a797b
Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42649
diff
changeset
|
2219 #else |
|
f2f0e95a797b
Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42649
diff
changeset
|
2220 int iorder[] = {3, 2, 1, 0}; |
|
f2f0e95a797b
Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42649
diff
changeset
|
2221 #endif |
|
42652
9c4f5b1d6b9d
Move another variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42650
diff
changeset
|
2222 PyObject *errorHandler = NULL; |
|
9c4f5b1d6b9d
Move another variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42650
diff
changeset
|
2223 PyObject *exc = NULL; |
|
62257
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2224 |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2225 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
|
2226 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
|
2227 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2228 if (byteorder) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2229 bo = *byteorder; |
|
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 /* 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
|
2232 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
|
2233 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
|
2234 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
|
2235 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
|
2236 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
|
2237 const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 51216 | 2238 (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
|
2239 #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 51216 | 2240 if (bom == 0x0000FEFF) { |
| 2241 q += 4; | |
| 2242 bo = -1; | |
| 2243 } | |
| 2244 else if (bom == 0xFFFE0000) { | |
| 2245 q += 4; | |
| 2246 bo = 1; | |
| 2247 } | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2248 #else |
| 51216 | 2249 if (bom == 0x0000FEFF) { |
| 2250 q += 4; | |
| 2251 bo = 1; | |
| 2252 } | |
| 2253 else if (bom == 0xFFFE0000) { | |
| 2254 q += 4; | |
| 2255 bo = -1; | |
| 2256 } | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2257 #endif |
| 51216 | 2258 } |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2259 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2260 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2261 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
|
2262 /* force LE */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2263 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
|
2264 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
|
2265 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
|
2266 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
|
2267 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2268 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
|
2269 /* force BE */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2270 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
|
2271 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
|
2272 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
|
2273 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
|
2274 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2275 |
|
62257
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2276 /* On narrow builds we split characters outside the BMP into two |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2277 codepoints => count how much extra space we need. */ |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2278 #ifndef Py_UNICODE_WIDE |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2279 for (qq = q; qq < e; qq += 4) |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2280 if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2281 pairs++; |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2282 #endif |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2283 |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2284 /* This might be one to much, because of a BOM */ |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2285 unicode = _PyUnicode_New((size+3)/4+pairs); |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2286 if (!unicode) |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2287 return NULL; |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2288 if (size == 0) |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2289 return (PyObject *)unicode; |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2290 |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2291 /* Unpack UTF-32 encoded data */ |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2292 p = unicode->str; |
|
22c045d1bc8c
Merged revisions 81907 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
59900
diff
changeset
|
2293 |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2294 while (q < e) { |
| 51216 | 2295 Py_UCS4 ch; |
| 2296 /* remaining bytes at the end? (size should be divisible by 4) */ | |
| 2297 if (e-q<4) { | |
| 2298 if (consumed) | |
| 2299 break; | |
| 2300 errmsg = "truncated data"; | |
| 2301 startinpos = ((const char *)q)-starts; | |
| 2302 endinpos = ((const char *)e)-starts; | |
| 2303 goto utf32Error; | |
| 2304 /* The remaining input chars are ignored if the callback | |
| 2305 chooses to skip the input */ | |
| 2306 } | |
| 2307 ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | | |
| 2308 (q[iorder[1]] << 8) | q[iorder[0]]; | |
| 2309 | |
| 2310 if (ch >= 0x110000) | |
| 2311 { | |
| 2312 errmsg = "codepoint not in range(0x110000)"; | |
| 2313 startinpos = ((const char *)q)-starts; | |
| 2314 endinpos = startinpos+4; | |
| 2315 goto utf32Error; | |
| 2316 } | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2317 #ifndef Py_UNICODE_WIDE |
| 51216 | 2318 if (ch >= 0x10000) |
| 2319 { | |
| 2320 *p++ = 0xD800 | ((ch-0x10000) >> 10); | |
| 2321 *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); | |
| 2322 } | |
| 2323 else | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2324 #endif |
| 51216 | 2325 *p++ = ch; |
| 2326 q += 4; | |
| 2327 continue; | |
| 2328 utf32Error: | |
| 2329 outpos = p-PyUnicode_AS_UNICODE(unicode); | |
| 2330 if (unicode_decode_call_errorhandler( | |
| 2331 errors, &errorHandler, | |
| 2332 "utf32", errmsg, | |
|
56034
0ff37e200292
Merged revisions 74869 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55751
diff
changeset
|
2333 starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
| 51216 | 2334 &unicode, &outpos, &p)) |
| 2335 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
|
2336 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2337 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2338 if (byteorder) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2339 *byteorder = bo; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2340 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2341 if (consumed) |
| 51216 | 2342 *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
|
2343 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2344 /* Adjust length */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2345 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
|
2346 goto onError; |
|
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 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
|
2349 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
|
2350 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
|
2351 |
| 51216 | 2352 onError: |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2353 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
|
2354 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
|
2355 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
|
2356 return NULL; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2357 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2358 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2359 PyObject * |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2360 PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 51216 | 2361 Py_ssize_t size, |
| 2362 const char *errors, | |
| 2363 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
|
2364 { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2365 PyObject *v; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2366 unsigned char *p; |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2367 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
|
2368 #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
|
2369 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
|
2370 #else |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2371 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
|
2372 #endif |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2373 /* 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
|
2374 #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
|
2375 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
|
2376 #else |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2377 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
|
2378 #endif |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2379 |
| 51216 | 2380 #define STORECHAR(CH) \ |
| 2381 do { \ | |
| 2382 p[iorder[3]] = ((CH) >> 24) & 0xff; \ | |
| 2383 p[iorder[2]] = ((CH) >> 16) & 0xff; \ | |
| 2384 p[iorder[1]] = ((CH) >> 8) & 0xff; \ | |
| 2385 p[iorder[0]] = (CH) & 0xff; \ | |
| 2386 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
|
2387 } while(0) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2388 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2389 /* 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
|
2390 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
|
2391 #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
|
2392 for (i = pairs = 0; i < size-1; i++) |
| 51216 | 2393 if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2394 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) | |
| 2395 pairs++; | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2396 #endif |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2397 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
|
2398 bytesize = nsize * 4; |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2399 if (bytesize / 4 != nsize) |
| 51216 | 2400 return PyErr_NoMemory(); |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2401 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
|
2402 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
|
2403 return NULL; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2404 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47392
diff
changeset
|
2405 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
|
2406 if (byteorder == 0) |
| 51216 | 2407 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
|
2408 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
|
2409 return v; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2410 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2411 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
|
2412 /* force LE */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2413 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
|
2414 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
|
2415 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
|
2416 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
|
2417 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2418 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
|
2419 /* force BE */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2420 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
|
2421 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
|
2422 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
|
2423 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
|
2424 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2425 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2426 while (size-- > 0) { |
| 51216 | 2427 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
|
2428 #ifndef Py_UNICODE_WIDE |
| 51216 | 2429 if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2430 Py_UCS4 ch2 = *s; | |
| 2431 if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { | |
| 2432 ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; | |
| 2433 s++; | |
| 2434 size--; | |
| 2435 } | |
| 2436 } | |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2437 #endif |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2438 STORECHAR(ch); |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2439 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2440 return v; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2441 #undef STORECHAR |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2442 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2443 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2444 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
|
2445 { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2446 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
|
2447 PyErr_BadArgument(); |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2448 return NULL; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2449 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2450 return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 2451 PyUnicode_GET_SIZE(unicode), |
| 2452 NULL, | |
| 2453 0); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2454 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2455 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2456 /* --- UTF-16 Codec ------------------------------------------------------- */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2457 |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2458 PyObject * |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2459 PyUnicode_DecodeUTF16(const char *s, |
| 51216 | 2460 Py_ssize_t size, |
| 2461 const char *errors, | |
| 2462 int *byteorder) | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2463 { |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
33311
diff
changeset
|
2464 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
|
2465 } |
|
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
|
2466 |
|
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
|
2467 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
|
2468 PyUnicode_DecodeUTF16Stateful(const char *s, |
| 51216 | 2469 Py_ssize_t size, |
| 2470 const char *errors, | |
| 2471 int *byteorder, | |
| 2472 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
|
2473 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2474 const char *starts = s; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2475 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2476 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2477 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
|
2478 PyUnicodeObject *unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2479 Py_UNICODE *p; |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2480 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
|
2481 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
|
2482 const char *errmsg = ""; |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2483 /* 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
|
2484 #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
|
2485 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
|
2486 #else |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2487 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
|
2488 #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
|
2489 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
|
2490 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
|
2491 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2492 /* 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
|
2493 character count */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2494 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
|
2495 if (!unicode) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2496 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2497 if (size == 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2498 return (PyObject *)unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2499 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2500 /* 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
|
2501 p = unicode->str; |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2502 q = (unsigned char *)s; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2503 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
|
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) |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2506 bo = *byteorder; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2507 |
|
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
|
2508 /* 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
|
2509 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
|
2510 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
|
2511 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
|
2512 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
|
2513 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
|
2514 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
|
2515 #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 51216 | 2516 if (bom == 0xFEFF) { |
| 2517 q += 2; | |
| 2518 bo = -1; | |
| 2519 } | |
| 2520 else if (bom == 0xFFFE) { | |
| 2521 q += 2; | |
| 2522 bo = 1; | |
| 2523 } | |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
33311
diff
changeset
|
2524 #else |
| 51216 | 2525 if (bom == 0xFEFF) { |
| 2526 q += 2; | |
| 2527 bo = 1; | |
| 2528 } | |
| 2529 else if (bom == 0xFFFE) { | |
| 2530 q += 2; | |
| 2531 bo = -1; | |
| 2532 } | |
|
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
|
2533 #endif |
| 51216 | 2534 } |
|
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
|
2535 } |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2536 |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2537 if (bo == -1) { |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2538 /* force LE */ |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2539 ihi = 1; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2540 ilo = 0; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2541 } |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2542 else if (bo == 1) { |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2543 /* force BE */ |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2544 ihi = 0; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2545 ilo = 1; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2546 } |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2547 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2548 while (q < e) { |
| 51216 | 2549 Py_UNICODE ch; |
| 2550 /* remaining bytes at the end? (size should be even) */ | |
| 2551 if (e-q<2) { | |
| 2552 if (consumed) | |
| 2553 break; | |
| 2554 errmsg = "truncated data"; | |
| 2555 startinpos = ((const char *)q)-starts; | |
| 2556 endinpos = ((const char *)e)-starts; | |
| 2557 goto utf16Error; | |
| 2558 /* The remaining input chars are ignored if the callback | |
| 2559 chooses to skip the input */ | |
| 2560 } | |
| 2561 ch = (q[ihi] << 8) | q[ilo]; | |
| 2562 | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
2563 q += 2; |
| 51216 | 2564 |
| 2565 if (ch < 0xD800 || ch > 0xDFFF) { | |
| 2566 *p++ = ch; | |
| 2567 continue; | |
| 2568 } | |
| 2569 | |
| 2570 /* UTF-16 code pair: */ | |
| 2571 if (q >= e) { | |
| 2572 errmsg = "unexpected end of data"; | |
| 2573 startinpos = (((const char *)q)-2)-starts; | |
| 2574 endinpos = ((const char *)e)-starts; | |
| 2575 goto utf16Error; | |
| 2576 } | |
| 2577 if (0xD800 <= ch && ch <= 0xDBFF) { | |
| 2578 Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; | |
| 2579 q += 2; | |
| 2580 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
|
2581 #ifndef Py_UNICODE_WIDE |
| 51216 | 2582 *p++ = ch; |
| 2583 *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
|
2584 #else |
| 51216 | 2585 *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
|
2586 #endif |
| 51216 | 2587 continue; |
| 2588 } | |
| 2589 else { | |
|
18292
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2590 errmsg = "illegal UTF-16 surrogate"; |
| 51216 | 2591 startinpos = (((const char *)q)-4)-starts; |
| 2592 endinpos = startinpos+2; | |
| 2593 goto utf16Error; | |
| 2594 } | |
| 2595 | |
| 2596 } | |
| 2597 errmsg = "illegal encoding"; | |
| 2598 startinpos = (((const char *)q)-2)-starts; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
2599 endinpos = startinpos+2; |
| 51216 | 2600 /* Fall through to report the error */ |
| 2601 | |
| 2602 utf16Error: | |
| 2603 outpos = p-PyUnicode_AS_UNICODE(unicode); | |
| 2604 if (unicode_decode_call_errorhandler( | |
| 2605 errors, &errorHandler, | |
| 2606 "utf16", errmsg, | |
| 2607 starts, size, &startinpos, &endinpos, &exc, (const char **)&q, | |
| 2608 &unicode, &outpos, &p)) | |
| 2609 goto onError; | |
|
11968
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 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2612 if (byteorder) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2613 *byteorder = bo; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2614 |
|
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
|
2615 if (consumed) |
| 51216 | 2616 *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
|
2617 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2618 /* Adjust length */ |
|
30431
cd0c3f916b34
Change checks of PyUnicode_Resize() return value for clarity.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
30216
diff
changeset
|
2619 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
|
2620 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2621 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2622 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
|
2623 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
|
2624 return (PyObject *)unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2625 |
| 51216 | 2626 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2627 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
|
2628 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
|
2629 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
|
2630 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2631 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2632 |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2633 PyObject * |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2634 PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 51216 | 2635 Py_ssize_t size, |
| 2636 const char *errors, | |
| 2637 int byteorder) | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2638 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2639 PyObject *v; |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2640 unsigned char *p; |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
2641 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
|
2642 #ifdef Py_UNICODE_WIDE |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
2643 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
|
2644 #else |
|
4edf200a8118
SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31199
diff
changeset
|
2645 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
|
2646 #endif |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2647 /* 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
|
2648 #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
|
2649 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
|
2650 #else |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2651 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
|
2652 #endif |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2653 |
| 51216 | 2654 #define STORECHAR(CH) \ |
| 2655 do { \ | |
| 2656 p[ihi] = ((CH) >> 8) & 0xff; \ | |
| 2657 p[ilo] = (CH) & 0xff; \ | |
| 2658 p += 2; \ | |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2659 } 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
|
2660 |
|
31238
4edf200a8118
SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31199
diff
changeset
|
2661 #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
|
2662 for (i = pairs = 0; i < size; i++) |
| 51216 | 2663 if (s[i] >= 0x10000) |
| 2664 pairs++; | |
|
31238
4edf200a8118
SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31199
diff
changeset
|
2665 #endif |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
2666 /* 2 * (size + pairs + (byteorder == 0)) */ |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
2667 if (size > PY_SSIZE_T_MAX || |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2668 size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
| 51216 | 2669 return PyErr_NoMemory(); |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
2670 nsize = size + pairs + (byteorder == 0); |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
2671 bytesize = nsize * 2; |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
2672 if (bytesize / 2 != nsize) |
| 51216 | 2673 return PyErr_NoMemory(); |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
2674 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
|
2675 if (v == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2676 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2677 |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2678 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
|
2679 if (byteorder == 0) |
| 51216 | 2680 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
|
2681 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
|
2682 return v; |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2683 |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2684 if (byteorder == -1) { |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2685 /* force LE */ |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2686 ihi = 1; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2687 ilo = 0; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2688 } |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2689 else if (byteorder == 1) { |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2690 /* force BE */ |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2691 ihi = 0; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2692 ilo = 1; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2693 } |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2694 |
|
18292
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2695 while (size-- > 0) { |
| 51216 | 2696 Py_UNICODE ch = *s++; |
| 2697 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
|
2698 #ifdef Py_UNICODE_WIDE |
| 51216 | 2699 if (ch >= 0x10000) { |
| 2700 ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); | |
| 2701 ch = 0xD800 | ((ch-0x10000) >> 10); | |
| 2702 } | |
|
31238
4edf200a8118
SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31199
diff
changeset
|
2703 #endif |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2704 STORECHAR(ch); |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2705 if (ch2) |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2706 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
|
2707 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2708 return v; |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2709 #undef STORECHAR |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2710 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2711 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2712 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
|
2713 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2714 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
|
2715 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2716 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2717 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2718 return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 2719 PyUnicode_GET_SIZE(unicode), |
| 2720 NULL, | |
| 2721 0); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2722 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2723 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2724 /* --- Unicode Escape Codec ----------------------------------------------- */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2725 |
|
16200
3484670c37f2
Move uchhash functionality into unicodedata (after the recent
Fredrik Lundh <fredrik@pythonware.com>
parents:
16064
diff
changeset
|
2726 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
|
2727 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2728 PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
| 51216 | 2729 Py_ssize_t size, |
| 2730 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
|
2731 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2732 const char *starts = s; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2733 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2734 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2735 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
|
2736 int i; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2737 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
|
2738 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
|
2739 const char *end; |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2740 char* message; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2741 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
|
2742 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
|
2743 PyObject *exc = NULL; |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2744 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2745 /* 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
|
2746 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
|
2747 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
|
2748 (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
|
2749 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
|
2750 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
|
2751 if (v == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2752 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2753 if (size == 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2754 return (PyObject *)v; |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2755 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2756 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
|
2757 end = s + size; |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2758 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2759 while (s < end) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2760 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
|
2761 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
|
2762 int digits; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2763 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2764 /* 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
|
2765 if (*s != '\\') { |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2766 *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
|
2767 continue; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2768 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2769 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2770 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
|
2771 /* \ - Escapes */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2772 s++; |
|
43754
cfcfad87eb7d
Backport r58709 from trunk:
Georg Brandl <georg@python.org>
parents:
42401
diff
changeset
|
2773 c = *s++; |
|
cfcfad87eb7d
Backport r58709 from trunk:
Georg Brandl <georg@python.org>
parents:
42401
diff
changeset
|
2774 if (s > end) |
|
cfcfad87eb7d
Backport r58709 from trunk:
Georg Brandl <georg@python.org>
parents:
42401
diff
changeset
|
2775 c = '\0'; /* Invalid after \ */ |
|
cfcfad87eb7d
Backport r58709 from trunk:
Georg Brandl <georg@python.org>
parents:
42401
diff
changeset
|
2776 switch (c) { |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2777 |
| 51216 | 2778 /* \x escapes */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2779 case '\n': break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2780 case '\\': *p++ = '\\'; break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2781 case '\'': *p++ = '\''; break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2782 case '\"': *p++ = '\"'; break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2783 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
|
2784 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
|
2785 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
|
2786 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
|
2787 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
|
2788 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
|
2789 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
|
2790 |
| 51216 | 2791 /* \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
|
2792 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
|
2793 case '4': case '5': case '6': case '7': |
| 12544 | 2794 x = s[-1] - '0'; |
|
43754
cfcfad87eb7d
Backport r58709 from trunk:
Georg Brandl <georg@python.org>
parents:
42401
diff
changeset
|
2795 if (s < end && '0' <= *s && *s <= '7') { |
| 12544 | 2796 x = (x<<3) + *s++ - '0'; |
|
43754
cfcfad87eb7d
Backport r58709 from trunk:
Georg Brandl <georg@python.org>
parents:
42401
diff
changeset
|
2797 if (s < end && '0' <= *s && *s <= '7') |
| 12544 | 2798 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
|
2799 } |
| 12544 | 2800 *p++ = x; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2801 break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2802 |
| 51216 | 2803 /* hex escapes */ |
| 2804 /* \xXX */ | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2805 case 'x': |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2806 digits = 2; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2807 message = "truncated \\xXX escape"; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2808 goto hexescape; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2809 |
| 51216 | 2810 /* \uXXXX */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2811 case 'u': |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2812 digits = 4; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2813 message = "truncated \\uXXXX escape"; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2814 goto hexescape; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2815 |
| 51216 | 2816 /* \UXXXXXXXX */ |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2817 case 'U': |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2818 digits = 8; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2819 message = "truncated \\UXXXXXXXX escape"; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2820 hexescape: |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2821 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
|
2822 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
|
2823 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
|
2824 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
|
2825 if (unicode_decode_call_errorhandler( |
| 51216 | 2826 errors, &errorHandler, |
| 2827 "unicodeescape", "end of string in escape sequence", | |
| 2828 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 2829 &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
|
2830 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
|
2831 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
|
2832 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2833 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
|
2834 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
|
2835 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
|
2836 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
|
2837 if (unicode_decode_call_errorhandler( |
| 51216 | 2838 errors, &errorHandler, |
| 2839 "unicodeescape", message, | |
| 2840 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 2841 &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
|
2842 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
|
2843 goto nextByte; |
|
14388
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2844 } |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2845 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
|
2846 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
|
2847 chr += c - '0'; |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2848 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
|
2849 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
|
2850 else |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2851 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
|
2852 } |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2853 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
|
2854 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
|
2855 /* _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
|
2856 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
|
2857 break; |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2858 store: |
|
14388
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2859 /* 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
|
2860 if (chr <= 0xffff) |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2861 /* UCS-2 character */ |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2862 *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
|
2863 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
|
2864 /* UCS-4 character. Either store directly, or as |
| 22521 | 2865 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
|
2866 #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
|
2867 *p++ = chr; |
|
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2868 #else |
|
14388
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2869 chr -= 0x10000L; |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2870 *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
|
2871 *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
|
2872 #endif |
|
14388
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2873 } 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
|
2874 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
|
2875 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
|
2876 if (unicode_decode_call_errorhandler( |
| 51216 | 2877 errors, &errorHandler, |
| 2878 "unicodeescape", "illegal Unicode character", | |
| 2879 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 2880 &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
|
2881 goto onError; |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2882 } |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2883 break; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2884 |
| 51216 | 2885 /* \N{name} */ |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2886 case 'N': |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2887 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
|
2888 if (ucnhash_CAPI == NULL) { |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2889 /* load the unicode data module */ |
|
59998
d11fbc796499
Backported PyCapsule from 3.1, and converted most uses of
Larry Hastings <larry@hastings.org>
parents:
59895
diff
changeset
|
2890 ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(PyUnicodeData_CAPSULE_NAME, 1); |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2891 if (ucnhash_CAPI == NULL) |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2892 goto ucnhashError; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2893 } |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2894 if (*s == '{') { |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2895 const char *start = s+1; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2896 /* look for the closing brace */ |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2897 while (*s != '}' && s < end) |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2898 s++; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2899 if (s > start && s < end && *s == '}') { |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2900 /* found a name. look it up in the unicode database */ |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2901 message = "unknown Unicode character name"; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2902 s++; |
|
36924
48e0a5e39403
Update Unicode database to Unicode 4.1.
Martin v. Löwis <martin@v.loewis.de>
parents:
36882
diff
changeset
|
2903 if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2904 goto store; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2905 } |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2906 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2907 endinpos = s-starts; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2908 outpos = p-PyUnicode_AS_UNICODE(v); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2909 if (unicode_decode_call_errorhandler( |
| 51216 | 2910 errors, &errorHandler, |
| 2911 "unicodeescape", message, | |
| 2912 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 2913 &v, &outpos, &p)) | |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2914 goto onError; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2915 break; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2916 |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2917 default: |
| 22521 | 2918 if (s > end) { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2919 message = "\\ at end of string"; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2920 s--; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2921 endinpos = s-starts; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2922 outpos = p-PyUnicode_AS_UNICODE(v); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2923 if (unicode_decode_call_errorhandler( |
| 51216 | 2924 errors, &errorHandler, |
| 2925 "unicodeescape", message, | |
| 2926 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 2927 &v, &outpos, &p)) | |
| 22521 | 2928 goto onError; |
| 2929 } | |
| 2930 else { | |
| 2931 *p++ = '\\'; | |
| 2932 *p++ = (unsigned char)s[-1]; | |
| 2933 } | |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2934 break; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2935 } |
| 51216 | 2936 nextByte: |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2937 ; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2938 } |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
2939 if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2940 goto onError; |
|
30160
3329b058db20
Fix another refcounting leak (in PyUnicode_DecodeUnicodeEscape()).
Walter Dörwald <walter@livinglogic.de>
parents:
30149
diff
changeset
|
2941 Py_XDECREF(errorHandler); |
|
3329b058db20
Fix another refcounting leak (in PyUnicode_DecodeUnicodeEscape()).
Walter Dörwald <walter@livinglogic.de>
parents:
30149
diff
changeset
|
2942 Py_XDECREF(exc); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2943 return (PyObject *)v; |
| 22521 | 2944 |
| 51216 | 2945 ucnhashError: |
|
16200
3484670c37f2
Move uchhash functionality into unicodedata (after the recent
Fredrik Lundh <fredrik@pythonware.com>
parents:
16064
diff
changeset
|
2946 PyErr_SetString( |
|
3484670c37f2
Move uchhash functionality into unicodedata (after the recent
Fredrik Lundh <fredrik@pythonware.com>
parents:
16064
diff
changeset
|
2947 PyExc_UnicodeError, |
|
3484670c37f2
Move uchhash functionality into unicodedata (after the recent
Fredrik Lundh <fredrik@pythonware.com>
parents:
16064
diff
changeset
|
2948 "\\N escapes not supported (can't load unicodedata module)" |
|
3484670c37f2
Move uchhash functionality into unicodedata (after the recent
Fredrik Lundh <fredrik@pythonware.com>
parents:
16064
diff
changeset
|
2949 ); |
|
36877
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36675
diff
changeset
|
2950 Py_XDECREF(v); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2951 Py_XDECREF(errorHandler); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2952 Py_XDECREF(exc); |
|
16064
a97aa9238771
Better error message if ucnhash cannot be found (obscure attribute
Fredrik Lundh <fredrik@pythonware.com>
parents:
16029
diff
changeset
|
2953 return NULL; |
|
a97aa9238771
Better error message if ucnhash cannot be found (obscure attribute
Fredrik Lundh <fredrik@pythonware.com>
parents:
16029
diff
changeset
|
2954 |
| 51216 | 2955 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2956 Py_XDECREF(v); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2957 Py_XDECREF(errorHandler); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2958 Py_XDECREF(exc); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2959 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2960 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2961 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2962 /* Return a Unicode-Escape string version of the Unicode object. |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2963 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2964 If quotes is true, the string is enclosed in u"" or u'' quotes as |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2965 appropriate. |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2966 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2967 */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2968 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
2969 Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
| 51216 | 2970 Py_ssize_t size, |
| 2971 Py_UNICODE ch) | |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2972 { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2973 /* like wcschr, but doesn't stop at NULL characters */ |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2974 |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2975 while (size-- > 0) { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2976 if (*s == ch) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2977 return s; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2978 s++; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2979 } |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2980 |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2981 return NULL; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2982 } |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11997
diff
changeset
|
2983 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2984 static |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2985 PyObject *unicodeescape_string(const Py_UNICODE *s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2986 Py_ssize_t size, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2987 int quotes) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2988 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2989 PyObject *repr; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2990 char *p; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2991 |
|
16226
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
2992 static const char *hexdigit = "0123456789abcdef"; |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
2993 #ifdef Py_UNICODE_WIDE |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
2994 const Py_ssize_t expandsize = 10; |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
2995 #else |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
2996 const Py_ssize_t expandsize = 6; |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
2997 #endif |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2998 |
|
39621
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2999 /* XXX(nnorwitz): rather than over-allocating, it would be |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
3000 better to choose a different scheme. Perhaps scan the |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
3001 first N-chars of the string and allocate based on that size. |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
3002 */ |
|
39619
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
3003 /* Initial allocation is based on the longest-possible unichr |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
3004 escape. |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
3005 |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
3006 In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
3007 unichr, so in this case it's the longest unichr escape. In |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
3008 narrow (UTF-16) builds this is five chars per source unichr |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
3009 since there are two unichrs in the surrogate pair, so in narrow |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
3010 (UTF-16) builds it's not the longest unichr escape. |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
3011 |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
3012 In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
3013 so in the narrow (UTF-16) build case it's the longest unichr |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
3014 escape. |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
3015 */ |
|
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
3016 |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
3017 if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
| 51216 | 3018 return PyErr_NoMemory(); |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
3019 |
|
39619
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
3020 repr = PyString_FromStringAndSize(NULL, |
| 51216 | 3021 2 |
| 3022 + expandsize*size | |
| 3023 + 1); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3024 if (repr == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3025 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3026 |
|
18828
081d96fbcf43
Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents:
18774
diff
changeset
|
3027 p = PyString_AS_STRING(repr); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3028 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3029 if (quotes) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3030 *p++ = 'u'; |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
3031 *p++ = (findchar(s, size, '\'') && |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3032 !findchar(s, size, '"')) ? '"' : '\''; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3033 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3034 while (size-- > 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3035 Py_UNICODE ch = *s++; |
|
18828
081d96fbcf43
Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents:
18774
diff
changeset
|
3036 |
|
36056
f1ae11457d34
Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents:
35999
diff
changeset
|
3037 /* Escape quotes and backslashes */ |
|
f1ae11457d34
Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents:
35999
diff
changeset
|
3038 if ((quotes && |
| 51216 | 3039 ch == (Py_UNICODE) PyString_AS_STRING(repr)[1]) || ch == '\\') { |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3040 *p++ = '\\'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3041 *p++ = (char) ch; |
| 51216 | 3042 continue; |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
3043 } |
|
18828
081d96fbcf43
Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents:
18774
diff
changeset
|
3044 |
|
18773
743f8e1d953c
#ifdef out generation of \U escapes unless Py_UNICODE_WIDE. This
Guido van Rossum <guido@python.org>
parents:
18324
diff
changeset
|
3045 #ifdef Py_UNICODE_WIDE |
|
18292
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
3046 /* Map 21-bit characters to '\U00xxxxxx' */ |
|
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
3047 else if (ch >= 0x10000) { |
|
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
3048 *p++ = '\\'; |
|
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
3049 *p++ = 'U'; |
|
18774
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
3050 *p++ = hexdigit[(ch >> 28) & 0x0000000F]; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
3051 *p++ = hexdigit[(ch >> 24) & 0x0000000F]; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
3052 *p++ = hexdigit[(ch >> 20) & 0x0000000F]; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
3053 *p++ = hexdigit[(ch >> 16) & 0x0000000F]; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
3054 *p++ = hexdigit[(ch >> 12) & 0x0000000F]; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
3055 *p++ = hexdigit[(ch >> 8) & 0x0000000F]; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
3056 *p++ = hexdigit[(ch >> 4) & 0x0000000F]; |
|
18828
081d96fbcf43
Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents:
18774
diff
changeset
|
3057 *p++ = hexdigit[ch & 0x0000000F]; |
| 51216 | 3058 continue; |
|
18292
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
3059 } |
|
39619
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
3060 #else |
| 51216 | 3061 /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3062 else if (ch >= 0xD800 && ch < 0xDC00) { | |
| 3063 Py_UNICODE ch2; | |
| 3064 Py_UCS4 ucs; | |
| 3065 | |
| 3066 ch2 = *s++; | |
| 3067 size--; | |
| 3068 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { | |
| 3069 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; | |
| 3070 *p++ = '\\'; | |
| 3071 *p++ = 'U'; | |
| 3072 *p++ = hexdigit[(ucs >> 28) & 0x0000000F]; | |
| 3073 *p++ = hexdigit[(ucs >> 24) & 0x0000000F]; | |
| 3074 *p++ = hexdigit[(ucs >> 20) & 0x0000000F]; | |
| 3075 *p++ = hexdigit[(ucs >> 16) & 0x0000000F]; | |
| 3076 *p++ = hexdigit[(ucs >> 12) & 0x0000000F]; | |
| 3077 *p++ = hexdigit[(ucs >> 8) & 0x0000000F]; | |
| 3078 *p++ = hexdigit[(ucs >> 4) & 0x0000000F]; | |
| 3079 *p++ = hexdigit[ucs & 0x0000000F]; | |
| 3080 continue; | |
| 3081 } | |
| 3082 /* Fall through: isolated surrogates are copied as-is */ | |
| 3083 s--; | |
| 3084 size++; | |
| 3085 } | |
|
39619
02bbeec2714f
- Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
3086 #endif |
|
18774
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
3087 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3088 /* Map 16-bit characters to '\uxxxx' */ |
|
18774
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
3089 if (ch >= 256) { |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3090 *p++ = '\\'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3091 *p++ = 'u'; |
|
18774
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
3092 *p++ = hexdigit[(ch >> 12) & 0x000F]; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
3093 *p++ = hexdigit[(ch >> 8) & 0x000F]; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
3094 *p++ = hexdigit[(ch >> 4) & 0x000F]; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
3095 *p++ = hexdigit[ch & 0x000F]; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3096 } |
|
18828
081d96fbcf43
Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents:
18774
diff
changeset
|
3097 |
|
16226
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3098 /* Map special whitespace to '\t', \n', '\r' */ |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3099 else if (ch == '\t') { |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3100 *p++ = '\\'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3101 *p++ = 't'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3102 } |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3103 else if (ch == '\n') { |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3104 *p++ = '\\'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3105 *p++ = 'n'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3106 } |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3107 else if (ch == '\r') { |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3108 *p++ = '\\'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3109 *p++ = 'r'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3110 } |
|
18828
081d96fbcf43
Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents:
18774
diff
changeset
|
3111 |
|
16226
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3112 /* Map non-printable US ASCII to '\xhh' */ |
|
21067
c9c4c523dcbf
Fix for bug #485951: repr diff between string and unicode.
Marc-André Lemburg <mal@egenix.com>
parents:
21028
diff
changeset
|
3113 else if (ch < ' ' || ch >= 0x7F) { |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3114 *p++ = '\\'; |
|
16226
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3115 *p++ = 'x'; |
|
18774
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
3116 *p++ = hexdigit[(ch >> 4) & 0x000F]; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
3117 *p++ = hexdigit[ch & 0x000F]; |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
3118 } |
|
18828
081d96fbcf43
Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents:
18774
diff
changeset
|
3119 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3120 /* Copy everything else as-is */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3121 else |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3122 *p++ = (char) ch; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3123 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3124 if (quotes) |
|
18828
081d96fbcf43
Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents:
18774
diff
changeset
|
3125 *p++ = PyString_AS_STRING(repr)[1]; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3126 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3127 *p = '\0'; |
|
60203
86f699766016
more _PyString_Resize error checking
Benjamin Peterson <benjamin@python.org>
parents:
60074
diff
changeset
|
3128 if (_PyString_Resize(&repr, p - PyString_AS_STRING(repr))) |
|
86f699766016
more _PyString_Resize error checking
Benjamin Peterson <benjamin@python.org>
parents:
60074
diff
changeset
|
3129 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3130 return repr; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3131 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3132 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3133 PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 51216 | 3134 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
|
3135 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3136 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
|
3137 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3138 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3139 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
|
3140 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3141 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
|
3142 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3143 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3144 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3145 return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 3146 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
|
3147 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3148 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3149 /* --- 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
|
3150 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3151 PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
| 51216 | 3152 Py_ssize_t size, |
| 3153 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
|
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 const char *starts = s; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3156 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3157 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3158 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
|
3159 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
|
3160 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
|
3161 const char *end; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3162 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
|
3163 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
|
3164 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
|
3165 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3166 /* 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
|
3167 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
|
3168 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
|
3169 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
|
3170 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
|
3171 if (v == NULL) |
| 51216 | 3172 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3173 if (size == 0) |
| 51216 | 3174 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
|
3175 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
|
3176 end = s + size; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3177 while (s < end) { |
| 51216 | 3178 unsigned char c; |
| 3179 Py_UCS4 x; | |
| 3180 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
|
3181 int count; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3182 |
| 51216 | 3183 /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3184 if (*s != '\\') { | |
| 3185 *p++ = (unsigned char)*s++; | |
| 3186 continue; | |
| 3187 } | |
| 3188 startinpos = s-starts; | |
| 3189 | |
| 3190 /* \u-escapes are only interpreted iff the number of leading | |
| 3191 backslashes if odd */ | |
| 3192 bs = s; | |
| 3193 for (;s < end;) { | |
| 3194 if (*s != '\\') | |
| 3195 break; | |
| 3196 *p++ = (unsigned char)*s++; | |
| 3197 } | |
| 3198 if (((s - bs) & 1) == 0 || | |
| 3199 s >= end || | |
| 3200 (*s != 'u' && *s != 'U')) { | |
| 3201 continue; | |
| 3202 } | |
| 3203 p--; | |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3204 count = *s=='u' ? 4 : 8; |
| 51216 | 3205 s++; |
| 3206 | |
| 3207 /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ | |
| 3208 outpos = p-PyUnicode_AS_UNICODE(v); | |
| 3209 for (x = 0, i = 0; i < count; ++i, ++s) { | |
| 3210 c = (unsigned char)*s; | |
| 3211 if (!isxdigit(c)) { | |
| 3212 endinpos = s-starts; | |
| 3213 if (unicode_decode_call_errorhandler( | |
| 3214 errors, &errorHandler, | |
| 3215 "rawunicodeescape", "truncated \\uXXXX", | |
| 3216 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 3217 &v, &outpos, &p)) | |
| 3218 goto onError; | |
| 3219 goto nextByte; | |
| 3220 } | |
| 3221 x = (x<<4) & ~0xF; | |
| 3222 if (c >= '0' && c <= '9') | |
| 3223 x += c - '0'; | |
| 3224 else if (c >= 'a' && c <= 'f') | |
| 3225 x += 10 + c - 'a'; | |
| 3226 else | |
| 3227 x += 10 + c - 'A'; | |
| 3228 } | |
|
46060
42285d63755a
#1477: ur'\U0010FFFF' used to raise in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
45826
diff
changeset
|
3229 if (x <= 0xffff) |
| 51216 | 3230 /* UCS-2 character */ |
| 3231 *p++ = (Py_UNICODE) x; | |
|
46060
42285d63755a
#1477: ur'\U0010FFFF' used to raise in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
45826
diff
changeset
|
3232 else if (x <= 0x10ffff) { |
| 51216 | 3233 /* UCS-4 character. Either store directly, or as |
| 3234 surrogate pair. */ | |
|
46060
42285d63755a
#1477: ur'\U0010FFFF' used to raise in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
45826
diff
changeset
|
3235 #ifdef Py_UNICODE_WIDE |
| 51216 | 3236 *p++ = (Py_UNICODE) x; |
|
46060
42285d63755a
#1477: ur'\U0010FFFF' used to raise in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
45826
diff
changeset
|
3237 #else |
| 51216 | 3238 x -= 0x10000L; |
| 3239 *p++ = 0xD800 + (Py_UNICODE) (x >> 10); | |
| 3240 *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); | |
|
46060
42285d63755a
#1477: ur'\U0010FFFF' used to raise in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
45826
diff
changeset
|
3241 #endif |
|
42285d63755a
#1477: ur'\U0010FFFF' used to raise in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
45826
diff
changeset
|
3242 } else { |
|
42285d63755a
#1477: ur'\U0010FFFF' used to raise in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
45826
diff
changeset
|
3243 endinpos = s-starts; |
|
42285d63755a
#1477: ur'\U0010FFFF' used to raise in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
45826
diff
changeset
|
3244 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
|
3245 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
|
3246 errors, &errorHandler, |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3247 "rawunicodeescape", "\\Uxxxxxxxx out of range", |
| 51216 | 3248 starts, size, &startinpos, &endinpos, &exc, &s, |
| 3249 &v, &outpos, &p)) | |
| 3250 goto onError; | |
| 3251 } | |
| 3252 nextByte: | |
| 3253 ; | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3254 } |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
3255 if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 51216 | 3256 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
|
3257 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
|
3258 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
|
3259 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
|
3260 |
| 51216 | 3261 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3262 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
|
3263 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
|
3264 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
|
3265 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3266 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3267 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3268 PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 51216 | 3269 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
|
3270 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3271 PyObject *repr; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3272 char *p; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3273 char *q; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3274 |
|
16226
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3275 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
|
3276 #ifdef Py_UNICODE_WIDE |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
3277 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
|
3278 #else |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
3279 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
|
3280 #endif |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
3281 |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
3282 if (size > PY_SSIZE_T_MAX / expandsize) |
| 51216 | 3283 return PyErr_NoMemory(); |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
3284 |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
3285 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
|
3286 if (repr == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3287 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
|
3288 if (size == 0) |
| 51216 | 3289 return repr; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3290 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3291 p = q = PyString_AS_STRING(repr); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3292 while (size-- > 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3293 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
|
3294 #ifdef Py_UNICODE_WIDE |
| 51216 | 3295 /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3296 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
|
3297 *p++ = '\\'; |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3298 *p++ = 'U'; |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3299 *p++ = hexdigit[(ch >> 28) & 0xf]; |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3300 *p++ = hexdigit[(ch >> 24) & 0xf]; |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3301 *p++ = hexdigit[(ch >> 20) & 0xf]; |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3302 *p++ = hexdigit[(ch >> 16) & 0xf]; |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3303 *p++ = hexdigit[(ch >> 12) & 0xf]; |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3304 *p++ = hexdigit[(ch >> 8) & 0xf]; |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3305 *p++ = hexdigit[(ch >> 4) & 0xf]; |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3306 *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
|
3307 } |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3308 else |
|
46060
42285d63755a
#1477: ur'\U0010FFFF' used to raise in narrow unicode builds.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
45826
diff
changeset
|
3309 #else |
| 51216 | 3310 /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3311 if (ch >= 0xD800 && ch < 0xDC00) { | |
| 3312 Py_UNICODE ch2; | |
| 3313 Py_UCS4 ucs; | |
| 3314 | |
| 3315 ch2 = *s++; | |
| 3316 size--; | |
| 3317 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { | |
| 3318 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; | |
| 3319 *p++ = '\\'; | |
| 3320 *p++ = 'U'; | |
| 3321 *p++ = hexdigit[(ucs >> 28) & 0xf]; | |
| 3322 *p++ = hexdigit[(ucs >> 24) & 0xf]; | |
| 3323 *p++ = hexdigit[(ucs >> 20) & 0xf]; | |
| 3324 *p++ = hexdigit[(ucs >> 16) & 0xf]; | |
| 3325 *p++ = hexdigit[(ucs >> 12) & 0xf]; | |
| 3326 *p++ = hexdigit[(ucs >> 8) & 0xf]; | |
| 3327 *p++ = hexdigit[(ucs >> 4) & 0xf]; | |
| 3328 *p++ = hexdigit[ucs & 0xf]; | |
| 3329 continue; | |
| 3330 } | |
| 3331 /* Fall through: isolated surrogates are copied as-is */ | |
| 3332 s--; | |
| 3333 size++; | |
| 3334 } | |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3335 #endif |
| 51216 | 3336 /* Map 16-bit characters to '\uxxxx' */ |
| 3337 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
|
3338 *p++ = '\\'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3339 *p++ = 'u'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3340 *p++ = hexdigit[(ch >> 12) & 0xf]; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3341 *p++ = hexdigit[(ch >> 8) & 0xf]; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3342 *p++ = hexdigit[(ch >> 4) & 0xf]; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3343 *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
|
3344 } |
| 51216 | 3345 /* Copy everything else as-is */ |
| 3346 else | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3347 *p++ = (char) ch; |
|
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 *p = '\0'; |
|
60203
86f699766016
more _PyString_Resize error checking
Benjamin Peterson <benjamin@python.org>
parents:
60074
diff
changeset
|
3350 if (_PyString_Resize(&repr, p - q)) |
|
86f699766016
more _PyString_Resize error checking
Benjamin Peterson <benjamin@python.org>
parents:
60074
diff
changeset
|
3351 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3352 return repr; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3353 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3354 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3355 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
|
3356 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3357 if (!PyUnicode_Check(unicode)) { |
| 51216 | 3358 PyErr_BadArgument(); |
| 3359 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3360 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3361 return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 3362 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
|
3363 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3364 |
| 35485 | 3365 /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3366 | |
| 3367 PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, | |
| 51216 | 3368 Py_ssize_t size, |
| 3369 const char *errors) | |
| 35485 | 3370 { |
| 3371 const char *starts = s; | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3372 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3373 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3374 Py_ssize_t outpos; |
| 35485 | 3375 PyUnicodeObject *v; |
| 3376 Py_UNICODE *p; | |
| 3377 const char *end; | |
| 3378 const char *reason; | |
| 3379 PyObject *errorHandler = NULL; | |
| 3380 PyObject *exc = NULL; | |
| 3381 | |
|
36236
ecd423718a32
Fix icc warnings: remove (sometimes) unused variable conditionally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36203
diff
changeset
|
3382 #ifdef Py_UNICODE_WIDE |
|
ecd423718a32
Fix icc warnings: remove (sometimes) unused variable conditionally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36203
diff
changeset
|
3383 Py_UNICODE unimax = PyUnicode_GetMax(); |
|
ecd423718a32
Fix icc warnings: remove (sometimes) unused variable conditionally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36203
diff
changeset
|
3384 #endif |
|
ecd423718a32
Fix icc warnings: remove (sometimes) unused variable conditionally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36203
diff
changeset
|
3385 |
|
39994
798935e1295e
Forward-port of r52136: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39743
diff
changeset
|
3386 /* XXX overflow detection missing */ |
| 35485 | 3387 v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3388 if (v == NULL) | |
| 51216 | 3389 goto onError; |
| 35485 | 3390 if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 51216 | 3391 return (PyObject *)v; |
| 35485 | 3392 p = PyUnicode_AS_UNICODE(v); |
| 3393 end = s + size; | |
| 3394 | |
| 3395 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
|
3396 memcpy(p, s, sizeof(Py_UNICODE)); |
| 35485 | 3397 /* We have to sanity check the raw data, otherwise doom looms for |
| 3398 some malformed UCS-4 data. */ | |
| 3399 if ( | |
| 51216 | 3400 #ifdef Py_UNICODE_WIDE |
| 35485 | 3401 *p > unimax || *p < 0 || |
| 51216 | 3402 #endif |
| 35485 | 3403 end-s < Py_UNICODE_SIZE |
| 3404 ) | |
| 51216 | 3405 { |
| 35485 | 3406 startinpos = s - starts; |
| 3407 if (end-s < Py_UNICODE_SIZE) { | |
| 3408 endinpos = end-starts; | |
| 3409 reason = "truncated input"; | |
| 3410 } | |
| 3411 else { | |
| 3412 endinpos = s - starts + Py_UNICODE_SIZE; | |
| 3413 reason = "illegal code point (> 0x10FFFF)"; | |
| 3414 } | |
| 3415 outpos = p - PyUnicode_AS_UNICODE(v); | |
| 3416 if (unicode_decode_call_errorhandler( | |
| 3417 errors, &errorHandler, | |
| 3418 "unicode_internal", reason, | |
| 3419 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
|
3420 &v, &outpos, &p)) { |
| 35485 | 3421 goto onError; |
| 3422 } | |
| 3423 } | |
| 3424 else { | |
| 3425 p++; | |
| 3426 s += Py_UNICODE_SIZE; | |
| 3427 } | |
| 3428 } | |
| 3429 | |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
3430 if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 35485 | 3431 goto onError; |
| 3432 Py_XDECREF(errorHandler); | |
| 3433 Py_XDECREF(exc); | |
| 3434 return (PyObject *)v; | |
| 3435 | |
| 51216 | 3436 onError: |
| 35485 | 3437 Py_XDECREF(v); |
| 3438 Py_XDECREF(errorHandler); | |
| 3439 Py_XDECREF(exc); | |
| 3440 return NULL; | |
| 3441 } | |
| 3442 | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3443 /* --- Latin-1 Codec ------------------------------------------------------ */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3444 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3445 PyObject *PyUnicode_DecodeLatin1(const char *s, |
| 51216 | 3446 Py_ssize_t size, |
| 3447 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
|
3448 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3449 PyUnicodeObject *v; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3450 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
|
3451 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3452 /* 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
|
3453 if (size == 1) { |
| 51216 | 3454 Py_UNICODE r = *(unsigned char*)s; |
| 3455 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
|
3456 } |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
3457 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3458 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
|
3459 if (v == NULL) |
| 51216 | 3460 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3461 if (size == 0) |
| 51216 | 3462 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
|
3463 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
|
3464 while (size-- > 0) |
| 51216 | 3465 *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
|
3466 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
|
3467 |
| 51216 | 3468 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3469 Py_XDECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3470 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3471 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3472 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3473 /* 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
|
3474 static void make_encode_exception(PyObject **exceptionObject, |
| 51216 | 3475 const char *encoding, |
| 3476 const Py_UNICODE *unicode, Py_ssize_t size, | |
| 3477 Py_ssize_t startpos, Py_ssize_t endpos, | |
| 3478 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
|
3479 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3480 if (*exceptionObject == NULL) { |
| 51216 | 3481 *exceptionObject = PyUnicodeEncodeError_Create( |
| 3482 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
|
3483 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3484 else { |
| 51216 | 3485 if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 3486 goto onError; | |
| 3487 if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) | |
| 3488 goto onError; | |
| 3489 if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) | |
| 3490 goto onError; | |
| 3491 return; | |
| 3492 onError: | |
| 3493 Py_DECREF(*exceptionObject); | |
| 3494 *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
|
3495 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3496 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3497 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3498 /* 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
|
3499 static void raise_encode_exception(PyObject **exceptionObject, |
| 51216 | 3500 const char *encoding, |
| 3501 const Py_UNICODE *unicode, Py_ssize_t size, | |
| 3502 Py_ssize_t startpos, Py_ssize_t endpos, | |
| 3503 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
|
3504 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3505 make_encode_exception(exceptionObject, |
| 51216 | 3506 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
|
3507 if (*exceptionObject != NULL) |
| 51216 | 3508 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
|
3509 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3510 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3511 /* 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
|
3512 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
|
3513 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
|
3514 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
|
3515 static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 51216 | 3516 PyObject **errorHandler, |
| 3517 const char *encoding, const char *reason, | |
| 3518 const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, | |
| 3519 Py_ssize_t startpos, Py_ssize_t endpos, | |
| 3520 Py_ssize_t *newpos) | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3521 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3522 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
|
3523 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3524 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
|
3525 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
|
3526 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3527 if (*errorHandler == NULL) { |
| 51216 | 3528 *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
|
3529 if (*errorHandler == NULL) |
| 51216 | 3530 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
|
3531 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3532 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3533 make_encode_exception(exceptionObject, |
| 51216 | 3534 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
|
3535 if (*exceptionObject == NULL) |
| 51216 | 3536 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
|
3537 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3538 restuple = PyObject_CallFunctionObjArgs( |
| 51216 | 3539 *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
|
3540 if (restuple == NULL) |
| 51216 | 3541 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
|
3542 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
|
3543 PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 51216 | 3544 Py_DECREF(restuple); |
| 3545 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
|
3546 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3547 if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 51216 | 3548 &resunicode, newpos)) { |
| 3549 Py_DECREF(restuple); | |
| 3550 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
|
3551 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3552 if (*newpos<0) |
| 51216 | 3553 *newpos = size+*newpos; |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
3554 if (*newpos<0 || *newpos>size) { |
| 51216 | 3555 PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 3556 Py_DECREF(restuple); | |
| 3557 return NULL; | |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
3558 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3559 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
|
3560 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
|
3561 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
|
3562 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3563 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3564 static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
| 51216 | 3565 Py_ssize_t size, |
| 3566 const char *errors, | |
| 3567 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
|
3568 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3569 /* 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
|
3570 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
|
3571 /* 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
|
3572 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
|
3573 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
|
3574 /* 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
|
3575 /* 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
|
3576 /* 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
|
3577 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
|
3578 /* current output position */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3579 Py_ssize_t respos = 0; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3580 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
|
3581 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
|
3582 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
|
3583 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
|
3584 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
|
3585 /* 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
|
3586 * -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
|
3587 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
|
3588 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3589 /* 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
|
3590 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
|
3591 res = PyString_FromStringAndSize(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
|
3592 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
|
3593 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
|
3594 if (size == 0) |
| 51216 | 3595 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
|
3596 str = PyString_AS_STRING(res); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3597 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
|
3598 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3599 while (p<endp) { |
| 51216 | 3600 Py_UNICODE c = *p; |
| 3601 | |
| 3602 /* can we encode this? */ | |
| 3603 if (c<limit) { | |
| 3604 /* no overflow check, because we know that the space is enough */ | |
| 3605 *str++ = (char)c; | |
| 3606 ++p; | |
| 3607 } | |
| 3608 else { | |
| 3609 Py_ssize_t unicodepos = p-startp; | |
| 3610 Py_ssize_t requiredsize; | |
| 3611 PyObject *repunicode; | |
| 3612 Py_ssize_t repsize; | |
| 3613 Py_ssize_t newpos; | |
| 3614 Py_ssize_t respos; | |
| 3615 Py_UNICODE *uni2; | |
| 3616 /* startpos for collecting unencodable chars */ | |
| 3617 const Py_UNICODE *collstart = p; | |
| 3618 const Py_UNICODE *collend = p; | |
| 3619 /* find all unecodable characters */ | |
| 3620 while ((collend < endp) && ((*collend)>=limit)) | |
| 3621 ++collend; | |
| 3622 /* cache callback name lookup (if not done yet, i.e. it's the first error) */ | |
| 3623 if (known_errorHandler==-1) { | |
| 3624 if ((errors==NULL) || (!strcmp(errors, "strict"))) | |
| 3625 known_errorHandler = 1; | |
| 3626 else if (!strcmp(errors, "replace")) | |
| 3627 known_errorHandler = 2; | |
| 3628 else if (!strcmp(errors, "ignore")) | |
| 3629 known_errorHandler = 3; | |
| 3630 else if (!strcmp(errors, "xmlcharrefreplace")) | |
| 3631 known_errorHandler = 4; | |
| 3632 else | |
| 3633 known_errorHandler = 0; | |
| 3634 } | |
| 3635 switch (known_errorHandler) { | |
| 3636 case 1: /* strict */ | |
| 3637 raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); | |
| 3638 goto onError; | |
| 3639 case 2: /* replace */ | |
| 3640 while (collstart++<collend) | |
| 3641 *str++ = '?'; /* fall through */ | |
| 3642 case 3: /* ignore */ | |
| 3643 p = collend; | |
| 3644 break; | |
| 3645 case 4: /* xmlcharrefreplace */ | |
| 3646 respos = str-PyString_AS_STRING(res); | |
| 3647 /* determine replacement size (temporarily (mis)uses p) */ | |
| 3648 for (p = collstart, repsize = 0; p < collend; ++p) { | |
| 3649 if (*p<10) | |
| 3650 repsize += 2+1+1; | |
| 3651 else if (*p<100) | |
| 3652 repsize += 2+2+1; | |
| 3653 else if (*p<1000) | |
| 3654 repsize += 2+3+1; | |
| 3655 else if (*p<10000) | |
| 3656 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
|
3657 #ifndef Py_UNICODE_WIDE |
| 51216 | 3658 else |
| 3659 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
|
3660 #else |
| 51216 | 3661 else if (*p<100000) |
| 3662 repsize += 2+5+1; | |
| 3663 else if (*p<1000000) | |
| 3664 repsize += 2+6+1; | |
| 3665 else | |
| 3666 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
|
3667 #endif |
| 51216 | 3668 } |
| 3669 requiredsize = respos+repsize+(endp-collend); | |
| 3670 if (requiredsize > ressize) { | |
| 3671 if (requiredsize<2*ressize) | |
| 3672 requiredsize = 2*ressize; | |
| 3673 if (_PyString_Resize(&res, requiredsize)) | |
| 3674 goto onError; | |
| 3675 str = PyString_AS_STRING(res) + respos; | |
| 3676 ressize = requiredsize; | |
| 3677 } | |
| 3678 /* generate replacement (temporarily (mis)uses p) */ | |
| 3679 for (p = collstart; p < collend; ++p) { | |
| 3680 str += sprintf(str, "&#%d;", (int)*p); | |
| 3681 } | |
| 3682 p = collend; | |
| 3683 break; | |
| 3684 default: | |
| 3685 repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, | |
| 3686 encoding, reason, startp, size, &exc, | |
| 3687 collstart-startp, collend-startp, &newpos); | |
| 3688 if (repunicode == NULL) | |
| 3689 goto onError; | |
|
51213
cd5db43dbb65
fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents:
51212
diff
changeset
|
3690 /* need more space? (at least enough for what we have+the |
|
cd5db43dbb65
fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents:
51212
diff
changeset
|
3691 replacement+the rest of the string, so we won't have to |
|
cd5db43dbb65
fix indentation; looks like all I managed to do the first time is make things uglier
Benjamin Peterson <benjamin@python.org>
parents:
51212
diff
changeset
|
3692 check space for encodable characters) */ |
| 51216 | 3693 respos = str-PyString_AS_STRING(res); |
| 3694 repsize = PyUnicode_GET_SIZE(repunicode); | |
| 3695 requiredsize = respos+repsize+(endp-collend); | |
| 3696 if (requiredsize > ressize) { | |
| 3697 if (requiredsize<2*ressize) | |
| 3698 requiredsize = 2*ressize; | |
| 3699 if (_PyString_Resize(&res, requiredsize)) { | |
| 3700 Py_DECREF(repunicode); | |
| 3701 goto onError; | |
| 3702 } | |
| 3703 str = PyString_AS_STRING(res) + respos; | |
| 3704 ressize = requiredsize; | |
| 3705 } | |
| 3706 /* check if there is anything unencodable in the replacement | |
| 3707 and copy it to the output */ | |
| 3708 for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { | |
| 3709 c = *uni2; | |
| 3710 if (c >= limit) { | |
| 3711 raise_encode_exception(&exc, encoding, startp, size, | |
| 3712 unicodepos, unicodepos+1, reason); | |
| 3713 Py_DECREF(repunicode); | |
| 3714 goto onError; | |
| 3715 } | |
| 3716 *str = (char)c; | |
| 3717 } | |
| 3718 p = startp + newpos; | |
| 3719 Py_DECREF(repunicode); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
3720 } |
| 51216 | 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 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3723 /* 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
|
3724 respos = str-PyString_AS_STRING(res); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3725 if (respos<ressize) |
| 51216 | 3726 /* If this falls res will be NULL */ |
| 3727 _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
|
3728 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
|
3729 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
|
3730 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
|
3731 |
| 51216 | 3732 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
|
3733 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
|
3734 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
|
3735 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
|
3736 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3737 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3738 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3739 PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
| 51216 | 3740 Py_ssize_t size, |
| 3741 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
|
3742 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3743 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
|
3744 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3745 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3746 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
|
3747 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3748 if (!PyUnicode_Check(unicode)) { |
| 51216 | 3749 PyErr_BadArgument(); |
| 3750 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3751 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3752 return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 3753 PyUnicode_GET_SIZE(unicode), |
| 3754 NULL); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3755 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3756 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3757 /* --- 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
|
3758 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3759 PyObject *PyUnicode_DecodeASCII(const char *s, |
| 51216 | 3760 Py_ssize_t size, |
| 3761 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
|
3762 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3763 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
|
3764 PyUnicodeObject *v; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3765 Py_UNICODE *p; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3766 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3767 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3768 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
|
3769 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
|
3770 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
|
3771 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
|
3772 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3773 /* 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
|
3774 if (size == 1 && *(unsigned char*)s < 128) { |
| 51216 | 3775 Py_UNICODE r = *(unsigned char*)s; |
| 3776 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
|
3777 } |
|
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
|
3778 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3779 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
|
3780 if (v == NULL) |
| 51216 | 3781 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3782 if (size == 0) |
| 51216 | 3783 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
|
3784 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
|
3785 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
|
3786 while (s < e) { |
| 51216 | 3787 register unsigned char c = (unsigned char)*s; |
| 3788 if (c < 128) { | |
| 3789 *p++ = c; | |
| 3790 ++s; | |
| 3791 } | |
| 3792 else { | |
| 3793 startinpos = s-starts; | |
| 3794 endinpos = startinpos + 1; | |
| 3795 outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); | |
| 3796 if (unicode_decode_call_errorhandler( | |
| 3797 errors, &errorHandler, | |
| 3798 "ascii", "ordinal not in range(128)", | |
| 3799 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 3800 &v, &outpos, &p)) | |
| 3801 goto onError; | |
| 3802 } | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3803 } |
| 12329 | 3804 if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) |
| 51216 | 3805 if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 3806 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
|
3807 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
|
3808 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
|
3809 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
|
3810 |
| 51216 | 3811 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3812 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
|
3813 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
|
3814 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
|
3815 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3816 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3817 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3818 PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 51216 | 3819 Py_ssize_t size, |
| 3820 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
|
3821 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3822 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
|
3823 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3824 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3825 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
|
3826 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3827 if (!PyUnicode_Check(unicode)) { |
| 51216 | 3828 PyErr_BadArgument(); |
| 3829 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3830 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3831 return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 3832 PyUnicode_GET_SIZE(unicode), |
| 3833 NULL); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3834 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3835 |
|
24071
8142f8d668a0
Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents:
23837
diff
changeset
|
3836 #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 12193 | 3837 |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3838 /* --- MBCS codecs for Windows -------------------------------------------- */ |
| 12193 | 3839 |
|
52254
b8934dd9267d
Merged revisions 70499 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
51216
diff
changeset
|
3840 #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
|
3841 #define NEED_RETRY |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3842 #endif |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3843 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3844 /* 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
|
3845 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
|
3846 b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
| 51216 | 3847 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
|
3848 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3849 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
|
3850 { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3851 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
|
3852 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3853 if (IsDBCSLeadByte(*curr)) { |
| 51216 | 3854 const char *prev = CharPrev(s, curr); |
| 3855 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
|
3856 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3857 return 0; |
|
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 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3860 /* |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3861 * 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
|
3862 * 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
|
3863 */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3864 static int decode_mbcs(PyUnicodeObject **v, |
| 51216 | 3865 const char *s, /* MBCS string */ |
| 3866 int size, /* sizeof MBCS string */ | |
| 3867 int final) | |
|
38817
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 Py_UNICODE *p; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3870 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
|
3871 int usize = 0; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3872 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3873 assert(size >= 0); |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3874 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3875 /* 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
|
3876 if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
| 51216 | 3877 --size; |
|
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 /* 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
|
3880 if (size > 0) { |
| 51216 | 3881 usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 3882 if (usize == 0) { | |
| 3883 PyErr_SetFromWindowsErrWithFilename(0, NULL); | |
| 3884 return -1; | |
| 3885 } | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3886 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3887 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3888 if (*v == NULL) { |
| 51216 | 3889 /* Create unicode object */ |
| 3890 *v = _PyUnicode_New(usize); | |
| 3891 if (*v == NULL) | |
| 3892 return -1; | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3893 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3894 else { |
| 51216 | 3895 /* Extend unicode object */ |
| 3896 n = PyUnicode_GET_SIZE(*v); | |
| 3897 if (_PyUnicode_Resize(v, n + usize) < 0) | |
| 3898 return -1; | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3899 } |
|
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 /* Do the conversion */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3902 if (size > 0) { |
| 51216 | 3903 p = PyUnicode_AS_UNICODE(*v) + n; |
| 3904 if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { | |
| 3905 PyErr_SetFromWindowsErrWithFilename(0, NULL); | |
| 3906 return -1; | |
| 3907 } | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3908 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3909 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3910 return size; |
|
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 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3913 PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
| 51216 | 3914 Py_ssize_t size, |
| 3915 const char *errors, | |
| 3916 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
|
3917 { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3918 PyUnicodeObject *v = NULL; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3919 int done; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3920 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3921 if (consumed) |
| 51216 | 3922 *consumed = 0; |
|
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 #ifdef NEED_RETRY |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3925 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 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
|
3928 else |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3929 #endif |
| 51216 | 3930 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
|
3931 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3932 if (done < 0) { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3933 Py_XDECREF(v); |
| 51216 | 3934 return NULL; |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3935 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3936 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3937 if (consumed) |
| 51216 | 3938 *consumed += done; |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3939 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3940 #ifdef NEED_RETRY |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3941 if (size > INT_MAX) { |
| 51216 | 3942 s += done; |
| 3943 size -= done; | |
| 3944 goto retry; | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3945 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3946 #endif |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3947 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3948 return (PyObject *)v; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3949 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3950 |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3951 PyObject *PyUnicode_DecodeMBCS(const char *s, |
| 51216 | 3952 Py_ssize_t size, |
| 3953 const char *errors) | |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3954 { |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3955 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
|
3956 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3957 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3958 /* |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3959 * 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
|
3960 * 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
|
3961 */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3962 static int encode_mbcs(PyObject **repr, |
| 51216 | 3963 const Py_UNICODE *p, /* unicode */ |
| 3964 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
|
3965 { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3966 int mbcssize = 0; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3967 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
|
3968 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3969 assert(size >= 0); |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3970 |
|
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3971 /* 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
|
3972 if (size > 0) { |
| 51216 | 3973 mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 3974 if (mbcssize == 0) { | |
| 3975 PyErr_SetFromWindowsErrWithFilename(0, NULL); | |
| 3976 return -1; | |
| 3977 } | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3978 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3979 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3980 if (*repr == NULL) { |
| 51216 | 3981 /* Create string object */ |
| 3982 *repr = PyString_FromStringAndSize(NULL, mbcssize); | |
| 3983 if (*repr == NULL) | |
| 3984 return -1; | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3985 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3986 else { |
| 51216 | 3987 /* Extend string object */ |
| 3988 n = PyString_Size(*repr); | |
| 3989 if (_PyString_Resize(repr, n + mbcssize) < 0) | |
| 3990 return -1; | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3991 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3992 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3993 /* Do the conversion */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3994 if (size > 0) { |
| 51216 | 3995 char *s = PyString_AS_STRING(*repr) + n; |
| 3996 if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { | |
| 3997 PyErr_SetFromWindowsErrWithFilename(0, NULL); | |
| 3998 return -1; | |
| 3999 } | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4000 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4001 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4002 return 0; |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
4003 } |
|
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
4004 |
|
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
4005 PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 51216 | 4006 Py_ssize_t size, |
| 4007 const char *errors) | |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
4008 { |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4009 PyObject *repr = NULL; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4010 int ret; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4011 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4012 #ifdef NEED_RETRY |
| 51216 | 4013 retry: |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4014 if (size > INT_MAX) |
| 51216 | 4015 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
|
4016 else |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4017 #endif |
| 51216 | 4018 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
|
4019 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4020 if (ret < 0) { |
| 51216 | 4021 Py_XDECREF(repr); |
| 4022 return NULL; | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4023 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4024 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4025 #ifdef NEED_RETRY |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4026 if (size > INT_MAX) { |
| 51216 | 4027 p += INT_MAX; |
| 4028 size -= INT_MAX; | |
| 4029 goto retry; | |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4030 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4031 #endif |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4032 |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
4033 return repr; |
|
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
4034 } |
| 12193 | 4035 |
|
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
|
4036 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
|
4037 { |
|
a4fead0e24f8
Support 'mbcs' as a 'built-in' encoding, so the C API can use it without
Mark Hammond <mhammond@skippinet.com.au>
parents:
29641
diff
changeset
|
4038 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
|
4039 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
|
4040 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
|
4041 } |
|
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
|
4042 return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 4043 PyUnicode_GET_SIZE(unicode), |
| 4044 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
|
4045 } |
|
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
|
4046 |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4047 #undef NEED_RETRY |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
4048 |
|
24071
8142f8d668a0
Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents:
23837
diff
changeset
|
4049 #endif /* MS_WINDOWS */ |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
4050 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4051 /* --- Character Mapping Codec -------------------------------------------- */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4052 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4053 PyObject *PyUnicode_DecodeCharmap(const char *s, |
| 51216 | 4054 Py_ssize_t size, |
| 4055 PyObject *mapping, | |
| 4056 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
|
4057 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4058 const char *starts = s; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4059 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4060 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4061 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
|
4062 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
|
4063 PyUnicodeObject *v; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4064 Py_UNICODE *p; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4065 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
|
4066 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
|
4067 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
|
4068 Py_UNICODE *mapstring = NULL; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4069 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
|
4070 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4071 /* 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
|
4072 if (mapping == NULL) |
| 51216 | 4073 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
|
4074 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4075 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
|
4076 if (v == NULL) |
| 51216 | 4077 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4078 if (size == 0) |
| 51216 | 4079 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
|
4080 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
|
4081 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
|
4082 if (PyUnicode_CheckExact(mapping)) { |
| 51216 | 4083 mapstring = PyUnicode_AS_UNICODE(mapping); |
| 4084 maplen = PyUnicode_GET_SIZE(mapping); | |
| 4085 while (s < e) { | |
| 4086 unsigned char ch = *s; | |
| 4087 Py_UNICODE x = 0xfffe; /* illegal value */ | |
| 4088 | |
| 4089 if (ch < maplen) | |
| 4090 x = mapstring[ch]; | |
| 4091 | |
| 4092 if (x == 0xfffe) { | |
| 4093 /* undefined mapping */ | |
| 4094 outpos = p-PyUnicode_AS_UNICODE(v); | |
| 4095 startinpos = s-starts; | |
| 4096 endinpos = startinpos+1; | |
| 4097 if (unicode_decode_call_errorhandler( | |
| 4098 errors, &errorHandler, | |
| 4099 "charmap", "character maps to <undefined>", | |
| 4100 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 4101 &v, &outpos, &p)) { | |
| 4102 goto onError; | |
| 4103 } | |
| 4104 continue; | |
| 4105 } | |
| 4106 *p++ = x; | |
| 4107 ++s; | |
| 4108 } | |
|
35702
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
4109 } |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
4110 else { |
| 51216 | 4111 while (s < e) { |
| 4112 unsigned char ch = *s; | |
| 4113 PyObject *w, *x; | |
| 4114 | |
| 4115 /* Get mapping (char ordinal -> integer, Unicode char or None) */ | |
| 4116 w = PyInt_FromLong((long)ch); | |
| 4117 if (w == NULL) | |
| 4118 goto onError; | |
| 4119 x = PyObject_GetItem(mapping, w); | |
| 4120 Py_DECREF(w); | |
| 4121 if (x == NULL) { | |
| 4122 if (PyErr_ExceptionMatches(PyExc_LookupError)) { | |
| 4123 /* No mapping found means: mapping is undefined. */ | |
| 4124 PyErr_Clear(); | |
| 4125 x = Py_None; | |
| 4126 Py_INCREF(x); | |
| 4127 } else | |
| 4128 goto onError; | |
| 4129 } | |
| 4130 | |
| 4131 /* Apply mapping */ | |
| 4132 if (PyInt_Check(x)) { | |
| 4133 long value = PyInt_AS_LONG(x); | |
| 4134 if (value < 0 || value > 65535) { | |
| 4135 PyErr_SetString(PyExc_TypeError, | |
| 4136 "character mapping must be in range(65536)"); | |
| 4137 Py_DECREF(x); | |
| 4138 goto onError; | |
| 4139 } | |
| 4140 *p++ = (Py_UNICODE)value; | |
| 4141 } | |
| 4142 else if (x == Py_None) { | |
| 4143 /* undefined mapping */ | |
| 4144 outpos = p-PyUnicode_AS_UNICODE(v); | |
| 4145 startinpos = s-starts; | |
| 4146 endinpos = startinpos+1; | |
| 4147 if (unicode_decode_call_errorhandler( | |
| 4148 errors, &errorHandler, | |
| 4149 "charmap", "character maps to <undefined>", | |
| 4150 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 4151 &v, &outpos, &p)) { | |
| 4152 Py_DECREF(x); | |
| 4153 goto onError; | |
| 4154 } | |
| 4155 Py_DECREF(x); | |
| 4156 continue; | |
| 4157 } | |
| 4158 else if (PyUnicode_Check(x)) { | |
| 4159 Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); | |
| 4160 | |
| 4161 if (targetsize == 1) | |
| 4162 /* 1-1 mapping */ | |
| 4163 *p++ = *PyUnicode_AS_UNICODE(x); | |
| 4164 | |
| 4165 else if (targetsize > 1) { | |
| 4166 /* 1-n mapping */ | |
| 4167 if (targetsize > extrachars) { | |
| 4168 /* resize first */ | |
| 4169 Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); | |
| 4170 Py_ssize_t needed = (targetsize - extrachars) + \ | |
| 4171 (targetsize << 2); | |
| 4172 extrachars += needed; | |
| 4173 /* XXX overflow detection missing */ | |
| 4174 if (_PyUnicode_Resize(&v, | |
| 4175 PyUnicode_GET_SIZE(v) + needed) < 0) { | |
| 4176 Py_DECREF(x); | |
| 4177 goto onError; | |
| 4178 } | |
| 4179 p = PyUnicode_AS_UNICODE(v) + oldpos; | |
| 4180 } | |
| 4181 Py_UNICODE_COPY(p, | |
| 4182 PyUnicode_AS_UNICODE(x), | |
| 4183 targetsize); | |
| 4184 p += targetsize; | |
| 4185 extrachars -= targetsize; | |
| 4186 } | |
| 4187 /* 1-0 mapping: skip the character */ | |
| 4188 } | |
| 4189 else { | |
| 4190 /* wrong return value */ | |
| 4191 PyErr_SetString(PyExc_TypeError, | |
| 4192 "character mapping must return integer, None or unicode"); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4193 Py_DECREF(x); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4194 goto onError; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4195 } |
| 51216 | 4196 Py_DECREF(x); |
| 4197 ++s; | |
| 4198 } | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4199 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4200 if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
| 51216 | 4201 if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4202 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
|
4203 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
|
4204 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
|
4205 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
|
4206 |
| 51216 | 4207 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
|
4208 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
|
4209 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
|
4210 Py_XDECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4211 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4212 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4213 |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4214 /* Charmap encoding: the lookup table */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4215 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4216 struct encoding_map{ |
| 51216 | 4217 PyObject_HEAD |
| 4218 unsigned char level1[32]; | |
| 4219 int count2, count3; | |
| 4220 unsigned char level23[1]; | |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4221 }; |
|
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 static PyObject* |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4224 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
|
4225 { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4226 struct encoding_map *map = (struct encoding_map*)obj; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4227 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
|
4228 128*map->count3); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4229 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4230 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4231 static PyMethodDef encoding_map_methods[] = { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4232 {"size", encoding_map_size, METH_NOARGS, |
| 51216 | 4233 PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4234 { 0 } | |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4235 }; |
|
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 static void |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4238 encoding_map_dealloc(PyObject* o) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4239 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4240 PyObject_FREE(o); |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4241 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4242 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4243 static PyTypeObject EncodingMapType = { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4244 PyVarObject_HEAD_INIT(NULL, 0) |
| 51216 | 4245 "EncodingMap", /*tp_name*/ |
| 4246 sizeof(struct encoding_map), /*tp_basicsize*/ | |
| 4247 0, /*tp_itemsize*/ | |
| 4248 /* methods */ | |
| 4249 encoding_map_dealloc, /*tp_dealloc*/ | |
| 4250 0, /*tp_print*/ | |
| 4251 0, /*tp_getattr*/ | |
| 4252 0, /*tp_setattr*/ | |
| 4253 0, /*tp_compare*/ | |
| 4254 0, /*tp_repr*/ | |
| 4255 0, /*tp_as_number*/ | |
| 4256 0, /*tp_as_sequence*/ | |
| 4257 0, /*tp_as_mapping*/ | |
| 4258 0, /*tp_hash*/ | |
| 4259 0, /*tp_call*/ | |
| 4260 0, /*tp_str*/ | |
| 4261 0, /*tp_getattro*/ | |
| 4262 0, /*tp_setattro*/ | |
| 4263 0, /*tp_as_buffer*/ | |
| 4264 Py_TPFLAGS_DEFAULT, /*tp_flags*/ | |
| 4265 0, /*tp_doc*/ | |
| 4266 0, /*tp_traverse*/ | |
| 4267 0, /*tp_clear*/ | |
| 4268 0, /*tp_richcompare*/ | |
| 4269 0, /*tp_weaklistoffset*/ | |
| 4270 0, /*tp_iter*/ | |
| 4271 0, /*tp_iternext*/ | |
| 4272 encoding_map_methods, /*tp_methods*/ | |
| 4273 0, /*tp_members*/ | |
| 4274 0, /*tp_getset*/ | |
| 4275 0, /*tp_base*/ | |
| 4276 0, /*tp_dict*/ | |
| 4277 0, /*tp_descr_get*/ | |
| 4278 0, /*tp_descr_set*/ | |
| 4279 0, /*tp_dictoffset*/ | |
| 4280 0, /*tp_init*/ | |
| 4281 0, /*tp_alloc*/ | |
| 4282 0, /*tp_new*/ | |
| 4283 0, /*tp_free*/ | |
| 4284 0, /*tp_is_gc*/ | |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4285 }; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4286 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4287 PyObject* |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4288 PyUnicode_BuildEncodingMap(PyObject* string) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4289 { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4290 Py_UNICODE *decode; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4291 PyObject *result; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4292 struct encoding_map *mresult; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4293 int i; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4294 int need_dict = 0; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4295 unsigned char level1[32]; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4296 unsigned char level2[512]; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4297 unsigned char *mlevel1, *mlevel2, *mlevel3; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4298 int count2 = 0, count3 = 0; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4299 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4300 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
|
4301 PyErr_BadArgument(); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4302 return NULL; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4303 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4304 decode = PyUnicode_AS_UNICODE(string); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4305 memset(level1, 0xFF, sizeof level1); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4306 memset(level2, 0xFF, sizeof level2); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4307 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4308 /* 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
|
4309 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
|
4310 a mapping dictionary. */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4311 if (decode[0] != 0) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4312 need_dict = 1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4313 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
|
4314 int l1, l2; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4315 if (decode[i] == 0 |
| 51216 | 4316 #ifdef Py_UNICODE_WIDE |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4317 || decode[i] > 0xFFFF |
| 51216 | 4318 #endif |
| 4319 ) { | |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4320 need_dict = 1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4321 break; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4322 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4323 if (decode[i] == 0xFFFE) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4324 /* unmapped character */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4325 continue; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4326 l1 = decode[i] >> 11; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4327 l2 = decode[i] >> 7; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4328 if (level1[l1] == 0xFF) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4329 level1[l1] = count2++; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4330 if (level2[l2] == 0xFF) |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4331 level2[l2] = count3++; |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4332 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4333 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4334 if (count2 >= 0xFF || count3 >= 0xFF) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4335 need_dict = 1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4336 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4337 if (need_dict) { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4338 PyObject *result = PyDict_New(); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4339 PyObject *key, *value; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4340 if (!result) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4341 return NULL; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4342 for (i = 0; i < 256; i++) { |
|
61204
55a593cc3280
Remove an unneeded variable and assignment.
Brett Cannon <bcannon@gmail.com>
parents:
60203
diff
changeset
|
4343 value = NULL; |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4344 key = PyInt_FromLong(decode[i]); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4345 value = PyInt_FromLong(i); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4346 if (!key || !value) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4347 goto failed1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4348 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
|
4349 goto failed1; |
|
38587
21969b71c36d
Repair refleaks in unicodeobject.
Georg Brandl <georg@python.org>
parents:
38586
diff
changeset
|
4350 Py_DECREF(key); |
|
21969b71c36d
Repair refleaks in unicodeobject.
Georg Brandl <georg@python.org>
parents:
38586
diff
changeset
|
4351 Py_DECREF(value); |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4352 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4353 return result; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4354 failed1: |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4355 Py_XDECREF(key); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4356 Py_XDECREF(value); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4357 Py_DECREF(result); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4358 return NULL; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4359 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4360 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4361 /* Create a three-level trie */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4362 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
|
4363 16*count2 + 128*count3 - 1); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4364 if (!result) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4365 return PyErr_NoMemory(); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4366 PyObject_Init(result, &EncodingMapType); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4367 mresult = (struct encoding_map*)result; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4368 mresult->count2 = count2; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4369 mresult->count3 = count3; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4370 mlevel1 = mresult->level1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4371 mlevel2 = mresult->level23; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4372 mlevel3 = mresult->level23 + 16*count2; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4373 memcpy(mlevel1, level1, 32); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4374 memset(mlevel2, 0xFF, 16*count2); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4375 memset(mlevel3, 0, 128*count3); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4376 count3 = 0; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4377 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
|
4378 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
|
4379 if (decode[i] == 0xFFFE) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4380 /* unmapped character */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4381 continue; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4382 o1 = decode[i]>>11; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4383 o2 = (decode[i]>>7) & 0xF; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4384 i2 = 16*mlevel1[o1] + o2; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4385 if (mlevel2[i2] == 0xFF) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4386 mlevel2[i2] = count3++; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4387 o3 = decode[i] & 0x7F; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4388 i3 = 128*mlevel2[i2] + o3; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4389 mlevel3[i3] = i; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4390 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4391 return result; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4392 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4393 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4394 static int |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4395 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
|
4396 { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4397 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
|
4398 int l1 = c>>11; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4399 int l2 = (c>>7) & 0xF; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4400 int l3 = c & 0x7F; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4401 int i; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4402 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4403 #ifdef Py_UNICODE_WIDE |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4404 if (c > 0xFFFF) { |
| 51216 | 4405 return -1; |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4406 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4407 #endif |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4408 if (c == 0) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4409 return 0; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4410 /* level 1*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4411 i = map->level1[l1]; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4412 if (i == 0xFF) { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4413 return -1; |
|
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 /* level 2*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4416 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
|
4417 if (i == 0xFF) { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4418 return -1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4419 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4420 /* level 3 */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4421 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
|
4422 if (i == 0) { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4423 return -1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4424 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4425 return i; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4426 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4427 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4428 /* 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
|
4429 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
|
4430 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
|
4431 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
|
4432 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4433 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
|
4434 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
|
4435 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4436 if (w == NULL) |
| 51216 | 4437 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
|
4438 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
|
4439 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
|
4440 if (x == NULL) { |
| 51216 | 4441 if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4442 /* No mapping found means: mapping is undefined. */ | |
| 4443 PyErr_Clear(); | |
| 4444 x = Py_None; | |
| 4445 Py_INCREF(x); | |
| 4446 return x; | |
| 4447 } else | |
| 4448 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
|
4449 } |
|
27175
47b134bb51c3
Fix charmapencode_lookup(), so that a None value in the mapping
Walter Dörwald <walter@livinglogic.de>
parents:
27170
diff
changeset
|
4450 else if (x == Py_None) |
| 51216 | 4451 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
|
4452 else if (PyInt_Check(x)) { |
| 51216 | 4453 long value = PyInt_AS_LONG(x); |
| 4454 if (value < 0 || value > 255) { | |
| 4455 PyErr_SetString(PyExc_TypeError, | |
| 4456 "character mapping must be in range(256)"); | |
| 4457 Py_DECREF(x); | |
| 4458 return NULL; | |
| 4459 } | |
| 4460 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
|
4461 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4462 else if (PyString_Check(x)) |
| 51216 | 4463 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
|
4464 else { |
| 51216 | 4465 /* wrong return value */ |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4466 PyErr_SetString(PyExc_TypeError, |
| 51216 | 4467 "character mapping must return integer, None or str"); |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4468 Py_DECREF(x); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4469 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
|
4470 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4471 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4472 |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4473 static int |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4474 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
|
4475 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4476 Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4477 /* exponentially overallocate to minimize reallocations */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4478 if (requiredsize < 2*outsize) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4479 requiredsize = 2*outsize; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4480 if (_PyString_Resize(outobj, requiredsize)) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4481 return 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4482 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4483 return 1; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4484 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4485 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4486 typedef enum charmapencode_result { |
| 51216 | 4487 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
|
4488 }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
|
4489 /* 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
|
4490 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
|
4491 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
|
4492 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
|
4493 (in which case no character was written) or NULL, if a |
| 35827 | 4494 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
|
4495 static |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4496 charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 51216 | 4497 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
|
4498 { |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4499 PyObject *rep; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4500 char *outstart; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4501 Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4502 |
|
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
|
4503 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
|
4504 int res = encoding_map_lookup(c, mapping); |
| 51216 | 4505 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
|
4506 if (res == -1) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4507 return enc_FAILED; |
| 51216 | 4508 if (outsize<requiredsize) |
| 4509 if (!charmapencode_resize(outobj, outpos, requiredsize)) | |
| 4510 return enc_EXCEPTION; | |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4511 outstart = PyString_AS_STRING(*outobj); |
| 51216 | 4512 outstart[(*outpos)++] = (char)res; |
| 4513 return enc_SUCCESS; | |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4514 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4515 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4516 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
|
4517 if (rep==NULL) |
| 51216 | 4518 return enc_EXCEPTION; |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4519 else if (rep==Py_None) { |
| 51216 | 4520 Py_DECREF(rep); |
| 4521 return enc_FAILED; | |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4522 } else { |
| 51216 | 4523 if (PyInt_Check(rep)) { |
| 4524 Py_ssize_t requiredsize = *outpos+1; | |
| 4525 if (outsize<requiredsize) | |
| 4526 if (!charmapencode_resize(outobj, outpos, requiredsize)) { | |
| 4527 Py_DECREF(rep); | |
| 4528 return enc_EXCEPTION; | |
| 4529 } | |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4530 outstart = PyString_AS_STRING(*outobj); |
| 51216 | 4531 outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 4532 } | |
| 4533 else { | |
| 4534 const char *repchars = PyString_AS_STRING(rep); | |
| 4535 Py_ssize_t repsize = PyString_GET_SIZE(rep); | |
| 4536 Py_ssize_t requiredsize = *outpos+repsize; | |
| 4537 if (outsize<requiredsize) | |
| 4538 if (!charmapencode_resize(outobj, outpos, requiredsize)) { | |
| 4539 Py_DECREF(rep); | |
| 4540 return enc_EXCEPTION; | |
| 4541 } | |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4542 outstart = PyString_AS_STRING(*outobj); |
| 51216 | 4543 memcpy(outstart + *outpos, repchars, repsize); |
| 4544 *outpos += repsize; | |
| 4545 } | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4546 } |
|
38587
21969b71c36d
Repair refleaks in unicodeobject.
Georg Brandl <georg@python.org>
parents:
38586
diff
changeset
|
4547 Py_DECREF(rep); |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4548 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
|
4549 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4550 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4551 /* 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
|
4552 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
|
4553 static |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4554 int charmap_encoding_error( |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4555 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
|
4556 PyObject **exceptionObject, |
|
30149
59bad7efff02
Fix refcount leak in PyUnicode_EncodeCharmap(). The bug surfaces
Walter Dörwald <walter@livinglogic.de>
parents:
29706
diff
changeset
|
4557 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
|
4558 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
|
4559 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4560 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
|
4561 Py_ssize_t repsize; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4562 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
|
4563 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
|
4564 /* startpos for collecting unencodable chars */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4565 Py_ssize_t collstartpos = *inpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4566 Py_ssize_t collendpos = *inpos+1; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4567 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
|
4568 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
|
4569 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
|
4570 charmapencode_result x; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4571 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4572 /* 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
|
4573 while (collendpos < size) { |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4574 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
|
4575 if (Py_TYPE(mapping) == &EncodingMapType) { |
| 51216 | 4576 int res = encoding_map_lookup(p[collendpos], mapping); |
| 4577 if (res != -1) | |
| 4578 break; | |
| 4579 ++collendpos; | |
| 4580 continue; | |
| 4581 } | |
| 4582 | |
| 4583 rep = charmapencode_lookup(p[collendpos], mapping); | |
| 4584 if (rep==NULL) | |
| 4585 return -1; | |
| 4586 else if (rep!=Py_None) { | |
| 4587 Py_DECREF(rep); | |
| 4588 break; | |
| 4589 } | |
| 4590 Py_DECREF(rep); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4591 ++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
|
4592 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4593 /* 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
|
4594 * (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
|
4595 if (*known_errorHandler==-1) { |
| 51216 | 4596 if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4597 *known_errorHandler = 1; | |
| 4598 else if (!strcmp(errors, "replace")) | |
| 4599 *known_errorHandler = 2; | |
| 4600 else if (!strcmp(errors, "ignore")) | |
| 4601 *known_errorHandler = 3; | |
| 4602 else if (!strcmp(errors, "xmlcharrefreplace")) | |
| 4603 *known_errorHandler = 4; | |
| 4604 else | |
| 4605 *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
|
4606 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4607 switch (*known_errorHandler) { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4608 case 1: /* strict */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4609 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
|
4610 return -1; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4611 case 2: /* replace */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4612 for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 51216 | 4613 x = charmapencode_output('?', mapping, res, respos); |
| 4614 if (x==enc_EXCEPTION) { | |
| 4615 return -1; | |
| 4616 } | |
| 4617 else if (x==enc_FAILED) { | |
| 4618 raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | |
| 4619 return -1; | |
| 4620 } | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4621 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4622 /* fall through */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4623 case 3: /* ignore */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4624 *inpos = collendpos; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4625 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4626 case 4: /* xmlcharrefreplace */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4627 /* generate replacement (temporarily (mis)uses p) */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4628 for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 51216 | 4629 char buffer[2+29+1+1]; |
| 4630 char *cp; | |
| 4631 sprintf(buffer, "&#%d;", (int)p[collpos]); | |
| 4632 for (cp = buffer; *cp; ++cp) { | |
| 4633 x = charmapencode_output(*cp, mapping, res, respos); | |
| 4634 if (x==enc_EXCEPTION) | |
| 4635 return -1; | |
| 4636 else if (x==enc_FAILED) { | |
| 4637 raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | |
| 4638 return -1; | |
| 4639 } | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4640 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4641 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4642 *inpos = collendpos; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4643 break; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4644 default: |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4645 repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
| 51216 | 4646 encoding, reason, p, size, exceptionObject, |
| 4647 collstartpos, collendpos, &newpos); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4648 if (repunicode == NULL) |
| 51216 | 4649 return -1; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4650 /* generate replacement */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4651 repsize = PyUnicode_GET_SIZE(repunicode); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4652 for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 51216 | 4653 x = charmapencode_output(*uni2, mapping, res, respos); |
| 4654 if (x==enc_EXCEPTION) { | |
| 4655 return -1; | |
| 4656 } | |
| 4657 else if (x==enc_FAILED) { | |
| 4658 Py_DECREF(repunicode); | |
| 4659 raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); | |
| 4660 return -1; | |
| 4661 } | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4662 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4663 *inpos = newpos; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4664 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
|
4665 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4666 return 0; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4667 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4668 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4669 PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 51216 | 4670 Py_ssize_t size, |
| 4671 PyObject *mapping, | |
| 4672 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
|
4673 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4674 /* 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
|
4675 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
|
4676 /* current input position */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4677 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
|
4678 /* current output position */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4679 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
|
4680 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
|
4681 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
|
4682 /* 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
|
4683 * -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
|
4684 * 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
|
4685 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
|
4686 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4687 /* 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
|
4688 if (mapping == NULL) |
| 51216 | 4689 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
|
4690 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4691 /* 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
|
4692 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
|
4693 res = PyString_FromStringAndSize(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
|
4694 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
|
4695 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
|
4696 if (size == 0) |
| 51216 | 4697 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
|
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 while (inpos<size) { |
| 51216 | 4700 /* try to encode it */ |
| 4701 charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); | |
| 4702 if (x==enc_EXCEPTION) /* error */ | |
| 4703 goto onError; | |
| 4704 if (x==enc_FAILED) { /* unencodable character */ | |
| 4705 if (charmap_encoding_error(p, size, &inpos, mapping, | |
| 4706 &exc, | |
| 4707 &known_errorHandler, &errorHandler, errors, | |
| 4708 &res, &respos)) { | |
| 4709 goto onError; | |
| 4710 } | |
| 4711 } | |
| 4712 else | |
| 4713 /* done with this character => adjust input position */ | |
| 4714 ++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
|
4715 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4716 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4717 /* 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
|
4718 if (respos<PyString_GET_SIZE(res)) { |
| 51216 | 4719 if (_PyString_Resize(&res, respos)) |
| 4720 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
|
4721 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4722 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
|
4723 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
|
4724 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
|
4725 |
| 51216 | 4726 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
|
4727 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
|
4728 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
|
4729 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
|
4730 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4731 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4732 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4733 PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 51216 | 4734 PyObject *mapping) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4735 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4736 if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 51216 | 4737 PyErr_BadArgument(); |
| 4738 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4739 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4740 return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 4741 PyUnicode_GET_SIZE(unicode), |
| 4742 mapping, | |
| 4743 NULL); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4744 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4745 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4746 /* 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
|
4747 static void make_translate_exception(PyObject **exceptionObject, |
| 51216 | 4748 const Py_UNICODE *unicode, Py_ssize_t size, |
| 4749 Py_ssize_t startpos, Py_ssize_t endpos, | |
| 4750 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
|
4751 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4752 if (*exceptionObject == NULL) { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4753 *exceptionObject = PyUnicodeTranslateError_Create( |
| 51216 | 4754 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
|
4755 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4756 else { |
| 51216 | 4757 if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 4758 goto onError; | |
| 4759 if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) | |
| 4760 goto onError; | |
| 4761 if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) | |
| 4762 goto onError; | |
| 4763 return; | |
| 4764 onError: | |
| 4765 Py_DECREF(*exceptionObject); | |
| 4766 *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
|
4767 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4768 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4769 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4770 /* 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
|
4771 static void raise_translate_exception(PyObject **exceptionObject, |
| 51216 | 4772 const Py_UNICODE *unicode, Py_ssize_t size, |
| 4773 Py_ssize_t startpos, Py_ssize_t endpos, | |
| 4774 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
|
4775 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4776 make_translate_exception(exceptionObject, |
| 51216 | 4777 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
|
4778 if (*exceptionObject != NULL) |
| 51216 | 4779 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
|
4780 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4781 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4782 /* 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
|
4783 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
|
4784 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
|
4785 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
|
4786 static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 51216 | 4787 PyObject **errorHandler, |
| 4788 const char *reason, | |
| 4789 const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, | |
| 4790 Py_ssize_t startpos, Py_ssize_t endpos, | |
| 4791 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
|
4792 { |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
4793 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
|
4794 |
|
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
4795 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
|
4796 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
|
4797 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
|
4798 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4799 if (*errorHandler == NULL) { |
| 51216 | 4800 *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
|
4801 if (*errorHandler == NULL) |
| 51216 | 4802 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
|
4803 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4804 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4805 make_translate_exception(exceptionObject, |
| 51216 | 4806 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
|
4807 if (*exceptionObject == NULL) |
| 51216 | 4808 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
|
4809 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4810 restuple = PyObject_CallFunctionObjArgs( |
| 51216 | 4811 *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
|
4812 if (restuple == NULL) |
| 51216 | 4813 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
|
4814 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
|
4815 PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 51216 | 4816 Py_DECREF(restuple); |
| 4817 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
|
4818 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4819 if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 51216 | 4820 &resunicode, &i_newpos)) { |
| 4821 Py_DECREF(restuple); | |
| 4822 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
|
4823 } |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4824 if (i_newpos<0) |
| 51216 | 4825 *newpos = size+i_newpos; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4826 else |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4827 *newpos = i_newpos; |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
4828 if (*newpos<0 || *newpos>size) { |
| 51216 | 4829 PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 4830 Py_DECREF(restuple); | |
| 4831 return NULL; | |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
4832 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4833 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
|
4834 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
|
4835 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
|
4836 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4837 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4838 /* 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
|
4839 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
|
4840 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
|
4841 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
|
4842 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
|
4843 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4844 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
|
4845 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
|
4846 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4847 if (w == NULL) |
| 51216 | 4848 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
|
4849 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
|
4850 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
|
4851 if (x == NULL) { |
| 51216 | 4852 if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4853 /* No mapping found means: use 1:1 mapping. */ | |
| 4854 PyErr_Clear(); | |
| 4855 *result = NULL; | |
| 4856 return 0; | |
| 4857 } else | |
| 4858 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
|
4859 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4860 else if (x == Py_None) { |
| 51216 | 4861 *result = x; |
| 4862 return 0; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4863 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4864 else if (PyInt_Check(x)) { |
| 51216 | 4865 long value = PyInt_AS_LONG(x); |
| 4866 long max = PyUnicode_GetMax(); | |
| 4867 if (value < 0 || value > max) { | |
| 4868 PyErr_Format(PyExc_TypeError, | |
| 4869 "character mapping must be in range(0x%lx)", max+1); | |
| 4870 Py_DECREF(x); | |
| 4871 return -1; | |
| 4872 } | |
| 4873 *result = x; | |
| 4874 return 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
|
4875 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4876 else if (PyUnicode_Check(x)) { |
| 51216 | 4877 *result = x; |
| 4878 return 0; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4879 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4880 else { |
| 51216 | 4881 /* wrong return value */ |
| 4882 PyErr_SetString(PyExc_TypeError, | |
| 4883 "character mapping must return integer, None or unicode"); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4884 Py_DECREF(x); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4885 return -1; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4886 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4887 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4888 /* ensure that *outobj is at least requiredsize characters long, |
| 51216 | 4889 if not reallocate and adjust various state variables. |
| 4890 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
|
4891 static |
|
30765
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4892 int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
| 51216 | 4893 Py_ssize_t requiredsize) |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4894 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4895 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
|
4896 if (requiredsize > oldsize) { |
| 51216 | 4897 /* remember old output position */ |
| 4898 Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); | |
| 4899 /* exponentially overallocate to minimize reallocations */ | |
| 4900 if (requiredsize < 2 * oldsize) | |
| 4901 requiredsize = 2 * oldsize; | |
| 4902 if (PyUnicode_Resize(outobj, requiredsize) < 0) | |
| 4903 return -1; | |
| 4904 *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
|
4905 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4906 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
|
4907 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4908 /* 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
|
4909 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
|
4910 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
|
4911 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
|
4912 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
|
4913 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
|
4914 static |
|
30765
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4915 int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
| 51216 | 4916 Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 4917 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
|
4918 { |
|
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4919 if (charmaptranslate_lookup(*curinp, mapping, res)) |
| 51216 | 4920 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
|
4921 if (*res==NULL) { |
| 51216 | 4922 /* not found => default to 1:1 mapping */ |
| 4923 *(*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
|
4924 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4925 else if (*res==Py_None) |
| 51216 | 4926 ; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4927 else if (PyInt_Check(*res)) { |
| 51216 | 4928 /* no overflow check, because we know that the space is enough */ |
| 4929 *(*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
|
4930 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4931 else if (PyUnicode_Check(*res)) { |
| 51216 | 4932 Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
| 4933 if (repsize==1) { | |
| 4934 /* no overflow check, because we know that the space is enough */ | |
| 4935 *(*outp)++ = *PyUnicode_AS_UNICODE(*res); | |
| 4936 } | |
| 4937 else if (repsize!=0) { | |
| 4938 /* more than one character */ | |
| 4939 Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + | |
| 4940 (insize - (curinp-startinp)) + | |
| 4941 repsize - 1; | |
| 4942 if (charmaptranslate_makespace(outobj, outp, requiredsize)) | |
| 4943 return -1; | |
| 4944 memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); | |
| 4945 *outp += repsize; | |
| 4946 } | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4947 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4948 else |
| 51216 | 4949 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
|
4950 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
|
4951 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4952 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4953 PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 51216 | 4954 Py_ssize_t size, |
| 4955 PyObject *mapping, | |
| 4956 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
|
4957 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4958 /* 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
|
4959 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
|
4960 /* 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
|
4961 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
|
4962 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
|
4963 /* 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
|
4964 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
|
4965 /* current output position */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4966 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
|
4967 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
|
4968 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
|
4969 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
|
4970 /* 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
|
4971 * -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
|
4972 * 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
|
4973 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
|
4974 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4975 if (mapping == NULL) { |
| 51216 | 4976 PyErr_BadArgument(); |
| 4977 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4978 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4979 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4980 /* 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
|
4981 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
|
4982 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
|
4983 if (res == NULL) |
| 51216 | 4984 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4985 if (size == 0) |
| 51216 | 4986 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
|
4987 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
|
4988 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4989 while (p<endp) { |
| 51216 | 4990 /* try to encode it */ |
| 4991 PyObject *x = NULL; | |
| 4992 if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { | |
| 4993 Py_XDECREF(x); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4994 goto onError; |
| 51216 | 4995 } |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
4996 Py_XDECREF(x); |
| 51216 | 4997 if (x!=Py_None) /* it worked => adjust input pointer */ |
| 4998 ++p; | |
| 4999 else { /* untranslatable character */ | |
| 5000 PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ | |
| 5001 Py_ssize_t repsize; | |
| 5002 Py_ssize_t newpos; | |
| 5003 Py_UNICODE *uni2; | |
| 5004 /* startpos for collecting untranslatable chars */ | |
| 5005 const Py_UNICODE *collstart = p; | |
| 5006 const Py_UNICODE *collend = p+1; | |
| 5007 const Py_UNICODE *coll; | |
| 5008 | |
| 5009 /* find all untranslatable characters */ | |
| 5010 while (collend < endp) { | |
| 5011 if (charmaptranslate_lookup(*collend, mapping, &x)) | |
| 5012 goto onError; | |
| 5013 Py_XDECREF(x); | |
| 5014 if (x!=Py_None) | |
| 5015 break; | |
| 5016 ++collend; | |
| 5017 } | |
| 5018 /* cache callback name lookup | |
| 5019 * (if not done yet, i.e. it's the first error) */ | |
| 5020 if (known_errorHandler==-1) { | |
| 5021 if ((errors==NULL) || (!strcmp(errors, "strict"))) | |
| 5022 known_errorHandler = 1; | |
| 5023 else if (!strcmp(errors, "replace")) | |
| 5024 known_errorHandler = 2; | |
| 5025 else if (!strcmp(errors, "ignore")) | |
| 5026 known_errorHandler = 3; | |
| 5027 else if (!strcmp(errors, "xmlcharrefreplace")) | |
| 5028 known_errorHandler = 4; | |
| 5029 else | |
| 5030 known_errorHandler = 0; | |
| 5031 } | |
| 5032 switch (known_errorHandler) { | |
| 5033 case 1: /* strict */ | |
| 5034 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
|
5035 goto onError; |
| 51216 | 5036 case 2: /* replace */ |
| 5037 /* No need to check for space, this is a 1:1 replacement */ | |
| 5038 for (coll = collstart; coll<collend; ++coll) | |
| 5039 *str++ = '?'; | |
| 5040 /* fall through */ | |
| 5041 case 3: /* ignore */ | |
| 5042 p = collend; | |
| 5043 break; | |
| 5044 case 4: /* xmlcharrefreplace */ | |
| 5045 /* generate replacement (temporarily (mis)uses p) */ | |
| 5046 for (p = collstart; p < collend; ++p) { | |
| 5047 char buffer[2+29+1+1]; | |
| 5048 char *cp; | |
| 5049 sprintf(buffer, "&#%d;", (int)*p); | |
| 5050 if (charmaptranslate_makespace(&res, &str, | |
| 5051 (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) | |
| 5052 goto onError; | |
| 5053 for (cp = buffer; *cp; ++cp) | |
| 5054 *str++ = *cp; | |
| 5055 } | |
| 5056 p = collend; | |
| 5057 break; | |
| 5058 default: | |
| 5059 repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, | |
| 5060 reason, startp, size, &exc, | |
| 5061 collstart-startp, collend-startp, &newpos); | |
| 5062 if (repunicode == NULL) | |
| 5063 goto onError; | |
| 5064 /* generate replacement */ | |
| 5065 repsize = PyUnicode_GET_SIZE(repunicode); | |
| 5066 if (charmaptranslate_makespace(&res, &str, | |
| 5067 (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { | |
| 5068 Py_DECREF(repunicode); | |
| 5069 goto onError; | |
| 5070 } | |
| 5071 for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) | |
| 5072 *str++ = *uni2; | |
| 5073 p = startp + newpos; | |
| 5074 Py_DECREF(repunicode); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5075 } |
| 51216 | 5076 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5077 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5078 /* 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
|
5079 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
|
5080 if (respos<PyUnicode_GET_SIZE(res)) { |
| 51216 | 5081 if (PyUnicode_Resize(&res, respos) < 0) |
| 5082 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
|
5083 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5084 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
|
5085 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
|
5086 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
|
5087 |
| 51216 | 5088 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
|
5089 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
|
5090 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
|
5091 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
|
5092 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5093 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5094 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5095 PyObject *PyUnicode_Translate(PyObject *str, |
| 51216 | 5096 PyObject *mapping, |
| 5097 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
|
5098 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5099 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
|
5100 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5101 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
|
5102 if (str == NULL) |
| 51216 | 5103 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5104 result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 51216 | 5105 PyUnicode_GET_SIZE(str), |
| 5106 mapping, | |
| 5107 errors); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5108 Py_DECREF(str); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5109 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
|
5110 |
| 51216 | 5111 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5112 Py_XDECREF(str); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5113 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5114 } |
|
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
|
5115 |
|
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
|
5116 /* --- 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
|
5117 |
|
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
|
5118 int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 51216 | 5119 Py_ssize_t length, |
| 5120 char *output, | |
| 5121 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
|
5122 { |
|
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
|
5123 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
|
5124 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
|
5125 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
|
5126 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
|
5127 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
|
5128 /* 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
|
5129 * -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
|
5130 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
|
5131 |
|
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
|
5132 if (output == NULL) { |
| 51216 | 5133 PyErr_BadArgument(); |
| 5134 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
|
5135 } |
|
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 |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
12193
diff
changeset
|
5137 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
|
5138 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
|
5139 while (p < end) { |
| 51216 | 5140 register Py_UNICODE ch = *p; |
| 5141 int decimal; | |
| 5142 PyObject *repunicode; | |
| 5143 Py_ssize_t repsize; | |
| 5144 Py_ssize_t newpos; | |
| 5145 Py_UNICODE *uni2; | |
| 5146 Py_UNICODE *collstart; | |
| 5147 Py_UNICODE *collend; | |
| 5148 | |
| 5149 if (Py_UNICODE_ISSPACE(ch)) { | |
| 5150 *output++ = ' '; | |
| 5151 ++p; | |
| 5152 continue; | |
| 5153 } | |
| 5154 decimal = Py_UNICODE_TODECIMAL(ch); | |
| 5155 if (decimal >= 0) { | |
| 5156 *output++ = '0' + decimal; | |
| 5157 ++p; | |
| 5158 continue; | |
| 5159 } | |
| 5160 if (0 < ch && ch < 256) { | |
| 5161 *output++ = (char)ch; | |
| 5162 ++p; | |
| 5163 continue; | |
| 5164 } | |
| 5165 /* All other characters are considered unencodable */ | |
| 5166 collstart = p; | |
| 5167 collend = p+1; | |
| 5168 while (collend < end) { | |
| 5169 if ((0 < *collend && *collend < 256) || | |
| 5170 !Py_UNICODE_ISSPACE(*collend) || | |
| 5171 Py_UNICODE_TODECIMAL(*collend)) | |
| 5172 break; | |
| 5173 } | |
| 5174 /* cache callback name lookup | |
| 5175 * (if not done yet, i.e. it's the first error) */ | |
| 5176 if (known_errorHandler==-1) { | |
| 5177 if ((errors==NULL) || (!strcmp(errors, "strict"))) | |
| 5178 known_errorHandler = 1; | |
| 5179 else if (!strcmp(errors, "replace")) | |
| 5180 known_errorHandler = 2; | |
| 5181 else if (!strcmp(errors, "ignore")) | |
| 5182 known_errorHandler = 3; | |
| 5183 else if (!strcmp(errors, "xmlcharrefreplace")) | |
| 5184 known_errorHandler = 4; | |
| 5185 else | |
| 5186 known_errorHandler = 0; | |
| 5187 } | |
| 5188 switch (known_errorHandler) { | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5189 case 1: /* strict */ |
| 51216 | 5190 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
|
5191 goto onError; |
| 51216 | 5192 case 2: /* replace */ |
| 5193 for (p = collstart; p < collend; ++p) | |
| 5194 *output++ = '?'; | |
| 5195 /* fall through */ | |
| 5196 case 3: /* ignore */ | |
| 5197 p = collend; | |
| 5198 break; | |
| 5199 case 4: /* xmlcharrefreplace */ | |
| 5200 /* generate replacement (temporarily (mis)uses p) */ | |
| 5201 for (p = collstart; p < collend; ++p) | |
| 5202 output += sprintf(output, "&#%d;", (int)*p); | |
| 5203 p = collend; | |
| 5204 break; | |
| 5205 default: | |
| 5206 repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, | |
| 5207 encoding, reason, s, length, &exc, | |
| 5208 collstart-s, collend-s, &newpos); | |
| 5209 if (repunicode == NULL) | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5210 goto onError; |
| 51216 | 5211 /* generate replacement */ |
| 5212 repsize = PyUnicode_GET_SIZE(repunicode); | |
| 5213 for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { | |
| 5214 Py_UNICODE ch = *uni2; | |
| 5215 if (Py_UNICODE_ISSPACE(ch)) | |
| 5216 *output++ = ' '; | |
| 5217 else { | |
| 5218 decimal = Py_UNICODE_TODECIMAL(ch); | |
| 5219 if (decimal >= 0) | |
| 5220 *output++ = '0' + decimal; | |
| 5221 else if (0 < ch && ch < 256) | |
| 5222 *output++ = (char)ch; | |
| 5223 else { | |
| 5224 Py_DECREF(repunicode); | |
| 5225 raise_encode_exception(&exc, encoding, | |
| 5226 s, length, collstart-s, collend-s, reason); | |
| 5227 goto onError; | |
| 5228 } | |
| 5229 } | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5230 } |
| 51216 | 5231 p = s + newpos; |
| 5232 Py_DECREF(repunicode); | |
| 5233 } | |
|
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
|
5234 } |
|
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
|
5235 /* 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
|
5236 *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
|
5237 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
|
5238 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
|
5239 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
|
5240 |
| 51216 | 5241 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
|
5242 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
|
5243 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
|
5244 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
|
5245 } |
|
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
|
5246 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5247 /* --- Helpers ------------------------------------------------------------ */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5248 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
5249 #include "stringlib/unicodedefs.h" |
|
38381
b32f9bb2e077
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38347
diff
changeset
|
5250 #include "stringlib/fastsearch.h" |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5251 |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5252 #include "stringlib/count.h" |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5253 #include "stringlib/find.h" |
|
38383
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
5254 #include "stringlib/partition.h" |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5255 #include "stringlib/split.h" |
|
38383
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
5256 |
|
38402
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5257 /* helper macro to fixup start/end slice values */ |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5258 #define ADJUST_INDICES(start, end, len) \ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5259 if (end > len) \ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5260 end = len; \ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5261 else if (end < 0) { \ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5262 end += len; \ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5263 if (end < 0) \ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5264 end = 0; \ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5265 } \ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5266 if (start < 0) { \ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5267 start += len; \ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5268 if (start < 0) \ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5269 start = 0; \ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5270 } |
|
38402
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5271 |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5272 Py_ssize_t PyUnicode_Count(PyObject *str, |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5273 PyObject *substr, |
|
38347
6d8615f9f1cc
use Py_LOCAL also for string and unicode objects
Fredrik Lundh <fredrik@pythonware.com>
parents:
38339
diff
changeset
|
5274 Py_ssize_t start, |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5275 Py_ssize_t end) |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5276 { |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5277 Py_ssize_t result; |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5278 PyUnicodeObject* str_obj; |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5279 PyUnicodeObject* sub_obj; |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5280 |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5281 str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5282 if (!str_obj) |
| 51216 | 5283 return -1; |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5284 sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5285 if (!sub_obj) { |
| 51216 | 5286 Py_DECREF(str_obj); |
| 5287 return -1; | |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5288 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5289 |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5290 ADJUST_INDICES(start, end, str_obj->length); |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5291 result = stringlib_count( |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5292 str_obj->str + start, end - start, sub_obj->str, sub_obj->length, |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5293 PY_SSIZE_T_MAX |
|
38275
43b1474b512b
needforspeed: use "fastsearch" for count and findstring helpers. this
Fredrik Lundh <fredrik@pythonware.com>
parents:
38274
diff
changeset
|
5294 ); |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
5295 |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5296 Py_DECREF(sub_obj); |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5297 Py_DECREF(str_obj); |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5298 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5299 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5300 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5301 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5302 Py_ssize_t PyUnicode_Find(PyObject *str, |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5303 PyObject *sub, |
|
38401
adc214fc1e1c
needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
38400
diff
changeset
|
5304 Py_ssize_t start, |
|
adc214fc1e1c
needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
38400
diff
changeset
|
5305 Py_ssize_t end, |
|
adc214fc1e1c
needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
38400
diff
changeset
|
5306 int direction) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5307 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5308 Py_ssize_t result; |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5309 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5310 str = PyUnicode_FromObject(str); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5311 if (!str) |
| 51216 | 5312 return -2; |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5313 sub = PyUnicode_FromObject(sub); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5314 if (!sub) { |
| 51216 | 5315 Py_DECREF(str); |
| 5316 return -2; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5317 } |
|
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
|
5318 |
|
38401
adc214fc1e1c
needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
38400
diff
changeset
|
5319 if (direction > 0) |
|
38441
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
5320 result = stringlib_find_slice( |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
5321 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
|
5322 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
|
5323 start, end |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
5324 ); |
|
38401
adc214fc1e1c
needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
38400
diff
changeset
|
5325 else |
|
38441
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
5326 result = stringlib_rfind_slice( |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
5327 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
|
5328 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
|
5329 start, end |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
5330 ); |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5331 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5332 Py_DECREF(str); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5333 Py_DECREF(sub); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5334 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5335 return result; |
|
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 tailmatch(PyUnicodeObject *self, |
| 51216 | 5340 PyUnicodeObject *substring, |
| 5341 Py_ssize_t start, | |
| 5342 Py_ssize_t end, | |
| 5343 int direction) | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5344 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5345 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
|
5346 return 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5347 |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5348 ADJUST_INDICES(start, end, self->length); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5349 end -= substring->length; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5350 if (end < start) |
| 51216 | 5351 return 0; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5352 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5353 if (direction > 0) { |
| 51216 | 5354 if (Py_UNICODE_MATCH(self, end, substring)) |
| 5355 return 1; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5356 } else { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5357 if (Py_UNICODE_MATCH(self, start, substring)) |
| 51216 | 5358 return 1; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5359 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5360 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5361 return 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5362 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5363 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5364 Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
| 51216 | 5365 PyObject *substr, |
| 5366 Py_ssize_t start, | |
| 5367 Py_ssize_t end, | |
| 5368 int direction) | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5369 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5370 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
|
5371 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5372 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
|
5373 if (str == NULL) |
| 51216 | 5374 return -1; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5375 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
|
5376 if (substr == NULL) { |
| 51216 | 5377 Py_DECREF(str); |
| 5378 return -1; | |
|
11968
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 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5381 result = tailmatch((PyUnicodeObject *)str, |
| 51216 | 5382 (PyUnicodeObject *)substr, |
| 5383 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
|
5384 Py_DECREF(str); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5385 Py_DECREF(substr); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5386 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5387 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5388 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5389 /* 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
|
5390 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
|
5391 |
|
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
|
5392 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5393 PyObject *fixup(PyUnicodeObject *self, |
| 51216 | 5394 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
|
5395 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5396 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5397 PyUnicodeObject *u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5398 |
|
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
|
5399 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
|
5400 if (u == NULL) |
| 51216 | 5401 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
|
5402 |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
5403 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
|
5404 |
|
19885
72e6eb653312
More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19849
diff
changeset
|
5405 if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
| 51216 | 5406 /* fixfct should return TRUE if it modified the buffer. If |
| 5407 FALSE, return a reference to the original buffer instead | |
| 5408 (to save space, not time) */ | |
| 5409 Py_INCREF(self); | |
| 5410 Py_DECREF(u); | |
| 5411 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
|
5412 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5413 return (PyObject*) u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5414 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5415 |
|
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
|
5416 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5417 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
|
5418 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5419 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
|
5420 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
|
5421 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
|
5422 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5423 while (len-- > 0) { |
| 51216 | 5424 register Py_UNICODE ch; |
| 5425 | |
| 5426 ch = Py_UNICODE_TOUPPER(*s); | |
| 5427 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
|
5428 status = 1; |
| 51216 | 5429 *s = ch; |
| 5430 } | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5431 s++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5432 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5433 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5434 return status; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5435 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5436 |
|
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
|
5437 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5438 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
|
5439 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5440 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
|
5441 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
|
5442 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
|
5443 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5444 while (len-- > 0) { |
| 51216 | 5445 register Py_UNICODE ch; |
| 5446 | |
| 5447 ch = Py_UNICODE_TOLOWER(*s); | |
| 5448 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
|
5449 status = 1; |
| 51216 | 5450 *s = ch; |
| 5451 } | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5452 s++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5453 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5454 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5455 return status; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5456 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5457 |
|
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
|
5458 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5459 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
|
5460 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5461 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
|
5462 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
|
5463 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
|
5464 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5465 while (len-- > 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5466 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
|
5467 *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
|
5468 status = 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5469 } 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
|
5470 *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
|
5471 status = 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5472 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5473 s++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5474 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5475 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5476 return status; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5477 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5478 |
|
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
|
5479 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5480 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
|
5481 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5482 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
|
5483 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
|
5484 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
|
5485 |
|
16332
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5486 if (len == 0) |
| 51216 | 5487 return 0; |
|
16332
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5488 if (Py_UNICODE_ISLOWER(*s)) { |
| 51216 | 5489 *s = Py_UNICODE_TOUPPER(*s); |
| 5490 status = 1; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5491 } |
|
16332
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5492 s++; |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5493 while (--len > 0) { |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5494 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
|
5495 *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
|
5496 status = 1; |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5497 } |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5498 s++; |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5499 } |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5500 return status; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5501 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5502 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5503 static |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5504 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
|
5505 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5506 register Py_UNICODE *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
|
5507 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
|
5508 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
|
5509 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5510 /* 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
|
5511 if (PyUnicode_GET_SIZE(self) == 1) { |
| 51216 | 5512 Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 5513 if (*p != ch) { | |
| 5514 *p = ch; | |
| 5515 return 1; | |
| 5516 } | |
| 5517 else | |
| 5518 return 0; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5519 } |
|
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
|
5520 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5521 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
|
5522 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
|
5523 for (; p < e; p++) { |
| 51216 | 5524 register const Py_UNICODE ch = *p; |
| 5525 | |
| 5526 if (previous_is_cased) | |
| 5527 *p = Py_UNICODE_TOLOWER(ch); | |
| 5528 else | |
| 5529 *p = Py_UNICODE_TOTITLE(ch); | |
| 5530 | |
| 5531 if (Py_UNICODE_ISLOWER(ch) || | |
| 5532 Py_UNICODE_ISUPPER(ch) || | |
| 5533 Py_UNICODE_ISTITLE(ch)) | |
| 5534 previous_is_cased = 1; | |
| 5535 else | |
| 5536 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
|
5537 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5538 return 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5539 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5540 |
|
33300
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5541 PyObject * |
|
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5542 PyUnicode_Join(PyObject *separator, PyObject *seq) |
|
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5543 { |
|
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5544 PyObject *internal_separator = NULL; |
|
33499
a4d3673bcd66
Initialize sep and seplen to suppress warning from gcc.
Skip Montanaro <skip@pobox.com>
parents:
33495
diff
changeset
|
5545 const Py_UNICODE blank = ' '; |
|
a4d3673bcd66
Initialize sep and seplen to suppress warning from gcc.
Skip Montanaro <skip@pobox.com>
parents:
33495
diff
changeset
|
5546 const Py_UNICODE *sep = ␣ |
|
38240
0373a074004c
PyUnicode_Join(): Recent code changes introduced new
Tim Peters <tim.peters@gmail.com>
parents:
38239
diff
changeset
|
5547 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
|
5548 PyUnicodeObject *res = NULL; /* the result */ |
|
38240
0373a074004c
PyUnicode_Join(): Recent code changes introduced new
Tim Peters <tim.peters@gmail.com>
parents:
38239
diff
changeset
|
5549 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
|
5550 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
|
5551 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
|
5552 PyObject *fseq; /* PySequence_Fast(seq) */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5553 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
|
5554 PyObject *item; |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5555 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
|
5556 |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5557 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
|
5558 if (fseq == NULL) { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5559 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
|
5560 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5561 |
|
33311
59bd121eeeb7
PyUnicode_Join(): Bozo Alert. While this is chugging along, it may
Tim Peters <tim.peters@gmail.com>
parents:
33310
diff
changeset
|
5562 /* 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
|
5563 * 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
|
5564 * 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
|
5565 * 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
|
5566 * 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
|
5567 * is invariant. |
|
59bd121eeeb7
PyUnicode_Join(): Bozo Alert. While this is chugging along, it may
Tim Peters <tim.peters@gmail.com>
parents:
33310
diff
changeset
|
5568 */ |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5569 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
|
5570 /* 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
|
5571 if (seqlen == 0) { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5572 res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5573 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
|
5574 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5575 /* 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
|
5576 if (seqlen == 1) { |
| 51216 | 5577 item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 5578 if (PyUnicode_CheckExact(item)) { | |
| 5579 Py_INCREF(item); | |
| 5580 res = (PyUnicodeObject *)item; | |
| 5581 goto Done; | |
| 5582 } | |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5583 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5584 |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5585 /* 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
|
5586 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
|
5587 /* Set up sep and seplen -- they're needed. */ |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5588 if (separator == NULL) { |
| 51216 | 5589 sep = ␣ |
| 5590 seplen = 1; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5591 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5592 else { |
| 51216 | 5593 internal_separator = PyUnicode_FromObject(separator); |
| 5594 if (internal_separator == NULL) | |
| 5595 goto onError; | |
| 5596 sep = PyUnicode_AS_UNICODE(internal_separator); | |
| 5597 seplen = PyUnicode_GET_SIZE(internal_separator); | |
| 5598 /* In case PyUnicode_FromObject() mutated seq. */ | |
| 5599 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
|
5600 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5601 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5602 |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5603 /* Get space. */ |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5604 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
|
5605 if (res == NULL) |
|
33300
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5606 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
|
5607 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
|
5608 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
|
5609 |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5610 for (i = 0; i < seqlen; ++i) { |
| 51216 | 5611 Py_ssize_t itemlen; |
| 5612 Py_ssize_t new_res_used; | |
| 5613 | |
| 5614 item = PySequence_Fast_GET_ITEM(fseq, i); | |
| 5615 /* Convert item to Unicode. */ | |
| 5616 if (! PyUnicode_Check(item) && ! PyString_Check(item)) { | |
| 5617 PyErr_Format(PyExc_TypeError, | |
| 5618 "sequence item %zd: expected string or Unicode," | |
| 5619 " %.80s found", | |
| 5620 i, Py_TYPE(item)->tp_name); | |
| 5621 goto onError; | |
| 5622 } | |
| 5623 item = PyUnicode_FromObject(item); | |
| 5624 if (item == NULL) | |
| 5625 goto onError; | |
| 5626 /* We own a reference to item from here on. */ | |
| 5627 | |
| 5628 /* In case PyUnicode_FromObject() mutated seq. */ | |
| 5629 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
|
5630 |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5631 /* Make sure we have enough space for the separator and the item. */ |
| 51216 | 5632 itemlen = PyUnicode_GET_SIZE(item); |
| 5633 new_res_used = res_used + itemlen; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5634 if (new_res_used < 0) |
| 51216 | 5635 goto Overflow; |
| 5636 if (i < seqlen - 1) { | |
| 5637 new_res_used += seplen; | |
| 5638 if (new_res_used < 0) | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5639 goto Overflow; |
| 51216 | 5640 } |
| 5641 if (new_res_used > res_alloc) { | |
| 5642 /* double allocated size until it's big enough */ | |
| 5643 do { | |
| 5644 res_alloc += res_alloc; | |
| 5645 if (res_alloc <= 0) | |
| 5646 goto Overflow; | |
| 5647 } while (new_res_used > res_alloc); | |
| 5648 if (_PyUnicode_Resize(&res, res_alloc) < 0) { | |
| 5649 Py_DECREF(item); | |
| 5650 goto onError; | |
| 5651 } | |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5652 res_p = PyUnicode_AS_UNICODE(res) + res_used; |
| 51216 | 5653 } |
| 5654 | |
| 5655 /* Copy item, and maybe the separator. */ | |
| 5656 Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); | |
| 5657 res_p += itemlen; | |
| 5658 if (i < seqlen - 1) { | |
| 5659 Py_UNICODE_COPY(res_p, sep, seplen); | |
| 5660 res_p += seplen; | |
| 5661 } | |
| 5662 Py_DECREF(item); | |
| 5663 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
|
5664 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5665 |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5666 /* 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
|
5667 * 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
|
5668 */ |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5669 if (_PyUnicode_Resize(&res, res_used) < 0) |
| 51216 | 5670 goto onError; |
| 5671 | |
| 5672 Done: | |
|
33300
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5673 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
|
5674 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
|
5675 return (PyObject *)res; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5676 |
| 51216 | 5677 Overflow: |
|
33300
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5678 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
|
5679 "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
|
5680 Py_DECREF(item); |
|
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5681 /* fall through */ |
|
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5682 |
| 51216 | 5683 onError: |
|
33300
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5684 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
|
5685 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
|
5686 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
|
5687 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5688 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5689 |
|
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
|
5690 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
|
5691 PyUnicodeObject *pad(PyUnicodeObject *self, |
| 51216 | 5692 Py_ssize_t left, |
| 5693 Py_ssize_t right, | |
| 5694 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
|
5695 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5696 PyUnicodeObject *u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5697 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5698 if (left < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5699 left = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5700 if (right < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5701 right = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5702 |
|
19885
72e6eb653312
More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19849
diff
changeset
|
5703 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
|
5704 Py_INCREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5705 return self; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5706 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5707 |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
5708 if (left > PY_SSIZE_T_MAX - self->length || |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48070
diff
changeset
|
5709 right > PY_SSIZE_T_MAX - (left + self->length)) { |
|
48113
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
5710 PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
5711 return NULL; |
|
0aafd1ae5120
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48072
diff
changeset
|
5712 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5713 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
|
5714 if (u) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5715 if (left) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5716 Py_UNICODE_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
|
5717 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
|
5718 if (right) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5719 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
|
5720 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5721 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5722 return u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5723 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5724 |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5725 PyObject *PyUnicode_Splitlines(PyObject *string, int keepends) |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5726 { |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5727 PyObject *list; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5728 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5729 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
|
5730 if (string == NULL) |
| 51216 | 5731 return NULL; |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5732 |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5733 list = stringlib_splitlines( |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5734 (PyObject*) string, PyUnicode_AS_UNICODE(string), |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5735 PyUnicode_GET_SIZE(string), keepends); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5736 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5737 Py_DECREF(string); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5738 return list; |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5739 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5740 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5741 static |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5742 PyObject *split(PyUnicodeObject *self, |
| 51216 | 5743 PyUnicodeObject *substring, |
| 5744 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
|
5745 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5746 if (maxcount < 0) |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5747 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
|
5748 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5749 if (substring == NULL) |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5750 return stringlib_split_whitespace( |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5751 (PyObject*) self, self->str, self->length, maxcount |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5752 ); |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5753 |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5754 return stringlib_split( |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5755 (PyObject*) self, self->str, self->length, |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5756 substring->str, substring->length, |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5757 maxcount |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5758 ); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5759 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5760 |
|
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
|
5761 static |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5762 PyObject *rsplit(PyUnicodeObject *self, |
| 51216 | 5763 PyUnicodeObject *substring, |
| 5764 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
|
5765 { |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5766 if (maxcount < 0) |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5767 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
|
5768 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5769 if (substring == NULL) |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5770 return stringlib_rsplit_whitespace( |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5771 (PyObject*) self, self->str, self->length, maxcount |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5772 ); |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5773 |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5774 return stringlib_rsplit( |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5775 (PyObject*) self, self->str, self->length, |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5776 substring->str, substring->length, |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5777 maxcount |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5778 ); |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5779 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5780 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5781 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5782 PyObject *replace(PyUnicodeObject *self, |
| 51216 | 5783 PyUnicodeObject *str1, |
| 5784 PyUnicodeObject *str2, | |
| 5785 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
|
5786 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5787 PyUnicodeObject *u; |
|
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 if (maxcount < 0) |
| 51216 | 5790 maxcount = PY_SSIZE_T_MAX; |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5791 else if (maxcount == 0 || self->length == 0) |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5792 goto nothing; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5793 |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5794 if (str1->length == str2->length) { |
|
58290
d51358645e60
Fix Windows build (re r77461)
Antoine Pitrou <solipsis@pitrou.net>
parents:
58288
diff
changeset
|
5795 Py_ssize_t i; |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5796 /* same length */ |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5797 if (str1->length == 0) |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5798 goto nothing; |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5799 if (str1->length == 1) { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5800 /* replace characters */ |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5801 Py_UNICODE u1, u2; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5802 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
|
5803 goto nothing; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5804 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
|
5805 if (!u) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5806 return NULL; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5807 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
|
5808 u1 = str1->str[0]; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5809 u2 = str2->str[0]; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5810 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
|
5811 if (u->str[i] == u1) { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5812 if (--maxcount < 0) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5813 break; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5814 u->str[i] = u2; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5815 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5816 } else { |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5817 i = stringlib_find( |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5818 self->str, self->length, str1->str, str1->length, 0 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5819 ); |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5820 if (i < 0) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5821 goto nothing; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5822 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
|
5823 if (!u) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5824 return NULL; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5825 Py_UNICODE_COPY(u->str, self->str, self->length); |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5826 |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5827 /* change everything in-place, starting with this one */ |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5828 Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5829 i += str1->length; |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5830 |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5831 while ( --maxcount > 0) { |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5832 i = stringlib_find(self->str+i, self->length-i, |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5833 str1->str, str1->length, |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5834 i); |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5835 if (i == -1) |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5836 break; |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5837 Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5838 i += str1->length; |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5839 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5840 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5841 } else { |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5842 |
|
61204
55a593cc3280
Remove an unneeded variable and assignment.
Brett Cannon <bcannon@gmail.com>
parents:
60203
diff
changeset
|
5843 Py_ssize_t n, i, j; |
|
38299
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5844 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
|
5845 Py_UNICODE *p; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5846 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5847 /* replace strings */ |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5848 n = stringlib_count(self->str, self->length, str1->str, str1->length, |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5849 maxcount); |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5850 if (n == 0) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5851 goto nothing; |
|
38299
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5852 /* 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
|
5853 delta = (str2->length - str1->length); |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5854 if (delta == 0) { |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5855 new_size = self->length; |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5856 } else { |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5857 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
|
5858 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
|
5859 PyErr_SetString(PyExc_OverflowError, |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5860 "replace string is too long"); |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5861 return NULL; |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5862 } |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5863 new_size = self->length + product; |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5864 if (new_size < 0) { |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5865 PyErr_SetString(PyExc_OverflowError, |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5866 "replace string is too long"); |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5867 return NULL; |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5868 } |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5869 } |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5870 u = _PyUnicode_New(new_size); |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5871 if (!u) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5872 return NULL; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5873 i = 0; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5874 p = u->str; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5875 if (str1->length > 0) { |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5876 while (n-- > 0) { |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5877 /* look for next match */ |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5878 j = stringlib_find(self->str+i, self->length-i, |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5879 str1->str, str1->length, |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5880 i); |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5881 if (j == -1) |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5882 break; |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
5883 else if (j > i) { |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5884 /* copy unchanged part [i:j] */ |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5885 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
|
5886 p += j - i; |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5887 } |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5888 /* copy substitution string */ |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5889 if (str2->length > 0) { |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5890 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
|
5891 p += str2->length; |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5892 } |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5893 i = j + str1->length; |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5894 } |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5895 if (i < self->length) |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5896 /* copy tail [i:] */ |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5897 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
|
5898 } else { |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5899 /* interleave */ |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5900 while (n > 0) { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5901 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
|
5902 p += str2->length; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5903 if (--n <= 0) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5904 break; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5905 *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
|
5906 } |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5907 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
|
5908 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5909 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5910 return (PyObject *) u; |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5911 |
| 51216 | 5912 nothing: |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5913 /* 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
|
5914 if (PyUnicode_CheckExact(self)) { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5915 Py_INCREF(self); |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5916 return (PyObject *) self; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5917 } |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5918 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
|
5919 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5920 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5921 /* --- Unicode Object Methods --------------------------------------------- */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5922 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
5923 PyDoc_STRVAR(title__doc__, |
| 51216 | 5924 "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
|
5925 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5926 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
|
5927 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
|
5928 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5929 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
|
5930 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
|
5931 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5932 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
|
5933 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5934 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
5935 PyDoc_STRVAR(capitalize__doc__, |
| 51216 | 5936 "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
|
5937 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5938 Return a capitalized version of S, i.e. make the first character\n\ |
|
62803
e84d51644144
Merged revisions 82573 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
62518
diff
changeset
|
5939 have upper case and the rest lower case."); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5940 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5941 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
|
5942 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
|
5943 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5944 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
|
5945 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5946 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5947 #if 0 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
5948 PyDoc_STRVAR(capwords__doc__, |
| 51216 | 5949 "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
|
5950 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5951 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
|
5952 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
|
5953 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5954 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
|
5955 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
|
5956 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5957 PyObject *list; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5958 PyObject *item; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5959 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
|
5960 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5961 /* Split into words */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5962 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
|
5963 if (!list) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5964 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5965 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5966 /* Capitalize each word */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5967 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
|
5968 item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 51216 | 5969 fixcapitalize); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5970 if (item == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5971 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5972 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
|
5973 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
|
5974 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5975 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5976 /* 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
|
5977 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
|
5978 |
| 51216 | 5979 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5980 Py_DECREF(list); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5981 return (PyObject *)item; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5982 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5983 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5984 |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
5985 /* 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
|
5986 |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
5987 static int |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
5988 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
|
5989 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5990 Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5991 PyObject *uniobj; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5992 Py_UNICODE *unistr; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5993 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5994 uniobj = PyUnicode_FromObject(obj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5995 if (uniobj == NULL) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5996 PyErr_SetString(PyExc_TypeError, |
| 51216 | 5997 "The fill character cannot be converted to Unicode"); |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5998 return 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
5999 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6000 if (PyUnicode_GET_SIZE(uniobj) != 1) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6001 PyErr_SetString(PyExc_TypeError, |
| 51216 | 6002 "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
|
6003 Py_DECREF(uniobj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6004 return 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6005 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6006 unistr = PyUnicode_AS_UNICODE(uniobj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6007 *fillcharloc = unistr[0]; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6008 Py_DECREF(uniobj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6009 return 1; |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6010 } |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6011 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6012 PyDoc_STRVAR(center__doc__, |
| 51216 | 6013 "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
|
6014 \n\ |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6015 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
|
6016 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
|
6017 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6018 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6019 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
|
6020 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6021 Py_ssize_t marg, left; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6022 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
|
6023 Py_UNICODE fillchar = ' '; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6024 |
|
36532
0c096be27000
Use correct PyArg_Parse format char for Py_ssize_t in unicode.center().
Thomas Wouters <thomas@python.org>
parents:
36519
diff
changeset
|
6025 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
|
6026 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6027 |
|
19885
72e6eb653312
More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19849
diff
changeset
|
6028 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
|
6029 Py_INCREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6030 return (PyObject*) self; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6031 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6032 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6033 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
|
6034 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
|
6035 |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6036 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
|
6037 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6038 |
|
13890
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6039 #if 0 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6040 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6041 /* 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
|
6042 module. The basic comparison should compare ordinals on a naive |
|
56812
a1b0936b027c
Merged revisions 72558,72745,72750,72876,73042,73045-73048,73069,73089,73163,73186,73213,73215,73217,73257-73258,73260 via svnmerge from
Georg Brandl <georg@python.org>
parents:
56034
diff
changeset
|
6043 basis (this is what Java does and thus Jython too). */ |
|
13890
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6044 |
| 13337 | 6045 /* speedy UTF-16 code point order comparison */ |
| 6046 /* gleaned from: */ | |
| 6047 /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ | |
| 6048 | |
|
13411
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
6049 static short utf16Fixup[32] = |
| 13337 | 6050 { |
|
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
|
6051 0, 0, 0, 0, 0, 0, 0, 0, |
| 13337 | 6052 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
|
6053 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
|
6054 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
| 13337 | 6055 }; |
| 6056 | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6057 static int |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6058 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
|
6059 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6060 Py_ssize_t len1, len2; |
| 13337 | 6061 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6062 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
|
6063 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
|
6064 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6065 len1 = str1->length; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6066 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
|
6067 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6068 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
|
6069 Py_UNICODE c1, c2; |
| 13337 | 6070 |
| 6071 c1 = *s1++; | |
| 6072 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
|
6073 |
| 51216 | 6074 if (c1 > (1<<11) * 26) |
| 6075 c1 += utf16Fixup[c1>>11]; | |
| 6076 if (c2 > (1<<11) * 26) | |
| 13337 | 6077 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
|
6078 /* 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
|
6079 |
|
8b7ff44b4669
experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents:
18278
diff
changeset
|
6080 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
|
6081 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
|
6082 |
| 13337 | 6083 len1--; len2--; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6084 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6085 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6086 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
|
6087 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6088 |
|
13890
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6089 #else |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6090 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6091 static int |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6092 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
|
6093 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6094 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
|
6095 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6096 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
|
6097 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
|
6098 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6099 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
|
6100 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
|
6101 |
|
13890
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6102 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
|
6103 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
|
6104 |
|
8b7ff44b4669
experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents:
18278
diff
changeset
|
6105 c1 = *s1++; |
|
8b7ff44b4669
experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents:
18278
diff
changeset
|
6106 c2 = *s2++; |
|
8b7ff44b4669
experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents:
18278
diff
changeset
|
6107 |
|
8b7ff44b4669
experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents:
18278
diff
changeset
|
6108 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
|
6109 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
|
6110 |
|
13890
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6111 len1--; len2--; |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6112 } |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6113 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6114 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
|
6115 } |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6116 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6117 #endif |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6118 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6119 int PyUnicode_Compare(PyObject *left, |
| 51216 | 6120 PyObject *right) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6121 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6122 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
|
6123 int result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6124 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6125 /* 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
|
6126 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
|
6127 if (u == NULL) |
| 51216 | 6128 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6129 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
|
6130 if (v == NULL) |
| 51216 | 6131 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6132 |
|
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
|
6133 /* 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
|
6134 if (v == u) { |
| 51216 | 6135 Py_DECREF(u); |
| 6136 Py_DECREF(v); | |
| 6137 return 0; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6138 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6139 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6140 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
|
6141 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6142 Py_DECREF(u); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6143 Py_DECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6144 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6145 |
| 51216 | 6146 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6147 Py_XDECREF(u); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6148 Py_XDECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6149 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6150 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6151 |
|
39487
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6152 PyObject *PyUnicode_RichCompare(PyObject *left, |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6153 PyObject *right, |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6154 int op) |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6155 { |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6156 int result; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6157 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6158 result = PyUnicode_Compare(left, right); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6159 if (result == -1 && PyErr_Occurred()) |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6160 goto onError; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6161 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6162 /* Convert the return value to a Boolean */ |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6163 switch (op) { |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6164 case Py_EQ: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6165 result = (result == 0); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6166 break; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6167 case Py_NE: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6168 result = (result != 0); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6169 break; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6170 case Py_LE: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6171 result = (result <= 0); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6172 break; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6173 case Py_GE: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6174 result = (result >= 0); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6175 break; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6176 case Py_LT: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6177 result = (result == -1); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6178 break; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6179 case Py_GT: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6180 result = (result == 1); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6181 break; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6182 } |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6183 return PyBool_FromLong(result); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6184 |
| 51216 | 6185 onError: |
|
39487
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6186 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6187 /* Standard case |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6188 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6189 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
|
6190 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
|
6191 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
|
6192 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
|
6193 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
|
6194 chance. |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6195 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6196 */ |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6197 if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6198 PyErr_Clear(); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6199 Py_INCREF(Py_NotImplemented); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6200 return Py_NotImplemented; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6201 } |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6202 if (op != Py_EQ && op != Py_NE) |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6203 return NULL; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6204 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6205 /* Equality comparison. |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6206 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6207 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
|
6208 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
|
6209 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6210 */ |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6211 if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6212 return NULL; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6213 PyErr_Clear(); |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6214 if (PyErr_Warn(PyExc_UnicodeWarning, |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6215 (op == Py_EQ) ? |
|
39487
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6216 "Unicode equal comparison " |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6217 "failed to convert both arguments to Unicode - " |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6218 "interpreting them as being unequal" : |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6219 "Unicode unequal comparison " |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6220 "failed to convert both arguments to Unicode - " |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6221 "interpreting them as being unequal" |
| 51216 | 6222 ) < 0) |
|
39487
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6223 return NULL; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6224 result = (op == Py_NE); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6225 return PyBool_FromLong(result); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6226 } |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6227 |
|
11997
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6228 int PyUnicode_Contains(PyObject *container, |
| 51216 | 6229 PyObject *element) |
|
11997
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6230 { |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6231 PyObject *str, *sub; |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6232 int result; |
|
11997
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6233 |
|
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6234 /* Coerce the two arguments */ |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6235 sub = PyUnicode_FromObject(element); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6236 if (!sub) { |
|
38243
0ba711a496e6
needforspeed: fixed unicode "in" operator to use same implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38241
diff
changeset
|
6237 return -1; |
|
0ba711a496e6
needforspeed: fixed unicode "in" operator to use same implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38241
diff
changeset
|
6238 } |
|
0ba711a496e6
needforspeed: fixed unicode "in" operator to use same implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38241
diff
changeset
|
6239 |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6240 str = PyUnicode_FromObject(container); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6241 if (!str) { |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6242 Py_DECREF(sub); |
|
38243
0ba711a496e6
needforspeed: fixed unicode "in" operator to use same implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38241
diff
changeset
|
6243 return -1; |
|
0ba711a496e6
needforspeed: fixed unicode "in" operator to use same implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38241
diff
changeset
|
6244 } |
|
11997
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6245 |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6246 result = stringlib_contains_obj(str, sub); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6247 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6248 Py_DECREF(str); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6249 Py_DECREF(sub); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6250 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6251 return result; |
|
11997
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6252 } |
|
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6253 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6254 /* 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
|
6255 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6256 PyObject *PyUnicode_Concat(PyObject *left, |
| 51216 | 6257 PyObject *right) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6258 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6259 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
|
6260 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6261 /* 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
|
6262 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
|
6263 if (u == NULL) |
| 51216 | 6264 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6265 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
|
6266 if (v == NULL) |
| 51216 | 6267 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6268 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6269 /* Shortcuts */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6270 if (v == unicode_empty) { |
| 51216 | 6271 Py_DECREF(v); |
| 6272 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
|
6273 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6274 if (u == unicode_empty) { |
| 51216 | 6275 Py_DECREF(u); |
| 6276 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
|
6277 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6278 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6279 /* 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
|
6280 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
|
6281 if (w == NULL) |
| 51216 | 6282 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6283 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
|
6284 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
|
6285 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6286 Py_DECREF(u); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6287 Py_DECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6288 return (PyObject *)w; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6289 |
| 51216 | 6290 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6291 Py_XDECREF(u); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6292 Py_XDECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6293 return NULL; |
|
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 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6296 PyDoc_STRVAR(count__doc__, |
| 51216 | 6297 "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
|
6298 \n\ |
|
38236
7e16fcce34a1
docstring tweaks: count counts non-overlapping substrings, not
Fredrik Lundh <fredrik@pythonware.com>
parents:
38182
diff
changeset
|
6299 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
|
6300 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
|
6301 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
|
6302 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6303 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6304 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
|
6305 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6306 PyUnicodeObject *substring; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6307 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
|
6308 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
|
6309 PyObject *result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6310 |
|
69471
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
6311 if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
|
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
6312 &start, &end)) |
| 51216 | 6313 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
|
6314 |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
6315 ADJUST_INDICES(start, end, self->length); |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
6316 result = PyInt_FromSsize_t( |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
6317 stringlib_count(self->str + start, end - start, |
|
58288
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
6318 substring->str, substring->length, |
|
66f1d4a4c799
Issue #7622: Improve the split(), rsplit(), splitlines() and replace()
Antoine Pitrou <solipsis@pitrou.net>
parents:
57729
diff
changeset
|
6319 PY_SSIZE_T_MAX) |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
6320 ); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6321 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6322 Py_DECREF(substring); |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
6323 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6324 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6325 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6326 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6327 PyDoc_STRVAR(encode__doc__, |
| 51216 | 6328 "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
|
6329 \n\ |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6330 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
|
6331 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
|
6332 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
|
6333 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
|
6334 '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
|
6335 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
|
6336 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6337 static PyObject * |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56033
diff
changeset
|
6338 unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
|
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56033
diff
changeset
|
6339 { |
|
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56033
diff
changeset
|
6340 static char *kwlist[] = {"encoding", "errors", 0}; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6341 char *encoding = NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6342 char *errors = NULL; |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6343 PyObject *v; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6344 |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56033
diff
changeset
|
6345 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
|
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56033
diff
changeset
|
6346 kwlist, &encoding, &errors)) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6347 return NULL; |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6348 v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6349 if (v == NULL) |
|
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6350 goto onError; |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6351 if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6352 PyErr_Format(PyExc_TypeError, |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6353 "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
|
6354 "(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
|
6355 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
|
6356 Py_DECREF(v); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6357 return NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6358 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6359 return v; |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6360 |
| 51216 | 6361 onError: |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6362 return NULL; |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6363 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6364 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6365 PyDoc_STRVAR(decode__doc__, |
| 51216 | 6366 "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
|
6367 \n\ |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6368 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
|
6369 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
|
6370 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
|
6371 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
|
6372 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
|
6373 able to handle UnicodeDecodeErrors."); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6374 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6375 static PyObject * |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56033
diff
changeset
|
6376 unicode_decode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
|
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56033
diff
changeset
|
6377 { |
|
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56033
diff
changeset
|
6378 static char *kwlist[] = {"encoding", "errors", 0}; |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6379 char *encoding = NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6380 char *errors = NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6381 PyObject *v; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6382 |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56033
diff
changeset
|
6383 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:decode", |
|
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56033
diff
changeset
|
6384 kwlist, &encoding, &errors)) |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6385 return NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6386 v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6387 if (v == NULL) |
|
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6388 goto onError; |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6389 if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6390 PyErr_Format(PyExc_TypeError, |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6391 "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
|
6392 "(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
|
6393 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
|
6394 Py_DECREF(v); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6395 return NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6396 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6397 return v; |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6398 |
| 51216 | 6399 onError: |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6400 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6401 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6402 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6403 PyDoc_STRVAR(expandtabs__doc__, |
| 51216 | 6404 "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
|
6405 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6406 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
|
6407 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
|
6408 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6409 static PyObject* |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6410 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
|
6411 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6412 Py_UNICODE *e; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6413 Py_UNICODE *p; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6414 Py_UNICODE *q; |
|
45730
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6415 Py_UNICODE *qe; |
|
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6416 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
|
6417 PyUnicodeObject *u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6418 int tabsize = 8; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6419 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6420 if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 51216 | 6421 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6422 |
|
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
|
6423 /* First pass: determine size of output string */ |
|
45730
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6424 i = 0; /* chars up to and including most recent \n or \r */ |
|
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6425 j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
|
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6426 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
|
6427 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
|
6428 if (*p == '\t') { |
| 51216 | 6429 if (tabsize > 0) { |
| 6430 incr = tabsize - (j % tabsize); /* cannot overflow */ | |
| 6431 if (j > PY_SSIZE_T_MAX - incr) | |
| 6432 goto overflow1; | |
| 6433 j += incr; | |
|
45730
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6434 } |
| 51216 | 6435 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6436 else { |
| 51216 | 6437 if (j > PY_SSIZE_T_MAX - 1) |
| 6438 goto overflow1; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6439 j++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6440 if (*p == '\n' || *p == '\r') { |
| 51216 | 6441 if (i > PY_SSIZE_T_MAX - j) |
| 6442 goto overflow1; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6443 i += j; |
|
45730
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6444 j = 0; |
|
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 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6447 |
|
45730
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6448 if (i > PY_SSIZE_T_MAX - j) |
| 51216 | 6449 goto overflow1; |
|
42005
2bd9417dacd5
Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents:
39994
diff
changeset
|
6450 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6451 /* 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
|
6452 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
|
6453 if (!u) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6454 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6455 |
|
45730
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6456 j = 0; /* same as in first pass */ |
|
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6457 q = u->str; /* next output char */ |
|
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6458 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
|
6459 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6460 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
|
6461 if (*p == '\t') { |
| 51216 | 6462 if (tabsize > 0) { |
| 6463 i = tabsize - (j % tabsize); | |
| 6464 j += i; | |
| 6465 while (i--) { | |
| 6466 if (q >= qe) | |
| 6467 goto overflow2; | |
| 6468 *q++ = ' '; | |
|
45730
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6469 } |
| 51216 | 6470 } |
| 6471 } | |
| 6472 else { | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6473 if (q >= qe) |
| 51216 | 6474 goto overflow2; |
| 6475 *q++ = *p; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6476 j++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6477 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
|
6478 j = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6479 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6480 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6481 return (PyObject*) u; |
|
45730
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6482 |
|
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6483 overflow2: |
|
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6484 Py_DECREF(u); |
|
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6485 overflow1: |
|
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6486 PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
|
f7c3658c71c3
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
43754
diff
changeset
|
6487 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6488 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6489 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6490 PyDoc_STRVAR(find__doc__, |
| 51216 | 6491 "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
|
6492 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6493 Return the lowest index in S where substring sub is found,\n\ |
|
71529
e266415cf42c
merge from 3.2 - Fix closes Issue12621 - Fix docstrings of find and rfind methods of bytes/bytearry/unicodeobject.
Senthil Kumaran <senthil@uthcode.com>
parents:
69562
diff
changeset
|
6494 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
|
6495 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
|
6496 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6497 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
|
6498 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6499 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6500 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
|
6501 { |
|
69471
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
6502 PyUnicodeObject *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
|
6503 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
|
6504 Py_ssize_t end; |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6505 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
|
6506 |
|
69471
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
6507 if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
|
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
6508 &start, &end)) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6509 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6510 |
|
38441
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
6511 result = stringlib_find_slice( |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
6512 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
|
6513 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
|
6514 start, end |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
6515 ); |
|
11968
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 Py_DECREF(substring); |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6518 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6519 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
|
6520 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6521 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6522 static PyObject * |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6523 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
|
6524 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6525 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
|
6526 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
|
6527 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6528 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6529 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6530 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
|
6531 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6532 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6533 static long |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6534 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
|
6535 { |
|
13522
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6536 /* 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
|
6537 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
|
6538 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
|
6539 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
|
6540 dictionary keys. */ |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6541 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6542 register Py_ssize_t len; |
|
13522
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6543 register Py_UNICODE *p; |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6544 register long x; |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6545 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6546 if (self->hash != -1) |
| 51216 | 6547 return self->hash; |
|
13522
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6548 len = PyUnicode_GET_SIZE(self); |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6549 p = PyUnicode_AS_UNICODE(self); |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6550 x = *p << 7; |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6551 while (--len >= 0) |
| 51216 | 6552 x = (1000003*x) ^ *p++; |
|
13522
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6553 x ^= PyUnicode_GET_SIZE(self); |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6554 if (x == -1) |
| 51216 | 6555 x = -2; |
|
13522
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6556 self->hash = x; |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6557 return x; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6558 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6559 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6560 PyDoc_STRVAR(index__doc__, |
| 51216 | 6561 "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
|
6562 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6563 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
|
6564 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6565 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6566 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
|
6567 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6568 Py_ssize_t result; |
|
69471
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
6569 PyUnicodeObject *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
|
6570 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
|
6571 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
|
6572 |
|
69471
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
6573 if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
|
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
6574 &start, &end)) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6575 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6576 |
|
38441
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
6577 result = stringlib_find_slice( |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
6578 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
|
6579 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
|
6580 start, end |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
6581 ); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6582 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6583 Py_DECREF(substring); |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6584 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6585 if (result < 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6586 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
|
6587 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6588 } |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6589 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6590 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
|
6591 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6592 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6593 PyDoc_STRVAR(islower__doc__, |
| 51216 | 6594 "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
|
6595 \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
|
6596 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
|
6597 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
|
6598 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6599 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
|
6600 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
|
6601 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6602 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
|
6603 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
|
6604 int cased; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6605 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6606 /* 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
|
6607 if (PyUnicode_GET_SIZE(self) == 1) |
| 51216 | 6608 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
|
6609 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6610 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6611 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6612 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6613 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6614 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
|
6615 cased = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6616 for (; p < e; p++) { |
| 51216 | 6617 register const Py_UNICODE ch = *p; |
| 6618 | |
| 6619 if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) | |
| 6620 return PyBool_FromLong(0); | |
| 6621 else if (!cased && Py_UNICODE_ISLOWER(ch)) | |
| 6622 cased = 1; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6623 } |
|
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
|
6624 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
|
6625 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6626 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6627 PyDoc_STRVAR(isupper__doc__, |
| 51216 | 6628 "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
|
6629 \n\ |
|
30707
9e21e56bf5f3
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30653
diff
changeset
|
6630 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
|
6631 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
|
6632 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6633 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
|
6634 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
|
6635 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6636 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
|
6637 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
|
6638 int cased; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6639 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6640 /* 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
|
6641 if (PyUnicode_GET_SIZE(self) == 1) |
| 51216 | 6642 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
|
6643 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6644 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6645 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6646 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6647 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6648 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
|
6649 cased = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6650 for (; p < e; p++) { |
| 51216 | 6651 register const Py_UNICODE ch = *p; |
| 6652 | |
| 6653 if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) | |
| 6654 return PyBool_FromLong(0); | |
| 6655 else if (!cased && Py_UNICODE_ISUPPER(ch)) | |
| 6656 cased = 1; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6657 } |
|
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
|
6658 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
|
6659 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6660 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6661 PyDoc_STRVAR(istitle__doc__, |
| 51216 | 6662 "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
|
6663 \n\ |
|
30707
9e21e56bf5f3
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30653
diff
changeset
|
6664 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
|
6665 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
|
6666 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
|
6667 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
|
6668 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6669 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
|
6670 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
|
6671 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6672 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
|
6673 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
|
6674 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
|
6675 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6676 /* 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
|
6677 if (PyUnicode_GET_SIZE(self) == 1) |
| 51216 | 6678 return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 6679 (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
|
6680 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6681 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6682 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6683 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6684 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6685 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
|
6686 cased = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6687 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
|
6688 for (; p < e; p++) { |
| 51216 | 6689 register const Py_UNICODE ch = *p; |
| 6690 | |
| 6691 if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { | |
| 6692 if (previous_is_cased) | |
| 6693 return PyBool_FromLong(0); | |
| 6694 previous_is_cased = 1; | |
| 6695 cased = 1; | |
| 6696 } | |
| 6697 else if (Py_UNICODE_ISLOWER(ch)) { | |
| 6698 if (!previous_is_cased) | |
| 6699 return PyBool_FromLong(0); | |
| 6700 previous_is_cased = 1; | |
| 6701 cased = 1; | |
| 6702 } | |
| 6703 else | |
| 6704 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
|
6705 } |
|
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
|
6706 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
|
6707 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6708 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6709 PyDoc_STRVAR(isspace__doc__, |
| 51216 | 6710 "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
|
6711 \n\ |
|
30707
9e21e56bf5f3
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30653
diff
changeset
|
6712 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
|
6713 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
|
6714 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6715 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
|
6716 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
|
6717 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6718 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
|
6719 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
|
6720 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6721 /* 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
|
6722 if (PyUnicode_GET_SIZE(self) == 1 && |
| 51216 | 6723 Py_UNICODE_ISSPACE(*p)) |
| 6724 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
|
6725 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6726 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6727 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6728 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6729 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6730 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
|
6731 for (; p < e; p++) { |
| 51216 | 6732 if (!Py_UNICODE_ISSPACE(*p)) |
| 6733 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
|
6734 } |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6735 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
|
6736 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6737 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6738 PyDoc_STRVAR(isalpha__doc__, |
| 51216 | 6739 "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
|
6740 \n\ |
|
30707
9e21e56bf5f3
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30653
diff
changeset
|
6741 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
|
6742 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
|
6743 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6744 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
|
6745 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
|
6746 { |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6747 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
|
6748 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
|
6749 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6750 /* 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
|
6751 if (PyUnicode_GET_SIZE(self) == 1 && |
| 51216 | 6752 Py_UNICODE_ISALPHA(*p)) |
| 6753 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
|
6754 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6755 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6756 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6757 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
|
6758 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6759 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
|
6760 for (; p < e; p++) { |
| 51216 | 6761 if (!Py_UNICODE_ISALPHA(*p)) |
| 6762 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
|
6763 } |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6764 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
|
6765 } |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6766 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6767 PyDoc_STRVAR(isalnum__doc__, |
| 51216 | 6768 "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
|
6769 \n\ |
|
30707
9e21e56bf5f3
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30653
diff
changeset
|
6770 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
|
6771 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
|
6772 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6773 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
|
6774 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
|
6775 { |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6776 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
|
6777 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
|
6778 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6779 /* 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
|
6780 if (PyUnicode_GET_SIZE(self) == 1 && |
| 51216 | 6781 Py_UNICODE_ISALNUM(*p)) |
| 6782 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
|
6783 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6784 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6785 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6786 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
|
6787 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6788 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
|
6789 for (; p < e; p++) { |
| 51216 | 6790 if (!Py_UNICODE_ISALNUM(*p)) |
| 6791 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
|
6792 } |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6793 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
|
6794 } |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6795 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6796 PyDoc_STRVAR(isdecimal__doc__, |
| 51216 | 6797 "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
|
6798 \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
|
6799 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
|
6800 False otherwise."); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6801 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6802 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
|
6803 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
|
6804 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6805 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
|
6806 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
|
6807 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6808 /* 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
|
6809 if (PyUnicode_GET_SIZE(self) == 1 && |
| 51216 | 6810 Py_UNICODE_ISDECIMAL(*p)) |
| 6811 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
|
6812 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6813 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6814 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6815 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6816 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6817 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
|
6818 for (; p < e; p++) { |
| 51216 | 6819 if (!Py_UNICODE_ISDECIMAL(*p)) |
| 6820 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
|
6821 } |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6822 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
|
6823 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6824 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6825 PyDoc_STRVAR(isdigit__doc__, |
| 51216 | 6826 "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
|
6827 \n\ |
|
30707
9e21e56bf5f3
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30653
diff
changeset
|
6828 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
|
6829 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
|
6830 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6831 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
|
6832 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
|
6833 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6834 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
|
6835 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
|
6836 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6837 /* 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
|
6838 if (PyUnicode_GET_SIZE(self) == 1 && |
| 51216 | 6839 Py_UNICODE_ISDIGIT(*p)) |
| 6840 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
|
6841 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6842 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6843 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6844 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6845 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6846 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
|
6847 for (; p < e; p++) { |
| 51216 | 6848 if (!Py_UNICODE_ISDIGIT(*p)) |
| 6849 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
|
6850 } |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6851 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
|
6852 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6853 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6854 PyDoc_STRVAR(isnumeric__doc__, |
| 51216 | 6855 "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
|
6856 \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
|
6857 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
|
6858 False otherwise."); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6859 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6860 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
|
6861 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
|
6862 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6863 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
|
6864 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
|
6865 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6866 /* 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
|
6867 if (PyUnicode_GET_SIZE(self) == 1 && |
| 51216 | 6868 Py_UNICODE_ISNUMERIC(*p)) |
| 6869 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
|
6870 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6871 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6872 if (PyUnicode_GET_SIZE(self) == 0) |
| 51216 | 6873 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6874 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6875 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
|
6876 for (; p < e; p++) { |
| 51216 | 6877 if (!Py_UNICODE_ISNUMERIC(*p)) |
| 6878 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
|
6879 } |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6880 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
|
6881 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6882 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6883 PyDoc_STRVAR(join__doc__, |
|
56824
b7b8970cd5a5
Merged revisions 75363,75365,75376,75392,75394,75403,75418,75484,75572,75580,75590,75592,75594-75596,75600,75602-75603,75605-75607,75610-75613,75616-75617,75623,75627,75647 via svnmerge from
Georg Brandl <georg@python.org>
parents:
56812
diff
changeset
|
6884 "S.join(iterable) -> unicode\n\ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6885 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6886 Return a string which is the concatenation of the strings in the\n\ |
|
56824
b7b8970cd5a5
Merged revisions 75363,75365,75376,75392,75394,75403,75418,75484,75572,75580,75590,75592,75594-75596,75600,75602-75603,75605-75607,75610-75613,75616-75617,75623,75627,75647 via svnmerge from
Georg Brandl <georg@python.org>
parents:
56812
diff
changeset
|
6887 iterable. 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
|
6888 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6889 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
|
6890 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
|
6891 { |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19121
diff
changeset
|
6892 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
|
6893 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6894 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6895 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
|
6896 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
|
6897 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6898 return self->length; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6899 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6900 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6901 PyDoc_STRVAR(ljust__doc__, |
| 51216 | 6902 "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
|
6903 \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
|
6904 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
|
6905 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
|
6906 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6907 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6908 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
|
6909 { |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
6910 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
|
6911 Py_UNICODE fillchar = ' '; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6912 |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
6913 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
|
6914 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6915 |
|
19885
72e6eb653312
More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19849
diff
changeset
|
6916 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
|
6917 Py_INCREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6918 return (PyObject*) self; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6919 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6920 |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6921 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
|
6922 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6923 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6924 PyDoc_STRVAR(lower__doc__, |
| 51216 | 6925 "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
|
6926 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6927 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
|
6928 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6929 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
|
6930 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
|
6931 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6932 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
|
6933 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6934 |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
6935 #define LEFTSTRIP 0 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
6936 #define RIGHTSTRIP 1 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
6937 #define BOTHSTRIP 2 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
6938 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
6939 /* Arrays indexed by above */ |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
6940 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
|
6941 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
6942 #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
|
6943 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
6944 /* 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
|
6945 PyObject * |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
6946 _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
|
6947 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6948 Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6949 Py_ssize_t len = PyUnicode_GET_SIZE(self); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6950 Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6951 Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6952 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
|
6953 |
| 51216 | 6954 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
|
6955 |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6956 i = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6957 if (striptype != RIGHTSTRIP) { |
| 51216 | 6958 while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 6959 i++; | |
| 6960 } | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6961 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6962 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6963 j = len; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6964 if (striptype != LEFTSTRIP) { |
| 51216 | 6965 do { |
| 6966 j--; | |
| 6967 } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); | |
| 6968 j++; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6969 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6970 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6971 if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 51216 | 6972 Py_INCREF(self); |
| 6973 return (PyObject*)self; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6974 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6975 else |
| 51216 | 6976 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
|
6977 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
6978 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6979 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6980 static PyObject * |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
6981 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
|
6982 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6983 Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6984 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
|
6985 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6986 i = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6987 if (striptype != RIGHTSTRIP) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6988 while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6989 i++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6990 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6991 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6992 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6993 j = len; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6994 if (striptype != LEFTSTRIP) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6995 do { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6996 j--; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6997 } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6998 j++; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
6999 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7000 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7001 if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7002 Py_INCREF(self); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7003 return (PyObject*)self; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7004 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7005 else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7006 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
|
7007 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7008 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7009 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7010 static PyObject * |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7011 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
|
7012 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7013 PyObject *sep = NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7014 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7015 if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7016 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7017 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7018 if (sep != NULL && sep != Py_None) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7019 if (PyUnicode_Check(sep)) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7020 return _PyUnicode_XStrip(self, striptype, sep); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7021 else if (PyString_Check(sep)) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7022 PyObject *res; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7023 sep = PyUnicode_FromObject(sep); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7024 if (sep==NULL) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7025 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7026 res = _PyUnicode_XStrip(self, striptype, sep); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7027 Py_DECREF(sep); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7028 return res; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7029 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7030 else { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7031 PyErr_Format(PyExc_TypeError, |
| 51216 | 7032 "%s arg must be None, unicode or str", |
| 7033 STRIPNAME(striptype)); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7034 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7035 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7036 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7037 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7038 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
|
7039 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7040 |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7041 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7042 PyDoc_STRVAR(strip__doc__, |
| 51216 | 7043 "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
|
7044 \n\ |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7045 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
|
7046 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
|
7047 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
|
7048 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
|
7049 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7050 static PyObject * |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7051 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
|
7052 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7053 if (PyTuple_GET_SIZE(args) == 0) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7054 return do_strip(self, BOTHSTRIP); /* Common case */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7055 else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7056 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
|
7057 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7058 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7059 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7060 PyDoc_STRVAR(lstrip__doc__, |
| 51216 | 7061 "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
|
7062 \n\ |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7063 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
|
7064 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
|
7065 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
|
7066 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7067 static PyObject * |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7068 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
|
7069 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7070 if (PyTuple_GET_SIZE(args) == 0) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7071 return do_strip(self, LEFTSTRIP); /* Common case */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7072 else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7073 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
|
7074 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7075 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7076 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7077 PyDoc_STRVAR(rstrip__doc__, |
| 51216 | 7078 "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
|
7079 \n\ |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7080 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
|
7081 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
|
7082 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
|
7083 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7084 static PyObject * |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7085 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
|
7086 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7087 if (PyTuple_GET_SIZE(args) == 0) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7088 return do_strip(self, RIGHTSTRIP); /* Common case */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7089 else |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7090 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
|
7091 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7092 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7093 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7094 static PyObject* |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7095 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
|
7096 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7097 PyUnicodeObject *u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7098 Py_UNICODE *p; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7099 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
|
7100 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
|
7101 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7102 if (len < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7103 len = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7104 |
|
19885
72e6eb653312
More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19849
diff
changeset
|
7105 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
|
7106 /* 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
|
7107 Py_INCREF(str); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7108 return (PyObject*) str; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7109 } |
|
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
|
7110 |
|
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
|
7111 /* 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
|
7112 * 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
|
7113 */ |
|
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
|
7114 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
|
7115 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
|
7116 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
|
7117 "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
|
7118 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
|
7119 } |
|
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
|
7120 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
|
7121 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
|
7122 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
|
7123 "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
|
7124 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
|
7125 } |
|
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
|
7126 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
|
7127 if (!u) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7128 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7129 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7130 p = u->str; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7131 |
|
38238
feb00df91922
needforspeed: speed up unicode repeat, unicode string copy
Fredrik Lundh <fredrik@pythonware.com>
parents:
38236
diff
changeset
|
7132 if (str->length == 1 && len > 0) { |
|
feb00df91922
needforspeed: speed up unicode repeat, unicode string copy
Fredrik Lundh <fredrik@pythonware.com>
parents:
38236
diff
changeset
|
7133 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
|
7134 } else { |
| 51216 | 7135 Py_ssize_t done = 0; /* number of characters copied this far */ |
| 7136 if (done < nchars) { | |
|
38238
feb00df91922
needforspeed: speed up unicode repeat, unicode string copy
Fredrik Lundh <fredrik@pythonware.com>
parents:
38236
diff
changeset
|
7137 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
|
7138 done = str->length; |
| 51216 | 7139 } |
| 7140 while (done < nchars) { | |
|
46062
09adff3d6691
Try to fix a bunch of compiler warnings on Win64.
Neal Norwitz <nnorwitz@gmail.com>
parents:
46059
diff
changeset
|
7141 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
|
7142 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
|
7143 done += n; |
| 51216 | 7144 } |
|
38239
604af867c65f
needforspeed: use memcpy for "long" strings; use a better algorithm
Fredrik Lundh <fredrik@pythonware.com>
parents:
38238
diff
changeset
|
7145 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7146 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7147 return (PyObject*) u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7148 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7149 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7150 PyObject *PyUnicode_Replace(PyObject *obj, |
| 51216 | 7151 PyObject *subobj, |
| 7152 PyObject *replobj, | |
| 7153 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
|
7154 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7155 PyObject *self; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7156 PyObject *str1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7157 PyObject *str2; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7158 PyObject *result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7159 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7160 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
|
7161 if (self == NULL) |
| 51216 | 7162 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7163 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
|
7164 if (str1 == NULL) { |
| 51216 | 7165 Py_DECREF(self); |
| 7166 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7167 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7168 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
|
7169 if (str2 == NULL) { |
| 51216 | 7170 Py_DECREF(self); |
| 7171 Py_DECREF(str1); | |
| 7172 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7173 } |
|
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
|
7174 result = replace((PyUnicodeObject *)self, |
| 51216 | 7175 (PyUnicodeObject *)str1, |
| 7176 (PyUnicodeObject *)str2, | |
| 7177 maxcount); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7178 Py_DECREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7179 Py_DECREF(str1); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7180 Py_DECREF(str2); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7181 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7182 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7183 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7184 PyDoc_STRVAR(replace__doc__, |
|
62520
014e7c00dc28
Merged revisions 82248 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62257
diff
changeset
|
7185 "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
|
7186 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7187 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
|
7188 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
|
7189 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
|
7190 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7191 static PyObject* |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7192 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
|
7193 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7194 PyUnicodeObject *str1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7195 PyUnicodeObject *str2; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7196 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
|
7197 PyObject *result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7198 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7199 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
|
7200 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7201 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
|
7202 if (str1 == NULL) |
| 51216 | 7203 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7204 str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
|
27774
bfc524379307
Fix two refcounting bugs
Walter Dörwald <walter@livinglogic.de>
parents:
27555
diff
changeset
|
7205 if (str2 == NULL) { |
| 51216 | 7206 Py_DECREF(str1); |
| 7207 return NULL; | |
|
27774
bfc524379307
Fix two refcounting bugs
Walter Dörwald <walter@livinglogic.de>
parents:
27555
diff
changeset
|
7208 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7209 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7210 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
|
7211 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7212 Py_DECREF(str1); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7213 Py_DECREF(str2); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7214 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7215 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7216 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7217 static |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7218 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
|
7219 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7220 return unicodeescape_string(PyUnicode_AS_UNICODE(unicode), |
| 51216 | 7221 PyUnicode_GET_SIZE(unicode), |
| 7222 1); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7223 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7224 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7225 PyDoc_STRVAR(rfind__doc__, |
| 51216 | 7226 "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
|
7227 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7228 Return the highest index in S where substring sub is found,\n\ |
|
71529
e266415cf42c
merge from 3.2 - Fix closes Issue12621 - Fix docstrings of find and rfind methods of bytes/bytearry/unicodeobject.
Senthil Kumaran <senthil@uthcode.com>
parents:
69562
diff
changeset
|
7229 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
|
7230 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
|
7231 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7232 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
|
7233 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7234 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7235 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
|
7236 { |
|
69471
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
7237 PyUnicodeObject *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
|
7238 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
|
7239 Py_ssize_t end; |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
7240 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
|
7241 |
|
69471
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
7242 if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
|
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
7243 &start, &end)) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7244 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7245 |
|
38441
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
7246 result = stringlib_rfind_slice( |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
7247 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
|
7248 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
|
7249 start, end |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
7250 ); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7251 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7252 Py_DECREF(substring); |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
7253 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
7254 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
|
7255 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7256 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7257 PyDoc_STRVAR(rindex__doc__, |
| 51216 | 7258 "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
|
7259 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7260 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
|
7261 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7262 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7263 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
|
7264 { |
|
69471
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
7265 PyUnicodeObject *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
|
7266 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
|
7267 Py_ssize_t end; |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
7268 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
|
7269 |
|
69471
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
7270 if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
|
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
7271 &start, &end)) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7272 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7273 |
|
38441
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
7274 result = stringlib_rfind_slice( |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
7275 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
|
7276 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
|
7277 start, end |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
7278 ); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7279 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7280 Py_DECREF(substring); |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
7281 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7282 if (result < 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7283 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
|
7284 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7285 } |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7286 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
|
7287 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7288 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7289 PyDoc_STRVAR(rjust__doc__, |
| 51216 | 7290 "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
|
7291 \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
|
7292 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
|
7293 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
|
7294 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7295 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7296 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
|
7297 { |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
7298 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
|
7299 Py_UNICODE fillchar = ' '; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
7300 |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
7301 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
|
7302 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7303 |
|
19885
72e6eb653312
More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19849
diff
changeset
|
7304 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
|
7305 Py_INCREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7306 return (PyObject*) self; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7307 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7308 |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
7309 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
|
7310 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7311 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7312 static PyObject* |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7313 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
|
7314 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7315 /* standard clamping */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7316 if (start < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7317 start = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7318 if (end < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7319 end = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7320 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
|
7321 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
|
7322 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
|
7323 /* 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
|
7324 Py_INCREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7325 return (PyObject*) self; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7326 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7327 if (start > end) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7328 start = end; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7329 /* copy slice */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7330 return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
| 51216 | 7331 end - start); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7332 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7333 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7334 PyObject *PyUnicode_Split(PyObject *s, |
| 51216 | 7335 PyObject *sep, |
| 7336 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
|
7337 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7338 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
|
7339 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7340 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
|
7341 if (s == NULL) |
| 51216 | 7342 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7343 if (sep != NULL) { |
| 51216 | 7344 sep = PyUnicode_FromObject(sep); |
| 7345 if (sep == NULL) { | |
| 7346 Py_DECREF(s); | |
| 7347 return NULL; | |
| 7348 } | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7349 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7350 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7351 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
|
7352 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7353 Py_DECREF(s); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7354 Py_XDECREF(sep); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7355 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7356 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7357 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7358 PyDoc_STRVAR(split__doc__, |
| 51216 | 7359 "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
|
7360 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7361 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
|
7362 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
|
7363 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
|
7364 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
|
7365 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
|
7366 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7367 static PyObject* |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7368 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
|
7369 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7370 PyObject *substring = Py_None; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7371 Py_ssize_t maxcount = -1; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7372 |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7373 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
|
7374 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7375 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7376 if (substring == Py_None) |
| 51216 | 7377 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
|
7378 else if (PyUnicode_Check(substring)) |
| 51216 | 7379 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
|
7380 else |
| 51216 | 7381 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
|
7382 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7383 |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7384 PyObject * |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7385 PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7386 { |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7387 PyObject* str_obj; |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7388 PyObject* sep_obj; |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7389 PyObject* out; |
|
38383
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7390 |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7391 str_obj = PyUnicode_FromObject(str_in); |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7392 if (!str_obj) |
| 51216 | 7393 return NULL; |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7394 sep_obj = PyUnicode_FromObject(sep_in); |
|
38383
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7395 if (!sep_obj) { |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7396 Py_DECREF(str_obj); |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7397 return NULL; |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7398 } |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7399 |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
7400 out = stringlib_partition( |
|
38383
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7401 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
|
7402 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
|
7403 ); |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7404 |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7405 Py_DECREF(sep_obj); |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7406 Py_DECREF(str_obj); |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7407 |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7408 return out; |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7409 } |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7410 |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7411 |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7412 PyObject * |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7413 PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7414 { |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7415 PyObject* str_obj; |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7416 PyObject* sep_obj; |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7417 PyObject* out; |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7418 |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7419 str_obj = PyUnicode_FromObject(str_in); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7420 if (!str_obj) |
| 51216 | 7421 return NULL; |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7422 sep_obj = PyUnicode_FromObject(sep_in); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7423 if (!sep_obj) { |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7424 Py_DECREF(str_obj); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7425 return NULL; |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7426 } |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7427 |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
7428 out = stringlib_rpartition( |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7429 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
|
7430 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
|
7431 ); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7432 |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7433 Py_DECREF(sep_obj); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7434 Py_DECREF(str_obj); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7435 |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7436 return out; |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7437 } |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7438 |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7439 PyDoc_STRVAR(partition__doc__, |
| 51216 | 7440 "S.partition(sep) -> (head, sep, tail)\n\ |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7441 \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
|
7442 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
|
7443 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
|
7444 found, return S and two empty strings."); |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7445 |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7446 static PyObject* |
|
38338
958f91684209
needforspeed: use METH_O for argument handling, which made partition some
Fredrik Lundh <fredrik@pythonware.com>
parents:
38335
diff
changeset
|
7447 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
|
7448 { |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7449 return PyUnicode_Partition((PyObject *)self, separator); |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7450 } |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7451 |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7452 PyDoc_STRVAR(rpartition__doc__, |
|
58488
ff66324f52a1
Merged revisions 77743 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56824
diff
changeset
|
7453 "S.rpartition(sep) -> (head, sep, tail)\n\ |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7454 \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
|
7455 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
|
7456 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
|
7457 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
|
7458 |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7459 static PyObject* |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7460 unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7461 { |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7462 return PyUnicode_RPartition((PyObject *)self, separator); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7463 } |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7464 |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7465 PyObject *PyUnicode_RSplit(PyObject *s, |
| 51216 | 7466 PyObject *sep, |
| 7467 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
|
7468 { |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7469 PyObject *result; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7470 |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7471 s = PyUnicode_FromObject(s); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7472 if (s == NULL) |
| 51216 | 7473 return NULL; |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7474 if (sep != NULL) { |
| 51216 | 7475 sep = PyUnicode_FromObject(sep); |
| 7476 if (sep == NULL) { | |
| 7477 Py_DECREF(s); | |
| 7478 return NULL; | |
| 7479 } | |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7480 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7481 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7482 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
|
7483 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7484 Py_DECREF(s); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7485 Py_XDECREF(sep); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7486 return result; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7487 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7488 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7489 PyDoc_STRVAR(rsplit__doc__, |
| 51216 | 7490 "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
|
7491 \n\ |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7492 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
|
7493 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
|
7494 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
|
7495 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
|
7496 is a separator."); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7497 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7498 static PyObject* |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7499 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
|
7500 { |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7501 PyObject *substring = Py_None; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7502 Py_ssize_t maxcount = -1; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7503 |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7504 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
|
7505 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7506 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7507 if (substring == Py_None) |
| 51216 | 7508 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
|
7509 else if (PyUnicode_Check(substring)) |
| 51216 | 7510 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
|
7511 else |
| 51216 | 7512 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
|
7513 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7514 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7515 PyDoc_STRVAR(splitlines__doc__, |
| 51216 | 7516 "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
|
7517 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7518 Return a list of the lines in S, breaking at line boundaries.\n\ |
| 12369 | 7519 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
|
7520 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
|
7521 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7522 static PyObject* |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7523 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
|
7524 { |
| 12369 | 7525 int keepends = 0; |
| 7526 | |
| 7527 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
|
7528 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7529 |
| 12369 | 7530 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
|
7531 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7532 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7533 static |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7534 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
|
7535 { |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
7536 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
|
7537 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7538 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7539 PyDoc_STRVAR(swapcase__doc__, |
| 51216 | 7540 "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
|
7541 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7542 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
|
7543 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
|
7544 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7545 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
|
7546 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
|
7547 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7548 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
|
7549 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7550 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7551 PyDoc_STRVAR(translate__doc__, |
| 51216 | 7552 "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
|
7553 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7554 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
|
7555 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
|
7556 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
|
7557 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
|
7558 are deleted."); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7559 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7560 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
|
7561 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
|
7562 { |
|
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
|
7563 return PyUnicode_TranslateCharmap(self->str, |
| 51216 | 7564 self->length, |
| 7565 table, | |
| 7566 "ignore"); | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7567 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7568 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7569 PyDoc_STRVAR(upper__doc__, |
| 51216 | 7570 "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
|
7571 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7572 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
|
7573 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7574 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
|
7575 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
|
7576 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7577 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
|
7578 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7579 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7580 PyDoc_STRVAR(zfill__doc__, |
| 51216 | 7581 "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
|
7582 \n\ |
|
49115
6f08278bf8b1
Fix varname in docstring. #3822.
Georg Brandl <georg@python.org>
parents:
48117
diff
changeset
|
7583 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
|
7584 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
|
7585 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7586 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7587 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
|
7588 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7589 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
|
7590 PyUnicodeObject *u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7591 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7592 Py_ssize_t width; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7593 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
|
7594 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7595 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7596 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
|
7597 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
|
7598 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
|
7599 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
|
7600 } |
|
19ea00feda33
Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents:
23045
diff
changeset
|
7601 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
|
7602 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
|
7603 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
|
7604 PyUnicode_GET_SIZE(self) |
| 51216 | 7605 ); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7606 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7607 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7608 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
|
7609 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7610 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
|
7611 |
|
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
|
7612 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
|
7613 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
|
7614 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7615 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
|
7616 /* 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
|
7617 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
|
7618 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
|
7619 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7620 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7621 return (PyObject*) u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7622 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7623 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7624 #if 0 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7625 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
|
7626 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
|
7627 { |
|
920a7633ae08
Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents:
45053
diff
changeset
|
7628 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
|
7629 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7630 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7631 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7632 PyDoc_STRVAR(startswith__doc__, |
| 51216 | 7633 "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
|
7634 \n\ |
|
28559
aff080f2efb4
Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents:
28494
diff
changeset
|
7635 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
|
7636 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
|
7637 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
|
7638 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
|
7639 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7640 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7641 unicode_startswith(PyUnicodeObject *self, |
| 51216 | 7642 PyObject *args) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7643 { |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7644 PyObject *subobj; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7645 PyUnicodeObject *substring; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7646 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
|
7647 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
|
7648 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
|
7649 |
|
69471
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
7650 if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
| 51216 | 7651 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
|
7652 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
|
7653 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
|
7654 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
|
7655 substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 51216 | 7656 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
|
7657 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
|
7658 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
|
7659 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
|
7660 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
|
7661 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
|
7662 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
|
7663 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7664 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7665 /* 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
|
7666 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
|
7667 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7668 substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
|
69562
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
69471
diff
changeset
|
7669 if (substring == NULL) { |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
69471
diff
changeset
|
7670 if (PyErr_ExceptionMatches(PyExc_TypeError)) |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
69471
diff
changeset
|
7671 PyErr_Format(PyExc_TypeError, "startswith first arg must be str, " |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
69471
diff
changeset
|
7672 "unicode, or tuple, not %s", Py_TYPE(subobj)->tp_name); |
| 51216 | 7673 return NULL; |
|
69562
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
69471
diff
changeset
|
7674 } |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7675 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
|
7676 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
|
7677 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
|
7678 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7679 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7680 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7681 PyDoc_STRVAR(endswith__doc__, |
| 51216 | 7682 "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
|
7683 \n\ |
|
28559
aff080f2efb4
Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents:
28494
diff
changeset
|
7684 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
|
7685 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
|
7686 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
|
7687 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
|
7688 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7689 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7690 unicode_endswith(PyUnicodeObject *self, |
| 51216 | 7691 PyObject *args) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7692 { |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7693 PyObject *subobj; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7694 PyUnicodeObject *substring; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7695 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
|
7696 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
|
7697 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
|
7698 |
|
69471
f4da64529e8c
startswith and endswith don't accept None as slice index. Patch by Torsten Becker. (closes #11828)
Jesus Cea <jcea@jcea.es>
parents:
68256
diff
changeset
|
7699 if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
| 51216 | 7700 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
|
7701 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
|
7702 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
|
7703 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
|
7704 substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 51216 | 7705 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
|
7706 if (substring == NULL) |
| 51216 | 7707 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
|
7708 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
|
7709 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
|
7710 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
|
7711 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
|
7712 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7713 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7714 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
|
7715 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7716 substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
|
69562
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
69471
diff
changeset
|
7717 if (substring == NULL) { |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
69471
diff
changeset
|
7718 if (PyErr_ExceptionMatches(PyExc_TypeError)) |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
69471
diff
changeset
|
7719 PyErr_Format(PyExc_TypeError, "endswith first arg must be str, " |
|
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
69471
diff
changeset
|
7720 "unicode, or tuple, not %s", Py_TYPE(subobj)->tp_name); |
| 51216 | 7721 return NULL; |
|
69562
3ceeccbc2c3b
#6780: fix starts/endswith error message to mention that tuples are accepted too.
Ezio Melotti
parents:
69471
diff
changeset
|
7722 } |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7723 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
|
7724 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
|
7725 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
|
7726 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7727 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7728 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7729 /* 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
|
7730 #include "stringlib/string_format.h" |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7731 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7732 PyDoc_STRVAR(format__doc__, |
| 51216 | 7733 "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
|
7734 \n\ |
|
66044
1499db99c4cd
Improved docstrings for str and unicode methods format and __format__.
Eric Smith <eric@trueblade.com>
parents:
63468
diff
changeset
|
7735 Return a formatted version of S, using substitutions from args and kwargs.\n\ |
|
1499db99c4cd
Improved docstrings for str and unicode methods format and __format__.
Eric Smith <eric@trueblade.com>
parents:
63468
diff
changeset
|
7736 The substitutions are identified by braces ('{' and '}')."); |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7737 |
|
47392
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7738 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
|
7739 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
|
7740 { |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7741 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
|
7742 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
|
7743 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
|
7744 |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7745 /* 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
|
7746 /* 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
|
7747 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
|
7748 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
|
7749 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
|
7750 PyErr_Format(PyExc_TypeError, "__format__ arg must be str " |
| 51216 | 7751 "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
|
7752 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
|
7753 } |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7754 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
|
7755 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
|
7756 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
|
7757 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
|
7758 |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7759 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
|
7760 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
|
7761 PyUnicode_GET_SIZE(format_spec)); |
| 51216 | 7762 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
|
7763 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
|
7764 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
|
7765 } |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47388
diff
changeset
|
7766 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7767 PyDoc_STRVAR(p_format__doc__, |
| 51216 | 7768 "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
|
7769 \n\ |
|
66044
1499db99c4cd
Improved docstrings for str and unicode methods format and __format__.
Eric Smith <eric@trueblade.com>
parents:
63468
diff
changeset
|
7770 Return a formatted version of S as described by format_spec."); |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7771 |
|
47543
296ab1038b7f
Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47527
diff
changeset
|
7772 static PyObject * |
|
296ab1038b7f
Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47527
diff
changeset
|
7773 unicode__sizeof__(PyUnicodeObject *v) |
|
296ab1038b7f
Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47527
diff
changeset
|
7774 { |
|
47922
7cfc28ac0715
Added additional __sizeof__ implementations and addressed comments made in
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47543
diff
changeset
|
7775 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
|
7776 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
|
7777 } |
|
296ab1038b7f
Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47527
diff
changeset
|
7778 |
|
296ab1038b7f
Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47527
diff
changeset
|
7779 PyDoc_STRVAR(sizeof__doc__, |
| 51216 | 7780 "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
|
7781 \n\ |
|
296ab1038b7f
Issue 3048: Fixed sys.getsizeof for unicode objects.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47527
diff
changeset
|
7782 "); |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7783 |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7784 static PyObject * |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7785 unicode_getnewargs(PyUnicodeObject *v) |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7786 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7787 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
|
7788 } |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7789 |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7790 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7791 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
|
7792 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7793 /* 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
|
7794 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
|
7795 |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56033
diff
changeset
|
7796 {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, 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
|
7797 {"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
|
7798 {"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
|
7799 {"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
|
7800 {"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
|
7801 {"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
|
7802 {"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
|
7803 {"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
|
7804 {"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
|
7805 {"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
|
7806 {"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
|
7807 {"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
|
7808 {"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
|
7809 {"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
|
7810 {"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
|
7811 {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56033
diff
changeset
|
7812 {"decode", (PyCFunction) unicode_decode, METH_VARARGS | METH_KEYWORDS, 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
|
7813 /* {"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
|
7814 {"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
|
7815 {"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
|
7816 {"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
|
7817 {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7818 {"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
|
7819 {"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
|
7820 {"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
|
7821 {"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
|
7822 {"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
|
7823 {"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
|
7824 {"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
|
7825 {"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
|
7826 {"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
|
7827 {"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
|
7828 {"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
|
7829 {"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
|
7830 {"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
|
7831 {"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
|
7832 {"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
|
7833 {"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
|
7834 {"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
|
7835 {"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
|
7836 {"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
|
7837 {"__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
|
7838 {"_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
|
7839 {"_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
|
7840 {"__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
|
7841 #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
|
7842 {"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
|
7843 #endif |
|
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 #if 0 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7846 /* 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
|
7847 {"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
|
7848 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7849 |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7850 {"__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
|
7851 {NULL, NULL} |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7852 }; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7853 |
|
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
|
7854 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
|
7855 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
|
7856 { |
| 51216 | 7857 if (!PyUnicode_Check(v)) { |
| 7858 Py_INCREF(Py_NotImplemented); | |
| 7859 return Py_NotImplemented; | |
| 7860 } | |
| 7861 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
|
7862 } |
|
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
|
7863 |
|
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
|
7864 static PyNumberMethods unicode_as_number = { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7865 0, /*nb_add*/ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7866 0, /*nb_subtract*/ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7867 0, /*nb_multiply*/ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7868 0, /*nb_divide*/ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7869 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
|
7870 }; |
|
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
|
7871 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7872 static PySequenceMethods unicode_as_sequence = { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7873 (lenfunc) unicode_length, /* sq_length */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7874 PyUnicode_Concat, /* sq_concat */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7875 (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7876 (ssizeargfunc) unicode_getitem, /* sq_item */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7877 (ssizessizeargfunc) unicode_slice, /* sq_slice */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7878 0, /* sq_ass_item */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7879 0, /* sq_ass_slice */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7880 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
|
7881 }; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7882 |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7883 static PyObject* |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7884 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
|
7885 { |
|
39491
2312e178b34d
Correct an accidentally removed previous patch.
Marc-André Lemburg <mal@egenix.com>
parents:
39487
diff
changeset
|
7886 if (PyIndex_Check(item)) { |
|
2312e178b34d
Correct an accidentally removed previous patch.
Marc-André Lemburg <mal@egenix.com>
parents:
39487
diff
changeset
|
7887 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
|
7888 if (i == -1 && PyErr_Occurred()) |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7889 return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7890 if (i < 0) |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
7891 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
|
7892 return unicode_getitem(self, i); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7893 } else if (PySlice_Check(item)) { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7894 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
|
7895 Py_UNICODE* source_buf; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7896 Py_UNICODE* result_buf; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7897 PyObject* result; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7898 |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
7899 if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
| 51216 | 7900 &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
|
7901 return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7902 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7903 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7904 if (slicelength <= 0) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7905 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
|
7906 } 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
|
7907 PyUnicode_CheckExact(self)) { |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42652
diff
changeset
|
7908 Py_INCREF(self); |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42652
diff
changeset
|
7909 return (PyObject *)self; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42652
diff
changeset
|
7910 } else if (step == 1) { |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42652
diff
changeset
|
7911 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
|
7912 } else { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7913 source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
| 45826 | 7914 result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 7915 sizeof(Py_UNICODE)); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7916 |
| 51216 | 7917 if (result_buf == NULL) |
| 7918 return PyErr_NoMemory(); | |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7919 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7920 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
|
7921 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
|
7922 } |
|
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
|
7923 |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7924 result = PyUnicode_FromUnicode(result_buf, slicelength); |
| 45826 | 7925 PyObject_FREE(result_buf); |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7926 return result; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7927 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7928 } else { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7929 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
|
7930 return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7931 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7932 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7933 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7934 static PyMappingMethods unicode_as_mapping = { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7935 (lenfunc)unicode_length, /* mp_length */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7936 (binaryfunc)unicode_subscript, /* mp_subscript */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
7937 (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
|
7938 }; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7939 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7940 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
|
7941 unicode_buffer_getreadbuf(PyUnicodeObject *self, |
| 51216 | 7942 Py_ssize_t index, |
| 7943 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
|
7944 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7945 if (index != 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7946 PyErr_SetString(PyExc_SystemError, |
| 51216 | 7947 "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
|
7948 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7949 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7950 *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
|
7951 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
|
7952 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7953 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7954 static Py_ssize_t |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7955 unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index, |
| 51216 | 7956 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
|
7957 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7958 PyErr_SetString(PyExc_TypeError, |
| 51216 | 7959 "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
|
7960 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7961 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7962 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7963 static int |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7964 unicode_buffer_getsegcount(PyUnicodeObject *self, |
| 51216 | 7965 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
|
7966 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7967 if (lenp) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7968 *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
|
7969 return 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7970 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7971 |
|
36519
d6d415df0df0
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36510
diff
changeset
|
7972 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
|
7973 unicode_buffer_getcharbuf(PyUnicodeObject *self, |
| 51216 | 7974 Py_ssize_t index, |
| 7975 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
|
7976 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7977 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
|
7978 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7979 if (index != 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7980 PyErr_SetString(PyExc_SystemError, |
| 51216 | 7981 "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
|
7982 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7983 } |
|
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
|
7984 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
|
7985 if (str == NULL) |
| 51216 | 7986 return -1; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7987 *ptr = (void *) PyString_AS_STRING(str); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7988 return PyString_GET_SIZE(str); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7989 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7990 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7991 /* 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
|
7992 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7993 static PyObject * |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7994 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
|
7995 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7996 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
|
7997 if (argidx < arglen) { |
| 51216 | 7998 (*p_argidx)++; |
| 7999 if (arglen < 0) | |
| 8000 return args; | |
| 8001 else | |
| 8002 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
|
8003 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8004 PyErr_SetString(PyExc_TypeError, |
| 51216 | 8005 "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
|
8006 return NULL; |
|
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 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8009 #define F_LJUST (1<<0) |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8010 #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
|
8011 #define F_BLANK (1<<2) |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8012 #define F_ALT (1<<3) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8013 #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
|
8014 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8015 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
|
8016 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
|
8017 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8018 register Py_ssize_t i; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8019 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
|
8020 for (i = len - 1; i >= 0; i--) |
| 51216 | 8021 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
|
8022 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8023 return len; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8024 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8025 |
|
36261
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8026 static int |
|
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8027 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
|
8028 { |
|
36508
7322dccba224
doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents:
36506
diff
changeset
|
8029 Py_ssize_t result; |
|
7322dccba224
doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents:
36506
diff
changeset
|
8030 |
|
36261
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8031 PyOS_snprintf((char *)buffer, len, format, x); |
|
36508
7322dccba224
doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents:
36506
diff
changeset
|
8032 result = strtounicode(buffer, (char *)buffer); |
|
7322dccba224
doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents:
36506
diff
changeset
|
8033 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
|
8034 } |
|
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8035 |
|
24736
ff05eef3a6d1
Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents:
24703
diff
changeset
|
8036 /* 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
|
8037 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
|
8038 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
|
8039 |
|
57412
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8040 /* Returns a new reference to a PyUnicode object, or NULL on failure. */ |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8041 |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8042 static PyObject * |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8043 formatfloat(PyObject *v, int flags, int prec, int type) |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8044 { |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8045 char *p; |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8046 PyObject *result; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8047 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
|
8048 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8049 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
|
8050 if (x == -1.0 && PyErr_Occurred()) |
|
57412
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8051 return NULL; |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8052 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8053 if (prec < 0) |
| 51216 | 8054 prec = 6; |
|
52410
f05b6d9adbe6
Issue #532631: Apply floatformat changes to unicodeobject.c
Mark Dickinson <dickinsm@gmail.com>
parents:
52404
diff
changeset
|
8055 |
|
57412
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8056 p = PyOS_double_to_string(x, type, prec, |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8057 (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8058 if (p == NULL) |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8059 return NULL; |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8060 result = PyUnicode_FromStringAndSize(p, strlen(p)); |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8061 PyMem_Free(p); |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8062 return result; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8063 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8064 |
|
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
|
8065 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
|
8066 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
|
8067 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8068 char *buf; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8069 int i, len; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8070 PyObject *str; /* temporary string object. */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8071 PyUnicodeObject *result; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8072 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8073 str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8074 if (!str) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8075 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8076 result = _PyUnicode_New(len); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8077 if (!result) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8078 Py_DECREF(str); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8079 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8080 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8081 for (i = 0; i < len; i++) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8082 result->str[i] = buf[i]; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8083 result->str[len] = 0; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8084 Py_DECREF(str); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8085 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
|
8086 } |
|
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
|
8087 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8088 static int |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8089 formatint(Py_UNICODE *buf, |
| 51216 | 8090 size_t buflen, |
| 8091 int flags, | |
| 8092 int prec, | |
| 8093 int type, | |
| 8094 PyObject *v) | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8095 { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8096 /* 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
|
8097 * 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
|
8098 * + 1 + 1 |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8099 * = 24 |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8100 */ |
|
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
|
8101 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
|
8102 char *sign; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8103 long x; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8104 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8105 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
|
8106 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
|
8107 return -1; |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8108 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
|
8109 type = 'd'; |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8110 } |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8111 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
|
8112 sign = "-"; |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8113 else |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8114 sign = ""; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8115 if (prec < 0) |
|
22087
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8116 prec = 1; |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8117 |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8118 /* 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
|
8119 * 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
|
8120 */ |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8121 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
|
8122 PyErr_SetString(PyExc_OverflowError, |
| 51216 | 8123 "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
|
8124 return -1; |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8125 } |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8126 |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8127 if ((flags & F_ALT) && |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8128 (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
|
8129 /* 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
|
8130 * 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
|
8131 * - 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
|
8132 * 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
|
8133 * %#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
|
8134 * hex() function |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8135 * - 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
|
8136 * 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
|
8137 * (Metrowerks, Compaq Tru64) |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8138 * - 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
|
8139 * 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
|
8140 * 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
|
8141 * |
|
22087
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8142 * 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
|
8143 * 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
|
8144 * of %#x/%#X. |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8145 * |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8146 * 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
|
8147 * 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
|
8148 */ |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8149 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
|
8150 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
|
8151 } |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8152 else { |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8153 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
|
8154 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
|
8155 prec, type); |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8156 } |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8157 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
|
8158 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
|
8159 else |
|
36261
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8160 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
|
8161 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8162 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8163 static int |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8164 formatchar(Py_UNICODE *buf, |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8165 size_t buflen, |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8166 PyObject *v) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8167 { |
|
59138
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8168 PyObject *unistr; |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8169 char *str; |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8170 /* 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
|
8171 if (PyUnicode_Check(v)) { |
| 51216 | 8172 if (PyUnicode_GET_SIZE(v) != 1) |
| 8173 goto onError; | |
| 8174 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
|
8175 } |
|
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8176 |
|
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8177 else if (PyString_Check(v)) { |
| 51216 | 8178 if (PyString_GET_SIZE(v) != 1) |
| 8179 goto onError; | |
|
59138
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8180 /* #7649: "u'%c' % char" should behave like "u'%s' % char" and fail |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8181 with a UnicodeDecodeError if 'char' is not decodable with the |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8182 default encoding (usually ASCII, but it might be something else) */ |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8183 str = PyString_AS_STRING(v); |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8184 if ((unsigned char)str[0] > 0x7F) { |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8185 /* the char is not ASCII; try to decode the string using the |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8186 default encoding and return -1 to let the UnicodeDecodeError |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8187 be raised if the string can't be decoded */ |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8188 unistr = PyUnicode_Decode(str, 1, NULL, "strict"); |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8189 if (unistr == NULL) |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8190 return -1; |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8191 buf[0] = PyUnicode_AS_UNICODE(unistr)[0]; |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8192 Py_DECREF(unistr); |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8193 } |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8194 else |
|
aacc0d48d44d
Merged revisions 78449 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59087
diff
changeset
|
8195 buf[0] = (Py_UNICODE)str[0]; |
|
12899
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8196 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8197 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8198 else { |
| 51216 | 8199 /* 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
|
8200 long x; |
| 51216 | 8201 x = PyInt_AsLong(v); |
| 8202 if (x == -1 && PyErr_Occurred()) | |
| 8203 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
|
8204 #ifdef Py_UNICODE_WIDE |
| 51216 | 8205 if (x < 0 || x > 0x10ffff) { |
| 8206 PyErr_SetString(PyExc_OverflowError, | |
| 8207 "%c arg not in range(0x110000) " | |
| 8208 "(wide Python build)"); | |
| 8209 return -1; | |
| 8210 } | |
|
24737
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
8211 #else |
| 51216 | 8212 if (x < 0 || x > 0xffff) { |
| 8213 PyErr_SetString(PyExc_OverflowError, | |
| 8214 "%c arg not in range(0x10000) " | |
| 8215 "(narrow Python build)"); | |
| 8216 return -1; | |
| 8217 } | |
|
24737
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
8218 #endif |
| 51216 | 8219 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
|
8220 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8221 buf[1] = '\0'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8222 return 1; |
|
12899
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8223 |
| 51216 | 8224 onError: |
|
12899
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8225 PyErr_SetString(PyExc_TypeError, |
| 51216 | 8226 "%c requires int or char"); |
|
12899
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8227 return -1; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8228 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8229 |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8230 /* 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
|
8231 |
|
57412
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8232 FORMATBUFLEN is the length of the buffer in which the ints & |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8233 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
|
8234 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
|
8235 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
|
8236 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
|
8237 */ |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8238 #define FORMATBUFLEN (size_t)120 |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8239 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8240 PyObject *PyUnicode_Format(PyObject *format, |
| 51216 | 8241 PyObject *args) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8242 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8243 Py_UNICODE *fmt, *res; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8244 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
|
8245 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
|
8246 PyUnicodeObject *result = NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8247 PyObject *dict = NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8248 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
|
8249 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8250 if (format == NULL || args == NULL) { |
| 51216 | 8251 PyErr_BadInternalCall(); |
| 8252 return NULL; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8253 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8254 uformat = PyUnicode_FromObject(format); |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
8255 if (uformat == NULL) |
| 51216 | 8256 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8257 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
|
8258 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
|
8259 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8260 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
|
8261 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
|
8262 if (result == NULL) |
| 51216 | 8263 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8264 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
|
8265 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8266 if (PyTuple_Check(args)) { |
| 51216 | 8267 arglen = PyTuple_Size(args); |
| 8268 argidx = 0; | |
|
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 else { |
| 51216 | 8271 arglen = -1; |
| 8272 argidx = -2; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8273 } |
|
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
|
8274 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
|
8275 !PyObject_TypeCheck(args, &PyBaseString_Type)) |
| 51216 | 8276 dict = args; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8277 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8278 while (--fmtcnt >= 0) { |
| 51216 | 8279 if (*fmt != '%') { |
| 8280 if (--rescnt < 0) { | |
| 8281 rescnt = fmtcnt + 100; | |
| 8282 reslen += rescnt; | |
| 8283 if (_PyUnicode_Resize(&result, reslen) < 0) | |
| 8284 goto onError; | |
| 8285 res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; | |
| 8286 --rescnt; | |
| 8287 } | |
| 8288 *res++ = *fmt++; | |
| 8289 } | |
| 8290 else { | |
| 8291 /* Got a format specifier */ | |
| 8292 int flags = 0; | |
| 8293 Py_ssize_t width = -1; | |
| 8294 int prec = -1; | |
| 8295 Py_UNICODE c = '\0'; | |
| 8296 Py_UNICODE fill; | |
| 8297 int isnumok; | |
| 8298 PyObject *v = NULL; | |
| 8299 PyObject *temp = NULL; | |
| 8300 Py_UNICODE *pbuf; | |
| 8301 Py_UNICODE sign; | |
| 8302 Py_ssize_t len; | |
|
57412
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8303 Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{int,char}() */ |
| 51216 | 8304 |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8305 fmt++; |
| 51216 | 8306 if (*fmt == '(') { |
| 8307 Py_UNICODE *keystart; | |
| 8308 Py_ssize_t keylen; | |
| 8309 PyObject *key; | |
| 8310 int pcount = 1; | |
| 8311 | |
| 8312 if (dict == NULL) { | |
| 8313 PyErr_SetString(PyExc_TypeError, | |
| 8314 "format requires a mapping"); | |
| 8315 goto onError; | |
| 8316 } | |
| 8317 ++fmt; | |
| 8318 --fmtcnt; | |
| 8319 keystart = fmt; | |
| 8320 /* Skip over balanced parentheses */ | |
| 8321 while (pcount > 0 && --fmtcnt >= 0) { | |
| 8322 if (*fmt == ')') | |
| 8323 --pcount; | |
| 8324 else if (*fmt == '(') | |
| 8325 ++pcount; | |
| 8326 fmt++; | |
| 8327 } | |
| 8328 keylen = fmt - keystart - 1; | |
| 8329 if (fmtcnt < 0 || pcount > 0) { | |
| 8330 PyErr_SetString(PyExc_ValueError, | |
| 8331 "incomplete format key"); | |
| 8332 goto onError; | |
| 8333 } | |
|
21028
3dfbbf42bf16
Fix for bug #438164: %-formatting using Unicode objects.
Marc-André Lemburg <mal@egenix.com>
parents:
20563
diff
changeset
|
8334 #if 0 |
| 51216 | 8335 /* keys are converted to strings using UTF-8 and |
| 8336 then looked up since Python uses strings to hold | |
| 8337 variables names etc. in its namespaces and we | |
| 8338 wouldn't want to break common idioms. */ | |
| 8339 key = PyUnicode_EncodeUTF8(keystart, | |
| 8340 keylen, | |
| 8341 NULL); | |
|
21028
3dfbbf42bf16
Fix for bug #438164: %-formatting using Unicode objects.
Marc-André Lemburg <mal@egenix.com>
parents:
20563
diff
changeset
|
8342 #else |
| 51216 | 8343 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
|
8344 #endif |
| 51216 | 8345 if (key == NULL) |
| 8346 goto onError; | |
| 8347 if (args_owned) { | |
| 8348 Py_DECREF(args); | |
| 8349 args_owned = 0; | |
| 8350 } | |
| 8351 args = PyObject_GetItem(dict, key); | |
| 8352 Py_DECREF(key); | |
| 8353 if (args == NULL) { | |
| 8354 goto onError; | |
| 8355 } | |
| 8356 args_owned = 1; | |
| 8357 arglen = -1; | |
| 8358 argidx = -2; | |
| 8359 } | |
| 8360 while (--fmtcnt >= 0) { | |
| 8361 switch (c = *fmt++) { | |
| 8362 case '-': flags |= F_LJUST; continue; | |
| 8363 case '+': flags |= F_SIGN; continue; | |
| 8364 case ' ': flags |= F_BLANK; continue; | |
| 8365 case '#': flags |= F_ALT; continue; | |
| 8366 case '0': flags |= F_ZERO; continue; | |
| 8367 } | |
| 8368 break; | |
| 8369 } | |
| 8370 if (c == '*') { | |
| 8371 v = getnextarg(args, arglen, &argidx); | |
| 8372 if (v == NULL) | |
| 8373 goto onError; | |
| 8374 if (!PyInt_Check(v)) { | |
| 8375 PyErr_SetString(PyExc_TypeError, | |
| 8376 "* wants int"); | |
| 8377 goto onError; | |
| 8378 } | |
| 8379 width = PyInt_AsLong(v); | |
| 8380 if (width < 0) { | |
| 8381 flags |= F_LJUST; | |
| 8382 width = -width; | |
| 8383 } | |
| 8384 if (--fmtcnt >= 0) | |
| 8385 c = *fmt++; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8386 } |
| 51216 | 8387 else if (c >= '0' && c <= '9') { |
| 8388 width = c - '0'; | |
| 8389 while (--fmtcnt >= 0) { | |
| 8390 c = *fmt++; | |
| 8391 if (c < '0' || c > '9') | |
| 8392 break; | |
| 8393 if ((width*10) / 10 != width) { | |
| 8394 PyErr_SetString(PyExc_ValueError, | |
| 8395 "width too big"); | |
| 8396 goto onError; | |
| 8397 } | |
| 8398 width = width*10 + (c - '0'); | |
| 8399 } | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8400 } |
| 51216 | 8401 if (c == '.') { |
| 8402 prec = 0; | |
| 8403 if (--fmtcnt >= 0) | |
| 8404 c = *fmt++; | |
| 8405 if (c == '*') { | |
| 8406 v = getnextarg(args, arglen, &argidx); | |
| 8407 if (v == NULL) | |
| 8408 goto onError; | |
| 8409 if (!PyInt_Check(v)) { | |
| 8410 PyErr_SetString(PyExc_TypeError, | |
| 8411 "* wants int"); | |
| 8412 goto onError; | |
| 8413 } | |
| 8414 prec = PyInt_AsLong(v); | |
| 8415 if (prec < 0) | |
| 8416 prec = 0; | |
| 8417 if (--fmtcnt >= 0) | |
| 8418 c = *fmt++; | |
| 8419 } | |
| 8420 else if (c >= '0' && c <= '9') { | |
| 8421 prec = c - '0'; | |
| 8422 while (--fmtcnt >= 0) { | |
|
63106
742fdf7f6540
Merged revisions 82980 via svnmerge from
Stefan Krah <stefan@bytereef.org>
parents:
62711
diff
changeset
|
8423 c = *fmt++; |
| 51216 | 8424 if (c < '0' || c > '9') |
| 8425 break; | |
| 8426 if ((prec*10) / 10 != prec) { | |
| 8427 PyErr_SetString(PyExc_ValueError, | |
| 8428 "prec too big"); | |
| 8429 goto onError; | |
| 8430 } | |
| 8431 prec = prec*10 + (c - '0'); | |
| 8432 } | |
| 8433 } | |
| 8434 } /* prec */ | |
| 8435 if (fmtcnt >= 0) { | |
| 8436 if (c == 'h' || c == 'l' || c == 'L') { | |
| 8437 if (--fmtcnt >= 0) | |
| 8438 c = *fmt++; | |
| 8439 } | |
| 8440 } | |
| 8441 if (fmtcnt < 0) { | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8442 PyErr_SetString(PyExc_ValueError, |
| 51216 | 8443 "incomplete format"); |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8444 goto onError; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8445 } |
| 51216 | 8446 if (c != '%') { |
| 8447 v = getnextarg(args, arglen, &argidx); | |
| 8448 if (v == NULL) | |
| 8449 goto onError; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8450 } |
| 51216 | 8451 sign = 0; |
| 8452 fill = ' '; | |
| 8453 switch (c) { | |
| 8454 | |
| 8455 case '%': | |
| 8456 pbuf = formatbuf; | |
| 8457 /* presume that buffer length is at least 1 */ | |
| 8458 pbuf[0] = '%'; | |
| 8459 len = 1; | |
| 8460 break; | |
| 8461 | |
| 8462 case 's': | |
| 8463 case 'r': | |
|
59900
6151d09ce039
Merged revisions 79278,79280 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59138
diff
changeset
|
8464 if (PyUnicode_CheckExact(v) && c == 's') { |
| 51216 | 8465 temp = v; |
| 8466 Py_INCREF(temp); | |
| 8467 } | |
| 8468 else { | |
| 8469 PyObject *unicode; | |
| 8470 if (c == 's') | |
| 8471 temp = PyObject_Unicode(v); | |
| 8472 else | |
| 8473 temp = PyObject_Repr(v); | |
| 8474 if (temp == NULL) | |
| 8475 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
|
8476 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
|
8477 /* nothing to do */; |
|
16de4f90ef44
Let u'%s' % obj try obj.__unicode__() first and fallback to obj.__str__().
Marc-André Lemburg <mal@egenix.com>
parents:
32695
diff
changeset
|
8478 else if (PyString_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
|
8479 /* convert to string to Unicode */ |
| 51216 | 8480 unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
| 8481 PyString_GET_SIZE(temp), | |
| 8482 NULL, | |
| 8483 "strict"); | |
| 8484 Py_DECREF(temp); | |
| 8485 temp = unicode; | |
| 8486 if (temp == NULL) | |
| 8487 goto onError; | |
| 8488 } | |
| 8489 else { | |
| 8490 Py_DECREF(temp); | |
| 8491 PyErr_SetString(PyExc_TypeError, | |
| 8492 "%s argument has non-string str()"); | |
| 8493 goto onError; | |
| 8494 } | |
| 8495 } | |
| 8496 pbuf = PyUnicode_AS_UNICODE(temp); | |
| 8497 len = PyUnicode_GET_SIZE(temp); | |
| 8498 if (prec >= 0 && len > prec) | |
| 8499 len = prec; | |
| 8500 break; | |
| 8501 | |
| 8502 case 'i': | |
| 8503 case 'd': | |
| 8504 case 'u': | |
| 8505 case 'o': | |
| 8506 case 'x': | |
| 8507 case 'X': | |
| 8508 if (c == 'i') | |
| 8509 c = 'd'; | |
| 8510 isnumok = 0; | |
| 8511 if (PyNumber_Check(v)) { | |
| 8512 PyObject *iobj=NULL; | |
| 8513 | |
| 8514 if (PyInt_Check(v) || (PyLong_Check(v))) { | |
| 8515 iobj = v; | |
| 8516 Py_INCREF(iobj); | |
| 8517 } | |
| 8518 else { | |
| 8519 iobj = PyNumber_Int(v); | |
| 8520 if (iobj==NULL) iobj = PyNumber_Long(v); | |
| 8521 } | |
| 8522 if (iobj!=NULL) { | |
| 8523 if (PyInt_Check(iobj)) { | |
| 8524 isnumok = 1; | |
| 8525 pbuf = formatbuf; | |
| 8526 len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), | |
| 8527 flags, prec, c, iobj); | |
| 8528 Py_DECREF(iobj); | |
| 8529 if (len < 0) | |
| 8530 goto onError; | |
| 8531 sign = 1; | |
| 8532 } | |
| 8533 else if (PyLong_Check(iobj)) { | |
| 8534 isnumok = 1; | |
| 8535 temp = formatlong(iobj, flags, prec, c); | |
| 8536 Py_DECREF(iobj); | |
| 8537 if (!temp) | |
| 8538 goto onError; | |
| 8539 pbuf = PyUnicode_AS_UNICODE(temp); | |
| 8540 len = PyUnicode_GET_SIZE(temp); | |
| 8541 sign = 1; | |
| 8542 } | |
| 8543 else { | |
| 8544 Py_DECREF(iobj); | |
| 8545 } | |
| 8546 } | |
| 8547 } | |
| 8548 if (!isnumok) { | |
| 8549 PyErr_Format(PyExc_TypeError, | |
| 8550 "%%%c format: a number is required, " | |
| 8551 "not %.200s", (char)c, Py_TYPE(v)->tp_name); | |
| 8552 goto onError; | |
| 8553 } | |
| 8554 if (flags & F_ZERO) | |
| 8555 fill = '0'; | |
| 8556 break; | |
| 8557 | |
| 8558 case 'e': | |
| 8559 case 'E': | |
| 8560 case 'f': | |
| 8561 case 'F': | |
| 8562 case 'g': | |
| 8563 case 'G': | |
|
57412
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8564 temp = formatfloat(v, flags, prec, c); |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8565 if (temp == NULL) |
| 51216 | 8566 goto onError; |
|
57412
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8567 pbuf = PyUnicode_AS_UNICODE(temp); |
|
808f9c0d4ac4
Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
8568 len = PyUnicode_GET_SIZE(temp); |
| 51216 | 8569 sign = 1; |
| 8570 if (flags & F_ZERO) | |
| 8571 fill = '0'; | |
| 8572 break; | |
| 8573 | |
| 8574 case 'c': | |
| 8575 pbuf = formatbuf; | |
| 8576 len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); | |
| 8577 if (len < 0) | |
| 8578 goto onError; | |
| 8579 break; | |
| 8580 | |
| 8581 default: | |
| 8582 PyErr_Format(PyExc_ValueError, | |
| 8583 "unsupported format character '%c' (0x%x) " | |
| 8584 "at index %zd", | |
| 8585 (31<=c && c<=126) ? (char)c : '?', | |
|
25375
4df0e691f4da
Add cast to avoid compiler warning.
Marc-André Lemburg <mal@egenix.com>
parents:
25248
diff
changeset
|
8586 (int)c, |
| 51216 | 8587 (Py_ssize_t)(fmt - 1 - |
| 8588 PyUnicode_AS_UNICODE(uformat))); | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8589 goto onError; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8590 } |
| 51216 | 8591 if (sign) { |
| 8592 if (*pbuf == '-' || *pbuf == '+') { | |
| 8593 sign = *pbuf++; | |
| 8594 len--; | |
| 8595 } | |
| 8596 else if (flags & F_SIGN) | |
| 8597 sign = '+'; | |
| 8598 else if (flags & F_BLANK) | |
| 8599 sign = ' '; | |
| 8600 else | |
| 8601 sign = 0; | |
| 8602 } | |
| 8603 if (width < len) | |
| 8604 width = len; | |
| 8605 if (rescnt - (sign != 0) < width) { | |
| 8606 reslen -= rescnt; | |
| 8607 rescnt = width + fmtcnt + 100; | |
| 8608 reslen += rescnt; | |
| 8609 if (reslen < 0) { | |
| 8610 Py_XDECREF(temp); | |
| 8611 PyErr_NoMemory(); | |
| 8612 goto onError; | |
| 8613 } | |
| 8614 if (_PyUnicode_Resize(&result, reslen) < 0) { | |
| 8615 Py_XDECREF(temp); | |
| 8616 goto onError; | |
| 8617 } | |
| 8618 res = PyUnicode_AS_UNICODE(result) | |
| 8619 + reslen - rescnt; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8620 } |
| 51216 | 8621 if (sign) { |
| 8622 if (fill != ' ') | |
| 8623 *res++ = sign; | |
| 8624 rescnt--; | |
| 8625 if (width > len) | |
| 8626 width--; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8627 } |
| 51216 | 8628 if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 8629 assert(pbuf[0] == '0'); | |
| 8630 assert(pbuf[1] == c); | |
| 8631 if (fill != ' ') { | |
| 8632 *res++ = *pbuf++; | |
| 8633 *res++ = *pbuf++; | |
| 8634 } | |
| 8635 rescnt -= 2; | |
| 8636 width -= 2; | |
| 8637 if (width < 0) | |
| 8638 width = 0; | |
| 8639 len -= 2; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8640 } |
| 51216 | 8641 if (width > len && !(flags & F_LJUST)) { |
| 8642 do { | |
| 8643 --rescnt; | |
| 8644 *res++ = fill; | |
| 8645 } while (--width > len); | |
| 8646 } | |
| 8647 if (fill == ' ') { | |
| 8648 if (sign) | |
| 8649 *res++ = sign; | |
| 8650 if ((flags & F_ALT) && (c == 'x' || c == 'X')) { | |
| 8651 assert(pbuf[0] == '0'); | |
| 8652 assert(pbuf[1] == c); | |
| 8653 *res++ = *pbuf++; | |
| 8654 *res++ = *pbuf++; | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8655 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8656 } |
| 51216 | 8657 Py_UNICODE_COPY(res, pbuf, len); |
| 8658 res += len; | |
| 8659 rescnt -= len; | |
| 8660 while (--width >= len) { | |
| 8661 --rescnt; | |
| 8662 *res++ = ' '; | |
| 8663 } | |
| 8664 if (dict && (argidx < arglen) && c != '%') { | |
| 8665 PyErr_SetString(PyExc_TypeError, | |
| 8666 "not all arguments converted during string formatting"); | |
|
36945
0f19a6fc9756
- Reindent a confusingly indented piece of code (no intended code changes
Thomas Wouters <thomas@python.org>
parents:
36924
diff
changeset
|
8667 Py_XDECREF(temp); |
| 51216 | 8668 goto onError; |
| 8669 } | |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8670 Py_XDECREF(temp); |
| 51216 | 8671 } /* '%' */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8672 } /* until end */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8673 if (argidx < arglen && !dict) { |
| 51216 | 8674 PyErr_SetString(PyExc_TypeError, |
| 8675 "not all arguments converted during string formatting"); | |
| 8676 goto onError; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8677 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8678 |
|
36945
0f19a6fc9756
- Reindent a confusingly indented piece of code (no intended code changes
Thomas Wouters <thomas@python.org>
parents:
36924
diff
changeset
|
8679 if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 51216 | 8680 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8681 if (args_owned) { |
| 51216 | 8682 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
|
8683 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8684 Py_DECREF(uformat); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8685 return (PyObject *)result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8686 |
| 51216 | 8687 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8688 Py_XDECREF(result); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8689 Py_DECREF(uformat); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8690 if (args_owned) { |
| 51216 | 8691 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
|
8692 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8693 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8694 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8695 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8696 static PyBufferProcs unicode_as_buffer = { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8697 (readbufferproc) unicode_buffer_getreadbuf, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8698 (writebufferproc) unicode_buffer_getwritebuf, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8699 (segcountproc) unicode_buffer_getsegcount, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8700 (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
|
8701 }; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8702 |
|
24280
1598a1e473e2
staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24071
diff
changeset
|
8703 static PyObject * |
|
19593
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8704 unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
|
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8705 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8706 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8707 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
|
8708 { |
| 51216 | 8709 PyObject *x = NULL; |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8710 static char *kwlist[] = {"string", "encoding", "errors", 0}; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8711 char *encoding = NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8712 char *errors = NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8713 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8714 if (type != &PyUnicode_Type) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8715 return unicode_subtype_new(type, args, kwds); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8716 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 51216 | 8717 kwlist, &x, &encoding, &errors)) |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8718 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8719 if (x == NULL) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8720 return (PyObject *)_PyUnicode_New(0); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8721 if (encoding == NULL && errors == NULL) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8722 return PyObject_Unicode(x); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8723 else |
| 51216 | 8724 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
|
8725 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8726 |
|
19593
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8727 static PyObject * |
|
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8728 unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8729 { |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8730 PyUnicodeObject *tmp, *pnew; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8731 Py_ssize_t n; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8732 |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8733 assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8734 tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8735 if (tmp == NULL) |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8736 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8737 assert(PyUnicode_Check(tmp)); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8738 pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8739 if (pnew == NULL) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8740 Py_DECREF(tmp); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8741 return NULL; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8742 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8743 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
|
8744 if (pnew->str == NULL) { |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8745 _Py_ForgetReference((PyObject *)pnew); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8746 PyObject_Del(pnew); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8747 Py_DECREF(tmp); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8748 return PyErr_NoMemory(); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8749 } |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8750 Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8751 pnew->length = n; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8752 pnew->hash = tmp->hash; |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8753 Py_DECREF(tmp); |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8754 return (PyObject *)pnew; |
|
19593
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8755 } |
|
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8756 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
8757 PyDoc_STRVAR(unicode_doc, |
| 51216 | 8758 "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
|
8759 \n\ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8760 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
|
8761 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
|
8762 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
|
8763 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8764 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
|
8765 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8766 "unicode", /* tp_name */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8767 sizeof(PyUnicodeObject), /* tp_size */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8768 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
|
8769 /* Slots */ |
|
51204
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8770 (destructor)unicode_dealloc, /* tp_dealloc */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8771 0, /* tp_print */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8772 0, /* tp_getattr */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8773 0, /* tp_setattr */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8774 0, /* tp_compare */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8775 unicode_repr, /* tp_repr */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8776 &unicode_as_number, /* tp_as_number */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8777 &unicode_as_sequence, /* tp_as_sequence */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8778 &unicode_as_mapping, /* tp_as_mapping */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8779 (hashfunc) unicode_hash, /* tp_hash*/ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8780 0, /* tp_call*/ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8781 (reprfunc) unicode_str, /* tp_str */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8782 PyObject_GenericGetAttr, /* tp_getattro */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8783 0, /* tp_setattro */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8784 &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
|
8785 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
| 51216 | 8786 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
|
8787 unicode_doc, /* tp_doc */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8788 0, /* tp_traverse */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8789 0, /* tp_clear */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8790 PyUnicode_RichCompare, /* tp_richcompare */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8791 0, /* tp_weaklistoffset */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8792 0, /* tp_iter */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8793 0, /* tp_iternext */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8794 unicode_methods, /* tp_methods */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8795 0, /* tp_members */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8796 0, /* tp_getset */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8797 &PyBaseString_Type, /* tp_base */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8798 0, /* tp_dict */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8799 0, /* tp_descr_get */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8800 0, /* tp_descr_set */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8801 0, /* tp_dictoffset */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8802 0, /* tp_init */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8803 0, /* tp_alloc */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8804 unicode_new, /* tp_new */ |
|
dcfd436567ce
expand tabs in unicodeobject.c
Benjamin Peterson <benjamin@python.org>
parents:
50305
diff
changeset
|
8805 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
|
8806 }; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8807 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8808 /* 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
|
8809 |
|
13660
a1ce07218781
Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents:
13635
diff
changeset
|
8810 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
|
8811 { |
|
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
|
8812 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
|
8813 |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8814 /* 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
|
8815 Py_UNICODE linebreak[] = { |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8816 0x000A, /* LINE FEED */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8817 0x000D, /* CARRIAGE RETURN */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8818 0x001C, /* FILE SEPARATOR */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8819 0x001D, /* GROUP SEPARATOR */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8820 0x001E, /* RECORD SEPARATOR */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8821 0x0085, /* NEXT LINE */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8822 0x2028, /* LINE SEPARATOR */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8823 0x2029, /* PARAGRAPH SEPARATOR */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8824 }; |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8825 |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
8826 /* 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
|
8827 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
|
8828 numfree = 0; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8829 unicode_empty = _PyUnicode_New(0); |
|
39129
f2031f15d99d
Handle allocation failures gracefully. Found with failmalloc.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38817
diff
changeset
|
8830 if (!unicode_empty) |
| 51216 | 8831 return; |
|
39129
f2031f15d99d
Handle allocation failures gracefully. Found with failmalloc.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38817
diff
changeset
|
8832 |
|
12876
243593f11fcf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12646
diff
changeset
|
8833 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
|
8834 for (i = 0; i < 256; i++) |
| 51216 | 8835 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
|
8836 if (PyType_Ready(&PyUnicode_Type) < 0) |
| 51216 | 8837 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
|
8838 |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8839 /* 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
|
8840 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
|
8841 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
|
8842 ); |
|
38786
9c52efcaff65
Initialize the type object so pychecker can't crash the interpreter.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38709
diff
changeset
|
8843 |
|
9c52efcaff65
Initialize the type object so pychecker can't crash the interpreter.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38709
diff
changeset
|
8844 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
|
8845 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8846 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8847 /* 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
|
8848 |
|
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
|
8849 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
|
8850 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
|
8851 { |
|
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
|
8852 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
|
8853 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
|
8854 |
|
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
|
8855 for (u = free_list; u != NULL;) { |
| 51216 | 8856 PyUnicodeObject *v = u; |
| 8857 u = *(PyUnicodeObject **)u; | |
| 8858 if (v->str) | |
| 8859 PyObject_DEL(v->str); | |
| 8860 Py_XDECREF(v->defenc); | |
| 8861 PyObject_Del(v); | |
| 8862 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
|
8863 } |
|
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
|
8864 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
|
8865 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
|
8866 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
|
8867 } |
|
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
|
8868 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8869 void |
|
13660
a1ce07218781
Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents:
13635
diff
changeset
|
8870 _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
|
8871 { |
|
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
|
8872 int i; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8873 |
|
14998
c16bdf555c79
In _PyUnicode_Fini(), decref unicode_empty before tearng down the free
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
8874 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
|
8875 unicode_empty = NULL; |
|
15001
a349b89bf10e
_PyUnicode_Fini(): Initialize the local freelist walking variable `u'
Barry Warsaw <barry@python.org>
parents:
14998
diff
changeset
|
8876 |
|
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
|
8877 for (i = 0; i < 256; i++) { |
| 51216 | 8878 if (unicode_latin1[i]) { |
| 8879 Py_DECREF(unicode_latin1[i]); | |
| 8880 unicode_latin1[i] = NULL; | |
| 8881 } | |
|
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
|
8882 } |
|
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
|
8883 (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
|
8884 } |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
8885 |
|
37655
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
8886 #ifdef __cplusplus |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
8887 } |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
8888 #endif |
