Mercurial > cpython-lru_cache-weakref
annotate Objects/unicodeobject.c @ 45837:c5241069fffd legacy-trunk
Speed up test_dict by about 10x by only checking selected dict literal sizes,
instead of every integer from 0 to 400. Exhaustive testing wastes time without
providing enough more assurance that the code is correct.
| author | Jeffrey Yasskin <jyasskin@gmail.com> |
|---|---|
| date | Tue, 18 Mar 2008 05:12:41 +0000 |
| parents | d33cadb595f6 |
| children | f1ecb3e978db |
| 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 |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
15 Copyright (c) 1999 by Secret Labs AB |
|
55d89a8fe963
refactored the unicodeobject/ucnhash interface, to hide the
Fredrik Lundh <fredrik@pythonware.com>
parents:
15952
diff
changeset
|
16 Copyright (c) 1999 by Fredrik Lundh |
|
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 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
45 #include "formatter_unicode.h" |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
46 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 #include "unicodeobject.h" |
|
13209
b11e0a898de0
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13196
diff
changeset
|
48 #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
|
49 |
|
24071
8142f8d668a0
Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents:
23837
diff
changeset
|
50 #ifdef MS_WINDOWS |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
51 #include <windows.h> |
|
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
52 #endif |
| 12329 | 53 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 /* 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
|
55 |
|
45191
920a7633ae08
Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents:
45053
diff
changeset
|
56 #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
|
57 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 /* 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
|
59 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 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
|
61 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
|
62 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
|
63 |
|
45191
920a7633ae08
Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents:
45053
diff
changeset
|
64 At worst this will result in PyUnicode_MAXFREELIST * |
| 12329 | 65 (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
|
66 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
|
67 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 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
|
69 |
| 12329 | 70 Note: This is an experimental feature ! If you get core dumps when |
| 71 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
|
72 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 |
| 12329 | 75 #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
|
76 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 /* 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
|
78 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 #ifdef WORDS_BIGENDIAN |
|
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_BIG_ENDIAN |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 #else |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 # 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
|
83 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 |
|
12899
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
85 /* --- Globals ------------------------------------------------------------ |
|
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
86 |
|
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
87 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
|
88 not be used before calling that API. |
|
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
89 |
|
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
90 */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 |
|
37655
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
92 |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
93 #ifdef __cplusplus |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
94 extern "C" { |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
95 #endif |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
96 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 /* 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
|
98 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
|
99 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
|
100 |
|
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
|
101 /* 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
|
102 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
|
103 |
|
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 /* 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
|
105 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
|
106 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
|
107 |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
108 /* 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
|
109 parameter; it is initialized by _PyUnicode_Init(). |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
110 |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
111 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
|
112 PyUnicode_GetDefaultEncoding() APIs to access this global. |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
113 |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
114 */ |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
115 static char unicode_default_encoding[100]; |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
116 |
|
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
|
117 /* 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
|
118 const unsigned char _Py_ascii_whitespace[] = { |
|
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
|
119 0, 0, 0, 0, 0, 0, 0, 0, |
|
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
|
120 // case 0x0009: /* HORIZONTAL TABULATION */ |
|
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
|
121 // case 0x000A: /* LINE FEED */ |
|
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
|
122 // case 0x000B: /* VERTICAL TABULATION */ |
|
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
|
123 // case 0x000C: /* FORM FEED */ |
|
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
|
124 // case 0x000D: /* CARRIAGE RETURN */ |
|
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
|
125 0, 1, 1, 1, 1, 1, 0, 0, |
|
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
|
126 0, 0, 0, 0, 0, 0, 0, 0, |
|
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
|
127 // case 0x001C: /* FILE SEPARATOR */ |
|
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
|
128 // case 0x001D: /* GROUP SEPARATOR */ |
|
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
|
129 // case 0x001E: /* RECORD SEPARATOR */ |
|
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
|
130 // case 0x001F: /* UNIT SEPARATOR */ |
|
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
|
131 0, 0, 0, 0, 1, 1, 1, 1, |
|
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
|
132 // case 0x0020: /* SPACE */ |
|
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
|
133 1, 0, 0, 0, 0, 0, 0, 0, |
|
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
|
134 0, 0, 0, 0, 0, 0, 0, 0, |
|
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
|
135 0, 0, 0, 0, 0, 0, 0, 0, |
|
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
|
136 0, 0, 0, 0, 0, 0, 0, 0, |
|
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
|
137 |
|
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
|
138 0, 0, 0, 0, 0, 0, 0, 0, |
|
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
|
139 0, 0, 0, 0, 0, 0, 0, 0, |
|
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
|
140 0, 0, 0, 0, 0, 0, 0, 0, |
|
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
|
141 0, 0, 0, 0, 0, 0, 0, 0, |
|
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
|
142 0, 0, 0, 0, 0, 0, 0, 0, |
|
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
|
143 0, 0, 0, 0, 0, 0, 0, 0, |
|
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 0, 0, 0, 0, 0, 0, 0, 0, |
|
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 0, 0, 0, 0, 0, 0, 0, 0 |
|
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 }; |
|
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 |
|
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
|
148 /* 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
|
149 static unsigned char ascii_linebreak[] = { |
|
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
|
150 0, 0, 0, 0, 0, 0, 0, 0, |
|
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
|
151 // 0x000A, /* LINE FEED */ |
|
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
|
152 // 0x000D, /* CARRIAGE RETURN */ |
|
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
|
153 0, 0, 1, 0, 0, 1, 0, 0, |
|
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
|
154 0, 0, 0, 0, 0, 0, 0, 0, |
|
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
|
155 // 0x001C, /* FILE SEPARATOR */ |
|
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
|
156 // 0x001D, /* GROUP SEPARATOR */ |
|
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
|
157 // 0x001E, /* RECORD SEPARATOR */ |
|
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
|
158 0, 0, 0, 0, 1, 1, 1, 0, |
|
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
|
159 0, 0, 0, 0, 0, 0, 0, 0, |
|
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
|
160 0, 0, 0, 0, 0, 0, 0, 0, |
|
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
|
161 0, 0, 0, 0, 0, 0, 0, 0, |
|
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
|
162 0, 0, 0, 0, 0, 0, 0, 0, |
|
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
|
163 |
|
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
|
164 0, 0, 0, 0, 0, 0, 0, 0, |
|
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
|
165 0, 0, 0, 0, 0, 0, 0, 0, |
|
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
|
166 0, 0, 0, 0, 0, 0, 0, 0, |
|
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
|
167 0, 0, 0, 0, 0, 0, 0, 0, |
|
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
|
168 0, 0, 0, 0, 0, 0, 0, 0, |
|
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
|
169 0, 0, 0, 0, 0, 0, 0, 0, |
|
26278796d4da
Patch #1970 by Antoine Pitrou: Speedup unicode whitespace and linebreak detection. The speedup is about 25% for split() (571 / 457 usec) and 35% (175 / 127 usec )for splitlines()
Christian Heimes <christian@cheimes.de>
parents:
44945
diff
changeset
|
170 0, 0, 0, 0, 0, 0, 0, 0, |
|
26278796d4da
Patch #1970 by Antoine Pitrou: Speedup unicode whitespace and linebreak detection. The speedup is about 25% for split() (571 / 457 usec) and 35% (175 / 127 usec )for splitlines()
Christian Heimes <christian@cheimes.de>
parents:
44945
diff
changeset
|
171 0, 0, 0, 0, 0, 0, 0, 0 |
|
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 |
|
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
|
179 return 0x10FFFF; |
|
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 |
|
982163c4e9d2
Encode surrogates in UTF-8 even for a wide Py_UNICODE.
Martin v. Löwis <martin@v.loewis.de>
parents:
18295
diff
changeset
|
181 /* This is actually an illegal character, so it should |
|
982163c4e9d2
Encode surrogates in UTF-8 even for a wide Py_UNICODE.
Martin v. Löwis <martin@v.loewis.de>
parents:
18295
diff
changeset
|
182 not be passed to unichr. */ |
|
982163c4e9d2
Encode surrogates in UTF-8 even for a wide Py_UNICODE.
Martin v. Löwis <martin@v.loewis.de>
parents:
18295
diff
changeset
|
183 return 0xFFFF; |
|
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 |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
195 #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
|
196 |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
197 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
|
198 |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
199 #define BLOOM(mask, ch) ((mask & (1 << ((ch) & 0x1F)))) |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
200 |
|
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
|
201 #define BLOOM_LINEBREAK(ch) \ |
|
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
|
202 ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
|
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
|
203 (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
|
204 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
205 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
|
206 { |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
207 /* 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
|
208 |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
209 long mask; |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
210 Py_ssize_t i; |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
211 |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
212 mask = 0; |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
213 for (i = 0; i < len; i++) |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
214 mask |= (1 << (ptr[i] & 0x1F)); |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
215 |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
216 return mask; |
|
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 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
219 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
|
220 { |
|
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 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
|
224 if (set[i] == chr) |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
225 return 1; |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
226 |
|
38270
1dc5657f1745
return 0 on misses, not -1.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38257
diff
changeset
|
227 return 0; |
|
38257
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 |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
230 #define BLOOM_MEMBER(mask, chr, set, setlen)\ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
231 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
|
232 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 /* --- Unicode Object ----------------------------------------------------- */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
234 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 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
|
236 int unicode_resize(register PyUnicodeObject *unicode, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
237 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
|
238 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
239 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
|
240 |
| 12329 | 241 /* 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
|
242 if (unicode->length == length) |
| 12329 | 243 goto reset; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
244 |
|
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
|
245 /* 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
|
246 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
|
247 instead ! */ |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
248 |
|
37661
b1c6a3864b1e
Revert 34153: Py_UNICODE should not be signed.
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
249 if (unicode == unicode_empty || |
|
b1c6a3864b1e
Revert 34153: Py_UNICODE should not be signed.
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
250 (unicode->length == 1 && |
|
b1c6a3864b1e
Revert 34153: Py_UNICODE should not be signed.
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
251 unicode->str[0] < 256U && |
|
17618
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
252 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
|
253 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
|
254 "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
|
255 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
258 /* 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
|
259 The overallocation is also used by fastsearch, which assumes that it's |
| 38405 | 260 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
|
261 it contains). */ |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
262 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
263 oldstr = unicode->str; |
|
45804
d33cadb595f6
Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents:
45731
diff
changeset
|
264 unicode->str = PyObject_REALLOC(unicode->str, |
|
d33cadb595f6
Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents:
45731
diff
changeset
|
265 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
|
266 if (!unicode->str) { |
|
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
|
267 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
|
268 PyErr_NoMemory(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
269 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
270 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
271 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
|
272 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
|
273 |
| 12329 | 274 reset: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
275 /* 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
|
276 if (unicode->defenc) { |
|
2badb322355f
This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents:
13828
diff
changeset
|
277 Py_DECREF(unicode->defenc); |
|
2badb322355f
This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents:
13828
diff
changeset
|
278 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
|
279 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
280 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
|
281 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
282 return 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
283 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
284 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
285 /* We allocate one more byte to make sure the string is |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
286 Ux0000 terminated -- XXX is this needed ? |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
287 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
288 XXX This allocator could further be enhanced by assuring that the |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
289 free list never reduces its size below 1. |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
290 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
291 */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
292 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
293 static |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
294 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
|
295 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
296 register PyUnicodeObject *unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
297 |
| 38426 | 298 /* 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
|
299 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
|
300 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
|
301 return unicode_empty; |
|
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 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
304 /* 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
|
305 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
|
306 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
|
307 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
|
308 numfree--; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
309 if (unicode->str) { |
| 12329 | 310 /* Keep-Alive optimization: we only upsize the buffer, |
| 311 never downsize it. */ | |
|
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
|
312 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
|
313 unicode_resize(unicode, length) < 0) { |
|
45804
d33cadb595f6
Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents:
45731
diff
changeset
|
314 PyObject_DEL(unicode->str); |
| 12511 | 315 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
316 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
317 } |
|
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
|
318 else { |
|
45804
d33cadb595f6
Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents:
45731
diff
changeset
|
319 size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
|
d33cadb595f6
Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents:
45731
diff
changeset
|
320 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
|
321 } |
|
c29f3135a20c
Fix a mis-indentation in _PyUnicode_New() that caused me to stare at
Guido van Rossum <guido@python.org>
parents:
18051
diff
changeset
|
322 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
|
323 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
324 else { |
|
45804
d33cadb595f6
Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents:
45731
diff
changeset
|
325 size_t new_size; |
|
22981
dc1fb19d990d
Remove PyMalloc_*.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22938
diff
changeset
|
326 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
|
327 if (unicode == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
328 return NULL; |
|
45804
d33cadb595f6
Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents:
45731
diff
changeset
|
329 new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
|
d33cadb595f6
Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents:
45731
diff
changeset
|
330 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
|
331 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
332 |
| 12511 | 333 if (!unicode->str) { |
| 334 PyErr_NoMemory(); | |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11997
diff
changeset
|
335 goto onError; |
| 12511 | 336 } |
|
30439
30e495efe90a
Double-fix of crash in Unicode freelist handling.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
30431
diff
changeset
|
337 /* 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
|
338 * 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
|
339 * 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
|
340 * 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
|
341 * 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
|
342 * 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
|
343 */ |
|
30439
30e495efe90a
Double-fix of crash in Unicode freelist handling.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
30431
diff
changeset
|
344 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
|
345 unicode->str[length] = 0; |
|
37668
4342e441b11c
Remove another INT_MAX limitation
Martin v. Löwis <martin@v.loewis.de>
parents:
37662
diff
changeset
|
346 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
|
347 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
|
348 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
|
349 return unicode; |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11997
diff
changeset
|
350 |
|
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11997
diff
changeset
|
351 onError: |
|
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11997
diff
changeset
|
352 _Py_ForgetReference((PyObject *)unicode); |
|
22981
dc1fb19d990d
Remove PyMalloc_*.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22938
diff
changeset
|
353 PyObject_Del(unicode); |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11997
diff
changeset
|
354 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
355 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
356 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
357 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
|
358 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
|
359 { |
|
21262
e8fea95a82a4
Fix for #489669 (Neil Norwitz): memory leak in test_descr (unicode).
Guido van Rossum <guido@python.org>
parents:
21082
diff
changeset
|
360 if (PyUnicode_CheckExact(unicode) && |
|
45191
920a7633ae08
Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents:
45053
diff
changeset
|
361 numfree < PyUnicode_MAXFREELIST) { |
| 12329 | 362 /* Keep-Alive optimization */ |
| 363 if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { | |
|
45804
d33cadb595f6
Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents:
45731
diff
changeset
|
364 PyObject_DEL(unicode->str); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
365 unicode->str = NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
366 unicode->length = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
367 } |
|
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
|
368 if (unicode->defenc) { |
|
2badb322355f
This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents:
13828
diff
changeset
|
369 Py_DECREF(unicode->defenc); |
|
2badb322355f
This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents:
13828
diff
changeset
|
370 unicode->defenc = NULL; |
| 12329 | 371 } |
| 372 /* 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
|
373 *(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
|
374 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
|
375 numfree++; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
376 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
377 else { |
|
45804
d33cadb595f6
Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents:
45731
diff
changeset
|
378 PyObject_DEL(unicode->str); |
|
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
|
379 Py_XDECREF(unicode->defenc); |
|
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
|
380 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
|
381 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
382 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
383 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
384 int PyUnicode_Resize(PyObject **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
|
385 { |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
386 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
|
387 |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
388 /* 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
|
389 if (unicode == 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
|
390 PyErr_BadInternalCall(); |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
391 return -1; |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
392 } |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
393 v = (PyUnicodeObject *)*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
|
394 if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 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
|
395 PyErr_BadInternalCall(); |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
396 return -1; |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
397 } |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
398 |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
399 /* 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
|
400 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
|
401 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
|
402 if (v->length != 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
|
403 (v == unicode_empty || v->length == 1)) { |
|
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 PyUnicodeObject *w = _PyUnicode_New(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
|
405 if (w == 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
|
406 return -1; |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
407 Py_UNICODE_COPY(w->str, v->str, |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
408 length < v->length ? length : v->length); |
|
28242
fe1d571c3f80
Sf patch #700047: unicode object leaks refcount on resizing
Raymond Hettinger <python@rcn.com>
parents:
27826
diff
changeset
|
409 Py_DECREF(*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
|
410 *unicode = (PyObject *)w; |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
411 return 0; |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
412 } |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
413 |
|
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 /* 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
|
415 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
|
416 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
|
417 } |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
418 |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
419 /* Internal API for use in unicodeobject.c only ! */ |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
420 #define _PyUnicode_Resize(unicodevar, 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
|
421 PyUnicode_Resize(((PyObject **)(unicodevar)), 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
|
422 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
423 PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
424 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
|
425 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
426 PyUnicodeObject *unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
427 |
|
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
|
428 /* 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
|
429 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
|
430 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
|
431 |
|
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 /* Optimization for empty strings */ |
|
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 if (size == 0 && unicode_empty != 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
|
434 Py_INCREF(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
|
435 return (PyObject *)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
|
436 } |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
437 |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
438 /* Single character Unicode objects in the Latin-1 range are |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
439 shared when using this constructor */ |
|
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 if (size == 1 && *u < 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
|
441 unicode = unicode_latin1[*u]; |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
442 if (!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
|
443 unicode = _PyUnicode_New(1); |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
444 if (!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
|
445 return NULL; |
|
18051
e897550b4748
Fixes [ #430986 ] Buglet in PyUnicode_FromUnicode.
Marc-André Lemburg <mal@egenix.com>
parents:
17995
diff
changeset
|
446 unicode->str[0] = *u; |
|
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
|
447 unicode_latin1[*u] = 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
|
448 } |
|
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 Py_INCREF(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
|
450 return (PyObject *)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
|
451 } |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
452 } |
|
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
|
453 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
454 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
|
455 if (!unicode) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
456 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
457 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
458 /* 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
|
459 if (u != 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
|
460 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
|
461 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
462 return (PyObject *)unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
463 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
464 |
|
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
|
465 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
|
466 { |
|
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
|
467 PyUnicodeObject *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
|
468 /* 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
|
469 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
|
470 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
|
471 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
|
472 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
|
473 |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
474 /* Optimization for empty strings */ |
|
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
|
475 if (size == 0 && unicode_empty != 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
|
476 Py_INCREF(unicode_empty); |
|
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
|
477 return (PyObject *)unicode_empty; |
|
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
|
478 } |
|
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
|
479 |
|
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
|
480 /* Single characters are shared when using this constructor. |
|
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
|
481 Restrict to ASCII, since the input must be UTF-8. */ |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
482 if (size == 1 && Py_CHARMASK(*u) < 128) { |
|
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 unicode = unicode_latin1[Py_CHARMASK(*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
|
484 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
|
485 unicode = _PyUnicode_New(1); |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
486 if (!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
|
487 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
|
488 unicode->str[0] = Py_CHARMASK(*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
|
489 unicode_latin1[Py_CHARMASK(*u)] = 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
|
490 } |
|
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
|
491 Py_INCREF(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
|
492 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
|
493 } |
|
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 |
|
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 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
|
496 } |
|
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 |
|
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 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
|
499 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
|
500 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
|
501 |
|
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
|
502 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
|
503 } |
|
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
|
504 |
|
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
|
505 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
|
506 { |
|
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
|
507 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
|
508 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
|
509 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
|
510 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
|
511 } |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
512 |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
513 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
|
514 } |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
515 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
516 #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
|
517 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
518 PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
519 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
|
520 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
521 PyUnicodeObject *unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
522 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
523 if (w == NULL) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
524 PyErr_BadInternalCall(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
525 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
526 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
527 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
528 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
|
529 if (!unicode) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
530 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
531 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
532 /* 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
|
533 #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
|
534 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
|
535 #else |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
536 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
537 register Py_UNICODE *u; |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
538 register 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
|
539 u = PyUnicode_AS_UNICODE(unicode); |
|
33766
1907ad54d351
Applied patch for [ 1047269 ] Buffer overwrite in PyUnicode_AsWideChar.
Marc-André Lemburg <mal@egenix.com>
parents:
33499
diff
changeset
|
540 for (i = size; i > 0; i--) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
541 *u++ = *w++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
542 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
543 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
544 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
545 return (PyObject *)unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
546 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
547 |
|
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
|
548 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
|
549 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
|
550 { |
|
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
|
551 *fmt++ = '%'; |
|
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
|
552 if (width) { |
|
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
|
553 if (zeropad) |
|
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
|
554 *fmt++ = '0'; |
|
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
|
555 fmt += sprintf(fmt, "%d", width); |
|
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
|
556 } |
|
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
|
557 if (precision) |
|
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
|
558 fmt += sprintf(fmt, ".%d", precision); |
|
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
|
559 if (longflag) |
|
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
|
560 *fmt++ = 'l'; |
|
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
|
561 else if (size_tflag) { |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
562 char *f = PY_FORMAT_SIZE_T; |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
563 while (*f) |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
564 *fmt++ = *f++; |
|
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
|
565 } |
|
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
|
566 *fmt++ = 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
|
567 *fmt = '\0'; |
|
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
|
568 } |
|
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
|
569 |
|
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
|
570 #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
|
571 |
|
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
|
572 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
|
573 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
|
574 { |
|
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
|
575 va_list count; |
|
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
|
576 Py_ssize_t callcount = 0; |
|
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
|
577 PyObject **callresults = 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
|
578 PyObject **callresult = 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
|
579 Py_ssize_t n = 0; |
|
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
|
580 int width = 0; |
|
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
|
581 int precision = 0; |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
582 int zeropad; |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
583 const char* f; |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
584 Py_UNICODE *s; |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
585 PyObject *string; |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
586 /* used by sprintf */ |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
587 char buffer[21]; |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
588 /* use abuffer instead of buffer, if we need more space |
|
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
|
589 * (which can happen if there's a format specifier with width). */ |
|
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
|
590 char *abuffer = 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
|
591 char *realbuffer; |
|
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
|
592 Py_ssize_t abuffersize = 0; |
|
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
|
593 char fmt[60]; /* should be enough for %0width.precisionld */ |
|
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
|
594 const char *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
|
595 |
|
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
|
596 #ifdef VA_LIST_IS_ARRAY |
|
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
|
597 Py_MEMCPY(count, vargs, sizeof(va_list)); |
|
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
|
598 #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
|
599 #ifdef __va_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
|
600 __va_copy(count, 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
|
601 #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
|
602 count = 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
|
603 #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
|
604 #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
|
605 /* step 1: count the number of %S/%R format specifications |
|
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
|
606 * (we call PyObject_Str()/PyObject_Repr() for these 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
|
607 * once during step 3 and put the result in an array) */ |
|
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
|
608 for (f = format; *f; f++) { |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
609 if (*f == '%' && (*(f+1)=='S' || *(f+1)=='R')) |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
610 ++callcount; |
|
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
|
611 } |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
612 /* step 2: allocate memory for the results of |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
613 * PyObject_Str()/PyObject_Repr() calls */ |
|
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
|
614 if (callcount) { |
|
45804
d33cadb595f6
Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents:
45731
diff
changeset
|
615 callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
|
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
|
616 if (!callresults) { |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
617 PyErr_NoMemory(); |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
618 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
|
619 } |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
620 callresult = callresults; |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
621 } |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
622 /* step 3: figure out how large a buffer we need */ |
|
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
|
623 for (f = format; *f; f++) { |
|
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
|
624 if (*f == '%') { |
|
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
|
625 const char* p = f; |
|
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
|
626 width = 0; |
|
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
|
627 while (isdigit(*f)) |
|
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
|
628 width = (width*10) + *f++ - '0'; |
|
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
|
629 while (*++f && *f != '%' && !isalpha(*f)) |
|
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
|
630 ; |
|
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 |
|
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 /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
|
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 * they don't affect the amount of space we reserve. |
|
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
|
634 */ |
|
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
|
635 if ((*f == 'l' || *f == 'z') && |
|
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
|
636 (f[1] == 'd' || f[1] == '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
|
637 ++f; |
|
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
|
638 |
|
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
|
639 switch (*f) { |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
640 case '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
|
641 (void)va_arg(count, int); |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
642 /* fall through... */ |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
643 case '%': |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
644 n++; |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
645 break; |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
646 case 'd': case 'u': case 'i': case 'x': |
|
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
|
647 (void) va_arg(count, int); |
|
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
|
648 /* 20 bytes is enough to hold a 64-bit |
|
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
|
649 integer. Decimal takes the most space. |
|
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
|
650 This isn't enough for octal. |
|
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 If a width is specified we need more |
|
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 (which we allocate later). */ |
|
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 if (width < 20) |
|
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 width = 20; |
|
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 n += width; |
|
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 if (abuffersize < width) |
|
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 abuffersize = width; |
|
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
|
658 break; |
|
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
|
659 case 's': |
|
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
|
660 { |
|
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
|
661 /* UTF-8 */ |
|
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
|
662 unsigned char*s; |
|
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
|
663 s = va_arg(count, unsigned char*); |
|
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
|
664 while (*s) { |
|
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
|
665 if (*s < 128) { |
|
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
|
666 n++; s++; |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
667 } else if (*s < 0xc0) { |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
668 /* invalid UTF-8 */ |
|
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
|
669 n++; s++; |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
670 } else if (*s < 0xc0) { |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
671 n++; |
|
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
|
672 s++; if(!*s)break; |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
673 s++; |
|
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
|
674 } else if (*s < 0xe0) { |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
675 n++; |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
676 s++; if(!*s)break; |
|
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
|
677 s++; if(!*s)break; |
|
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 s++; |
|
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 } 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
|
680 #ifdef Py_UNICODE_WIDE |
|
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 n++; |
|
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 #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
|
683 n+=2; |
|
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 #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
|
685 s++; if(!*s)break; |
|
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 s++; if(!*s)break; |
|
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 s++; if(!*s)break; |
|
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
|
688 s++; |
|
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
|
689 } |
|
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
|
690 } |
|
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
|
691 break; |
|
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
|
692 } |
|
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
|
693 case '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
|
694 { |
|
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
|
695 PyObject *obj = va_arg(count, 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
|
696 assert(obj && PyUnicode_Check(obj)); |
|
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
|
697 n += PyUnicode_GET_SIZE(obj); |
|
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
|
698 break; |
|
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
|
699 } |
|
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
|
700 case 'V': |
|
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
|
701 { |
|
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
|
702 PyObject *obj = va_arg(count, 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
|
703 const char *str = va_arg(count, const char *); |
|
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
|
704 assert(obj || str); |
|
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
|
705 assert(!obj || PyUnicode_Check(obj)); |
|
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
|
706 if (obj) |
|
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
|
707 n += PyUnicode_GET_SIZE(obj); |
|
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
|
708 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
|
709 n += strlen(str); |
|
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
|
710 break; |
|
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
|
711 } |
|
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
|
712 case 'S': |
|
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
|
713 { |
|
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
|
714 PyObject *obj = va_arg(count, 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
|
715 PyObject *str; |
|
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
|
716 assert(obj); |
|
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
|
717 str = PyObject_Str(obj); |
|
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
|
718 if (!str) |
|
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
|
719 goto fail; |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
720 n += PyUnicode_GET_SIZE(str); |
|
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
|
721 /* Remember the str and switch to the next slot */ |
|
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
|
722 *callresult++ = str; |
|
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
|
723 break; |
|
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
|
724 } |
|
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
|
725 case 'R': |
|
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
|
726 { |
|
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
|
727 PyObject *obj = va_arg(count, 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
|
728 PyObject *repr; |
|
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
|
729 assert(obj); |
|
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
|
730 repr = PyObject_Repr(obj); |
|
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 if (!repr) |
|
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
|
732 goto fail; |
|
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
|
733 n += PyUnicode_GET_SIZE(repr); |
|
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
|
734 /* Remember the repr and switch to the next slot */ |
|
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
|
735 *callresult++ = repr; |
|
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
|
736 break; |
|
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
|
737 } |
|
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
|
738 case 'p': |
|
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
|
739 (void) va_arg(count, int); |
|
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
|
740 /* maximum 64-bit pointer representation: |
|
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
|
741 * 0xffffffffffffffff |
|
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
|
742 * so 19 characters is enough. |
|
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
|
743 * XXX I count 18 -- what's the extra for? |
|
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
|
744 */ |
|
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
|
745 n += 19; |
|
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
|
746 break; |
|
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
|
747 default: |
|
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
|
748 /* if we stumble upon an unknown |
|
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
|
749 formatting code, copy the rest of |
|
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
|
750 the format string to the output |
|
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
|
751 string. (we cannot just skip 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
|
752 code, since there's no way to know |
|
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
|
753 what's in the argument list) */ |
|
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
|
754 n += strlen(p); |
|
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
|
755 goto expand; |
|
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
|
756 } |
|
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
|
757 } 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
|
758 n++; |
|
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
|
759 } |
|
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
|
760 expand: |
|
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
|
761 if (abuffersize > 20) { |
|
45804
d33cadb595f6
Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents:
45731
diff
changeset
|
762 abuffer = PyObject_Malloc(abuffersize); |
|
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
|
763 if (!abuffer) { |
|
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
|
764 PyErr_NoMemory(); |
|
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
|
765 goto fail; |
|
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
|
766 } |
|
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
|
767 realbuffer = abuffer; |
|
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
|
768 } |
|
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
|
769 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
|
770 realbuffer = buffer; |
|
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
|
771 /* step 4: fill the buffer */ |
|
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
|
772 /* Since we've analyzed how much space we need for the worst case, |
|
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
|
773 we don't have to resize the string. |
|
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
|
774 There can be no errors beyond this point. */ |
|
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
|
775 string = PyUnicode_FromUnicode(NULL, n); |
|
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
|
776 if (!string) |
|
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
|
777 goto fail; |
|
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
|
778 |
|
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
|
779 s = PyUnicode_AS_UNICODE(string); |
|
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
|
780 callresult = callresults; |
|
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
|
781 |
|
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
|
782 for (f = format; *f; f++) { |
|
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
|
783 if (*f == '%') { |
|
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
|
784 const char* p = f++; |
|
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
|
785 int longflag = 0; |
|
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
|
786 int size_tflag = 0; |
|
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
|
787 zeropad = (*f == '0'); |
|
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
|
788 /* parse the width.precision part */ |
|
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
|
789 width = 0; |
|
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
|
790 while (isdigit(*f)) |
|
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
|
791 width = (width*10) + *f++ - '0'; |
|
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
|
792 precision = 0; |
|
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
|
793 if (*f == '.') { |
|
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
|
794 f++; |
|
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
|
795 while (isdigit(*f)) |
|
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
|
796 precision = (precision*10) + *f++ - '0'; |
|
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
|
797 } |
|
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
|
798 /* handle the long flag, but only for %ld and %lu. |
|
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
|
799 others can be added when necessary. */ |
|
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
|
800 if (*f == 'l' && (f[1] == 'd' || f[1] == '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
|
801 longflag = 1; |
|
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
|
802 ++f; |
|
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
|
803 } |
|
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
|
804 /* handle the size_t flag. */ |
|
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
|
805 if (*f == 'z' && (f[1] == 'd' || f[1] == '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
|
806 size_tflag = 1; |
|
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
|
807 ++f; |
|
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
|
808 } |
|
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
|
809 |
|
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
|
810 switch (*f) { |
|
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
|
811 case '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
|
812 *s++ = va_arg(vargs, int); |
|
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
|
813 break; |
|
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
|
814 case 'd': |
|
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
|
815 makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd'); |
|
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
|
816 if (longflag) |
|
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
|
817 sprintf(realbuffer, fmt, va_arg(vargs, 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
|
818 else if (size_tflag) |
|
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
|
819 sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
|
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
|
820 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
|
821 sprintf(realbuffer, fmt, va_arg(vargs, int)); |
|
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
|
822 appendstring(realbuffer); |
|
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
|
823 break; |
|
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
|
824 case '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
|
825 makefmt(fmt, longflag, size_tflag, zeropad, width, precision, '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
|
826 if (longflag) |
|
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
|
827 sprintf(realbuffer, fmt, va_arg(vargs, unsigned 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
|
828 else if (size_tflag) |
|
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
|
829 sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
|
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
|
830 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
|
831 sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
|
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
|
832 appendstring(realbuffer); |
|
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
|
833 break; |
|
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
|
834 case 'i': |
|
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
|
835 makefmt(fmt, 0, 0, zeropad, width, precision, 'i'); |
|
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
|
836 sprintf(realbuffer, fmt, va_arg(vargs, int)); |
|
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
|
837 appendstring(realbuffer); |
|
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
|
838 break; |
|
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
|
839 case 'x': |
|
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
|
840 makefmt(fmt, 0, 0, zeropad, width, precision, 'x'); |
|
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
|
841 sprintf(realbuffer, fmt, va_arg(vargs, int)); |
|
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
|
842 appendstring(realbuffer); |
|
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
|
843 break; |
|
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
|
844 case 's': |
|
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
|
845 { |
|
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
|
846 /* Parameter must be UTF-8 encoded. |
|
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
|
847 In case of encoding errors, use |
|
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
|
848 the replacement character. */ |
|
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
|
849 PyObject *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
|
850 p = va_arg(vargs, char*); |
|
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
|
851 u = PyUnicode_DecodeUTF8(p, strlen(p), |
|
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
|
852 "replace"); |
|
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
|
853 if (!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
|
854 goto fail; |
|
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
|
855 Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(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
|
856 PyUnicode_GET_SIZE(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
|
857 s += PyUnicode_GET_SIZE(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
|
858 Py_DECREF(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
|
859 break; |
|
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
|
860 } |
|
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
|
861 case '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
|
862 { |
|
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
|
863 PyObject *obj = va_arg(vargs, 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
|
864 Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
|
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
|
865 Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), 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
|
866 s += 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
|
867 break; |
|
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
|
868 } |
|
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
|
869 case 'V': |
|
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
|
870 { |
|
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
|
871 PyObject *obj = va_arg(vargs, 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
|
872 const char *str = va_arg(vargs, const char *); |
|
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
|
873 if (obj) { |
|
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
|
874 Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
|
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
|
875 Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), 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
|
876 s += 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
|
877 } 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
|
878 appendstring(str); |
|
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
|
879 } |
|
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
|
880 break; |
|
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
|
881 } |
|
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
|
882 case 'S': |
|
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
|
883 case 'R': |
|
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
|
884 { |
|
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
|
885 Py_UNICODE *ucopy; |
|
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
|
886 Py_ssize_t usize; |
|
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
|
887 Py_ssize_t upos; |
|
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
|
888 /* unused, since we already have the result */ |
|
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
|
889 (void) va_arg(vargs, 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
|
890 ucopy = PyUnicode_AS_UNICODE(*callresult); |
|
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
|
891 usize = PyUnicode_GET_SIZE(*callresult); |
|
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
|
892 for (upos = 0; upos<usize;) |
|
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
|
893 *s++ = ucopy[upos++]; |
|
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
|
894 /* We're done with the unicode()/repr() => forget it */ |
|
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
|
895 Py_DECREF(*callresult); |
|
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
|
896 /* switch to next unicode()/repr() result */ |
|
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
|
897 ++callresult; |
|
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
|
898 break; |
|
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
|
899 } |
|
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
|
900 case 'p': |
|
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
|
901 sprintf(buffer, "%p", va_arg(vargs, 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
|
902 /* %p is ill-defined: ensure leading 0x. */ |
|
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
|
903 if (buffer[1] == 'X') |
|
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
|
904 buffer[1] = 'x'; |
|
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
|
905 else if (buffer[1] != 'x') { |
|
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
|
906 memmove(buffer+2, buffer, strlen(buffer)+1); |
|
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
|
907 buffer[0] = '0'; |
|
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
|
908 buffer[1] = 'x'; |
|
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
|
909 } |
|
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
|
910 appendstring(buffer); |
|
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
|
911 break; |
|
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
|
912 case '%': |
|
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
|
913 *s++ = '%'; |
|
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
|
914 break; |
|
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
|
915 default: |
|
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
|
916 appendstring(p); |
|
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
|
917 goto 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
|
918 } |
|
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
|
919 } 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
|
920 *s++ = *f; |
|
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
|
921 } |
|
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
|
922 |
|
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
|
923 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
|
924 if (callresults) |
|
45804
d33cadb595f6
Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents:
45731
diff
changeset
|
925 PyObject_Free(callresults); |
|
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
|
926 if (abuffer) |
|
45804
d33cadb595f6
Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents:
45731
diff
changeset
|
927 PyObject_Free(abuffer); |
|
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
|
928 _PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
|
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
|
929 return string; |
|
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
|
930 fail: |
|
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
|
931 if (callresults) { |
|
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
|
932 PyObject **callresult2 = callresults; |
|
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
|
933 while (callresult2 < callresult) { |
|
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
|
934 Py_DECREF(*callresult2); |
|
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
|
935 ++callresult2; |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
936 } |
|
45804
d33cadb595f6
Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents:
45731
diff
changeset
|
937 PyObject_Free(callresults); |
|
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
|
938 } |
|
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
|
939 if (abuffer) |
|
45804
d33cadb595f6
Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents:
45731
diff
changeset
|
940 PyObject_Free(abuffer); |
|
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
|
941 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
|
942 } |
|
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
|
943 |
|
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
|
944 #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
|
945 |
|
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
|
946 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
|
947 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
|
948 { |
|
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
|
949 PyObject* ret; |
|
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
|
950 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
|
951 |
|
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
|
952 #ifdef HAVE_STDARG_PROTOTYPES |
|
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
|
953 va_start(vargs, 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
|
954 #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
|
955 va_start(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
|
956 #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
|
957 ret = PyUnicode_FromFormatV(format, 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
|
958 va_end(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
|
959 return ret; |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
960 } |
|
bbc719a8b58b
Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents:
44439
diff
changeset
|
961 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
962 Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
963 wchar_t *w, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
964 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
|
965 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
966 if (unicode == NULL) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
967 PyErr_BadInternalCall(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
968 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
969 } |
|
34016
264e45baacd5
Correct the handling of 0-termination of PyUnicode_AsWideChar()
Marc-André Lemburg <mal@egenix.com>
parents:
33766
diff
changeset
|
970 |
|
264e45baacd5
Correct the handling of 0-termination of PyUnicode_AsWideChar()
Marc-André Lemburg <mal@egenix.com>
parents:
33766
diff
changeset
|
971 /* 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
|
972 if (size > PyUnicode_GET_SIZE(unicode)) |
|
34016
264e45baacd5
Correct the handling of 0-termination of PyUnicode_AsWideChar()
Marc-André Lemburg <mal@egenix.com>
parents:
33766
diff
changeset
|
973 size = PyUnicode_GET_SIZE(unicode) + 1; |
|
264e45baacd5
Correct the handling of 0-termination of PyUnicode_AsWideChar()
Marc-André Lemburg <mal@egenix.com>
parents:
33766
diff
changeset
|
974 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
975 #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
|
976 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
|
977 #else |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
978 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
979 register Py_UNICODE *u; |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
980 register 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
|
981 u = PyUnicode_AS_UNICODE(unicode); |
|
33766
1907ad54d351
Applied patch for [ 1047269 ] Buffer overwrite in PyUnicode_AsWideChar.
Marc-André Lemburg <mal@egenix.com>
parents:
33499
diff
changeset
|
982 for (i = size; i > 0; i--) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
983 *w++ = *u++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
984 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
985 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
986 |
|
34016
264e45baacd5
Correct the handling of 0-termination of PyUnicode_AsWideChar()
Marc-André Lemburg <mal@egenix.com>
parents:
33766
diff
changeset
|
987 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
|
988 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
|
989 else |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
990 return size; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
991 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
992 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
993 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
994 |
|
24737
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
995 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
|
996 { |
|
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
|
997 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
|
998 |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
999 #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
|
1000 if (ordinal < 0 || ordinal > 0x10ffff) { |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1001 PyErr_SetString(PyExc_ValueError, |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1002 "unichr() arg not in range(0x110000) " |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1003 "(wide Python build)"); |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1004 return NULL; |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1005 } |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1006 #else |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1007 if (ordinal < 0 || ordinal > 0xffff) { |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1008 PyErr_SetString(PyExc_ValueError, |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1009 "unichr() arg not in range(0x10000) " |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1010 "(narrow Python build)"); |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1011 return NULL; |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1012 } |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1013 #endif |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1014 |
|
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
|
1015 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
|
1016 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
|
1017 } |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
1018 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1019 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
|
1020 { |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1021 /* XXX Perhaps we should make this API an alias of |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1022 PyObject_Unicode() instead ?! */ |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1023 if (PyUnicode_CheckExact(obj)) { |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1024 Py_INCREF(obj); |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1025 return obj; |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1026 } |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1027 if (PyUnicode_Check(obj)) { |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1028 /* For a Unicode subtype that's not a Unicode object, |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1029 return a true Unicode object with the same data. */ |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1030 return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1031 PyUnicode_GET_SIZE(obj)); |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1032 } |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1033 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
|
1034 } |
|
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1035 |
|
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1036 PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
|
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1037 const char *encoding, |
|
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1038 const char *errors) |
|
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1039 { |
|
19999
1f4e2627b627
Implement the changes proposed in patch #413333. unicode(obj) now
Marc-André Lemburg <mal@egenix.com>
parents:
19998
diff
changeset
|
1040 const char *s = NULL; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1041 Py_ssize_t len; |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1042 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
|
1043 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1044 if (obj == NULL) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1045 PyErr_BadInternalCall(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1046 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1047 } |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1048 |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1049 #if 0 |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1050 /* 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
|
1051 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
|
1052 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
|
1053 Unicode subclasses. |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1054 |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1055 NOTE: This API should really only be used for object which |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1056 represent *encoded* Unicode ! |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1057 |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1058 */ |
|
19999
1f4e2627b627
Implement the changes proposed in patch #413333. unicode(obj) now
Marc-André Lemburg <mal@egenix.com>
parents:
19998
diff
changeset
|
1059 if (PyUnicode_Check(obj)) { |
|
1f4e2627b627
Implement the changes proposed in patch #413333. unicode(obj) now
Marc-André Lemburg <mal@egenix.com>
parents:
19998
diff
changeset
|
1060 if (encoding) { |
|
1f4e2627b627
Implement the changes proposed in patch #413333. unicode(obj) now
Marc-André Lemburg <mal@egenix.com>
parents:
19998
diff
changeset
|
1061 PyErr_SetString(PyExc_TypeError, |
|
1f4e2627b627
Implement the changes proposed in patch #413333. unicode(obj) now
Marc-André Lemburg <mal@egenix.com>
parents:
19998
diff
changeset
|
1062 "decoding Unicode is not supported"); |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1063 return NULL; |
|
19999
1f4e2627b627
Implement the changes proposed in patch #413333. unicode(obj) now
Marc-André Lemburg <mal@egenix.com>
parents:
19998
diff
changeset
|
1064 } |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1065 return PyObject_Unicode(obj); |
|
19999
1f4e2627b627
Implement the changes proposed in patch #413333. unicode(obj) now
Marc-André Lemburg <mal@egenix.com>
parents:
19998
diff
changeset
|
1066 } |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1067 #else |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1068 if (PyUnicode_Check(obj)) { |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1069 PyErr_SetString(PyExc_TypeError, |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1070 "decoding Unicode is not supported"); |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1071 return NULL; |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1072 } |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1073 #endif |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1074 |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1075 /* Coerce object */ |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1076 if (PyString_Check(obj)) { |
|
19999
1f4e2627b627
Implement the changes proposed in patch #413333. unicode(obj) now
Marc-André Lemburg <mal@egenix.com>
parents:
19998
diff
changeset
|
1077 s = PyString_AS_STRING(obj); |
|
1f4e2627b627
Implement the changes proposed in patch #413333. unicode(obj) now
Marc-André Lemburg <mal@egenix.com>
parents:
19998
diff
changeset
|
1078 len = PyString_GET_SIZE(obj); |
|
1f4e2627b627
Implement the changes proposed in patch #413333. unicode(obj) now
Marc-André Lemburg <mal@egenix.com>
parents:
19998
diff
changeset
|
1079 } |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1080 else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1081 /* Overwrite the error message with something more useful in |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1082 case of a TypeError. */ |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1083 if (PyErr_ExceptionMatches(PyExc_TypeError)) |
|
19999
1f4e2627b627
Implement the changes proposed in patch #413333. unicode(obj) now
Marc-André Lemburg <mal@egenix.com>
parents:
19998
diff
changeset
|
1084 PyErr_Format(PyExc_TypeError, |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1085 "coercing to Unicode: need string or buffer, " |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
1086 "%.80s found", |
|
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
|
1087 Py_TYPE(obj)->tp_name); |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1088 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
|
1089 } |
|
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
|
1090 |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1091 /* 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
|
1092 if (len == 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1093 Py_INCREF(unicode_empty); |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1094 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
|
1095 } |
|
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
|
1096 else |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1097 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
|
1098 |
|
13401
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1099 return v; |
|
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1100 |
|
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1101 onError: |
|
8f1a2deeb9b0
Added new API PyUnicode_FromEncodedObject() which supports decoding
Marc-André Lemburg <mal@egenix.com>
parents:
13396
diff
changeset
|
1102 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1103 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1104 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1105 PyObject *PyUnicode_Decode(const char *s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1106 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
|
1107 const char *encoding, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1108 const char *errors) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1109 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1110 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
|
1111 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1112 if (encoding == NULL) |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1113 encoding = PyUnicode_GetDefaultEncoding(); |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1114 |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1115 /* Shortcuts for common default encodings */ |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1116 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
|
1117 return PyUnicode_DecodeUTF8(s, size, errors); |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1118 else if (strcmp(encoding, "latin-1") == 0) |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1119 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
|
1120 #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
|
1121 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
|
1122 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
|
1123 #endif |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1124 else if (strcmp(encoding, "ascii") == 0) |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1125 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
|
1126 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1127 /* 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
|
1128 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
|
1129 if (buffer == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1130 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1131 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
|
1132 if (unicode == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1133 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1134 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
|
1135 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
|
1136 "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
|
1137 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
|
1138 Py_DECREF(unicode); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1139 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1140 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1141 Py_DECREF(buffer); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1142 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
|
1143 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1144 onError: |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1145 Py_XDECREF(buffer); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1146 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1147 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1148 |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1149 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
|
1150 const char *encoding, |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1151 const char *errors) |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1152 { |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1153 PyObject *v; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1154 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1155 if (!PyUnicode_Check(unicode)) { |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1156 PyErr_BadArgument(); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1157 goto onError; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1158 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1159 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1160 if (encoding == NULL) |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1161 encoding = PyUnicode_GetDefaultEncoding(); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1162 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1163 /* 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
|
1164 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
|
1165 if (v == NULL) |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1166 goto onError; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1167 return v; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1168 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1169 onError: |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1170 return NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1171 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1172 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1173 PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1174 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
|
1175 const char *encoding, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1176 const char *errors) |
|
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 *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
|
1179 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1180 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
|
1181 if (unicode == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1182 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1183 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
|
1184 Py_DECREF(unicode); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1185 return v; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1186 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1187 |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1188 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
|
1189 const char *encoding, |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1190 const char *errors) |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1191 { |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1192 PyObject *v; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1193 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1194 if (!PyUnicode_Check(unicode)) { |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1195 PyErr_BadArgument(); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1196 goto onError; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1197 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1198 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1199 if (encoding == NULL) |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1200 encoding = PyUnicode_GetDefaultEncoding(); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1201 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1202 /* 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
|
1203 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
|
1204 if (v == NULL) |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1205 goto onError; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1206 return v; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1207 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1208 onError: |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1209 return NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1210 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
1211 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1212 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
|
1213 const char *encoding, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1214 const char *errors) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1215 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1216 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
|
1217 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1218 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
|
1219 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1220 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1221 } |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1222 |
|
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
|
1223 if (encoding == NULL) |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1224 encoding = PyUnicode_GetDefaultEncoding(); |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1225 |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1226 /* Shortcuts for common default encodings */ |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1227 if (errors == NULL) { |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1228 if (strcmp(encoding, "utf-8") == 0) |
|
17995
837825a779c1
fix bogus indentation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17887
diff
changeset
|
1229 return PyUnicode_AsUTF8String(unicode); |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1230 else if (strcmp(encoding, "latin-1") == 0) |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1231 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
|
1232 #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
|
1233 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
|
1234 return PyUnicode_AsMBCSString(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
|
1235 #endif |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1236 else if (strcmp(encoding, "ascii") == 0) |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1237 return PyUnicode_AsASCIIString(unicode); |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1238 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1239 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1240 /* 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
|
1241 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
|
1242 if (v == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1243 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1244 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
|
1245 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
|
1246 "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
|
1247 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
|
1248 Py_DECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1249 goto onError; |
|
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 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
|
1252 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1253 onError: |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1254 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1255 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1256 |
|
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
|
1257 PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
|
2badb322355f
This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents:
13828
diff
changeset
|
1258 const char *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
|
1259 { |
|
2badb322355f
This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents:
13828
diff
changeset
|
1260 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
|
1261 |
|
2badb322355f
This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents:
13828
diff
changeset
|
1262 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
|
1263 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
|
1264 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
|
1265 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
|
1266 ((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
|
1267 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
|
1268 } |
|
2badb322355f
This patch finalizes the move from UTF-8 to a default encoding in
Marc-André Lemburg <mal@egenix.com>
parents:
13828
diff
changeset
|
1269 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1270 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
|
1271 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1272 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
|
1273 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1274 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1275 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1276 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
|
1277 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1278 onError: |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1279 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1280 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1281 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1282 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
|
1283 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1284 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
|
1285 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1286 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1287 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1288 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
|
1289 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1290 onError: |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1291 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1292 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1293 |
|
13660
a1ce07218781
Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents:
13635
diff
changeset
|
1294 const char *PyUnicode_GetDefaultEncoding(void) |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1295 { |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1296 return unicode_default_encoding; |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1297 } |
|
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 int PyUnicode_SetDefaultEncoding(const char *encoding) |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1300 { |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1301 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
|
1302 |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1303 /* 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
|
1304 loads the encoding into the codec registry cache. */ |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1305 v = _PyCodec_Lookup(encoding); |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1306 if (v == NULL) |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1307 goto onError; |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1308 Py_DECREF(v); |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1309 strncpy(unicode_default_encoding, |
|
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
|
1310 encoding, |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1311 sizeof(unicode_default_encoding)); |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1312 return 0; |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1313 |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1314 onError: |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1315 return -1; |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1316 } |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
1317 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1318 /* 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
|
1319 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
|
1320 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
|
1321 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
|
1322 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
|
1323 */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1324 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1325 static |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1326 int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1327 const char *encoding, const char *reason, |
| 43052 | 1328 const char *input, Py_ssize_t insize, Py_ssize_t *startinpos, |
| 1329 Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1330 PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1331 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1332 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
|
1333 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1334 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
|
1335 PyObject *repunicode = NULL; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1336 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
|
1337 Py_ssize_t requiredsize; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1338 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
|
1339 Py_UNICODE *repptr; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1340 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
|
1341 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
|
1342 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1343 if (*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
|
1344 *errorHandler = PyCodec_LookupError(errors); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1345 if (*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
|
1346 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
|
1347 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1348 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1349 if (*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
|
1350 *exceptionObject = PyUnicodeDecodeError_Create( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1351 encoding, input, insize, *startinpos, *endinpos, reason); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1352 if (*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
|
1353 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
|
1354 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1355 else { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1356 if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1357 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
|
1358 if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1359 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
|
1360 if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1361 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
|
1362 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1363 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1364 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
|
1365 if (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
|
1366 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
|
1367 if (!PyTuple_Check(restuple)) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1368 PyErr_Format(PyExc_TypeError, &argparse[4]); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1369 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
|
1370 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1371 if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1372 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
|
1373 if (newpos<0) |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
1374 newpos = insize+newpos; |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
1375 if (newpos<0 || newpos>insize) { |
|
36510
0f3894bb2ada
Support %zd in PyErr_Format and PyString_FromFormat.
Martin v. Löwis <martin@v.loewis.de>
parents:
36508
diff
changeset
|
1376 PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
1377 goto onError; |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
1378 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1379 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1380 /* 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
|
1381 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
|
1382 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
|
1383 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
|
1384 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
|
1385 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
|
1386 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
|
1387 if (requiredsize > outsize) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1388 if (requiredsize<2*outsize) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1389 requiredsize = 2*outsize; |
|
30431
cd0c3f916b34
Change checks of PyUnicode_Resize() return value for clarity.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
30216
diff
changeset
|
1390 if (PyUnicode_Resize(output, requiredsize) < 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
|
1391 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
|
1392 *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1393 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1394 *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
|
1395 *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
|
1396 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
|
1397 *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
|
1398 *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
|
1399 /* 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
|
1400 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
|
1401 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1402 onError: |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1403 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
|
1404 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
|
1405 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1406 |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1407 /* --- UTF-7 Codec -------------------------------------------------------- */ |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1408 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1409 /* see RFC2152 for details */ |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1410 |
|
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
|
1411 static |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1412 char utf7_special[128] = { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1413 /* indicate whether a UTF-7 character is special i.e. cannot be directly |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1414 encoded: |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1415 0 - not special |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1416 1 - special |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1417 2 - whitespace (optional) |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1418 3 - RFC2152 Set O (optional) */ |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1419 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1420 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1421 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1422 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1423 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1424 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1425 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1426 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1427 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1428 }; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1429 |
|
35728
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1430 /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
|
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1431 warnings about the comparison always being false; since |
|
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1432 utf7_special[0] is 1, we can safely make that one comparison |
|
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1433 true */ |
|
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1434 |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1435 #define SPECIAL(c, encodeO, encodeWS) \ |
|
35728
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1436 ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
|
35729
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1437 (encodeWS && (utf7_special[(c)] == 2)) || \ |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1438 (encodeO && (utf7_special[(c)] == 3))) |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1439 |
|
35728
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1440 #define B64(n) \ |
|
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1441 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
|
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1442 #define B64CHAR(c) \ |
|
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1443 (isalnum(c) || (c) == '+' || (c) == '/') |
|
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1444 #define UB64(c) \ |
|
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1445 ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
|
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1446 (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1447 |
|
35729
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1448 #define ENCODE(out, ch, bits) \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1449 while (bits >= 6) { \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1450 *out++ = B64(ch >> (bits-6)); \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1451 bits -= 6; \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1452 } |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1453 |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1454 #define DECODE(out, ch, bits, surrogate) \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1455 while (bits >= 16) { \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1456 Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1457 bits -= 16; \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1458 if (surrogate) { \ |
|
35728
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1459 /* We have already generated an error for the high surrogate \ |
|
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1460 so let's not bother seeing if the low surrogate is correct or not */ \ |
|
35729
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1461 surrogate = 0; \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1462 } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1463 /* This is a surrogate pair. Unfortunately we can't represent \ |
|
35729
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1464 it in a 16-bit character */ \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1465 surrogate = 1; \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1466 errmsg = "code pairs are not supported"; \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1467 goto utf7Error; \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1468 } else { \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1469 *out++ = outCh; \ |
|
6913abb7047a
Whitespace corrections.
Marc-André Lemburg <mal@egenix.com>
parents:
35728
diff
changeset
|
1470 } \ |
|
35728
9ed43e637ae7
Bug fix for [ 1331062 ] utf 7 codec broken.
Marc-André Lemburg <mal@egenix.com>
parents:
35702
diff
changeset
|
1471 } |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1472 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1473 PyObject *PyUnicode_DecodeUTF7(const char *s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1474 Py_ssize_t size, |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1475 const char *errors) |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1476 { |
|
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
|
1477 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
|
1478 } |
|
e8ac1ae4503c
The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43888
diff
changeset
|
1479 |
|
e8ac1ae4503c
The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43888
diff
changeset
|
1480 PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
|
e8ac1ae4503c
The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43888
diff
changeset
|
1481 Py_ssize_t size, |
|
e8ac1ae4503c
The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43888
diff
changeset
|
1482 const char *errors, |
|
e8ac1ae4503c
The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43888
diff
changeset
|
1483 Py_ssize_t *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
|
1484 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1485 const char *starts = s; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1486 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1487 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1488 Py_ssize_t outpos; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1489 const char *e; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1490 PyUnicodeObject *unicode; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1491 Py_UNICODE *p; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1492 const char *errmsg = ""; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1493 int inShift = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1494 unsigned int bitsleft = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1495 unsigned long charsleft = 0; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1496 int surrogate = 0; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1497 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
|
1498 PyObject *exc = NULL; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1499 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1500 unicode = _PyUnicode_New(size); |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1501 if (!unicode) |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1502 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
|
1503 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
|
1504 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
|
1505 *consumed = 0; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1506 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
|
1507 } |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1508 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1509 p = unicode->str; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1510 e = s + size; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1511 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1512 while (s < e) { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1513 Py_UNICODE ch; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1514 restart: |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1515 ch = *s; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1516 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1517 if (inShift) { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1518 if ((ch == '-') || !B64CHAR(ch)) { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1519 inShift = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1520 s++; |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
1521 |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1522 /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1523 if (bitsleft >= 6) { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1524 /* The shift sequence has a partial character in it. If |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1525 bitsleft < 6 then we could just classify it as padding |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1526 but that is not the case here */ |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1527 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1528 errmsg = "partial character in shift sequence"; |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
1529 goto utf7Error; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1530 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1531 /* According to RFC2152 the remaining bits should be zero. We |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
1532 choose to signal an error/insert a replacement character |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1533 here so indicate the potential of a misencoded character. */ |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1534 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1535 /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1536 if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1537 errmsg = "non-zero padding bits in shift sequence"; |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
1538 goto utf7Error; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1539 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1540 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1541 if (ch == '-') { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1542 if ((s < e) && (*(s) == '-')) { |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
1543 *p++ = '-'; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1544 inShift = 1; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1545 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1546 } else if (SPECIAL(ch,0,0)) { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1547 errmsg = "unexpected special character"; |
|
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
|
1548 goto utf7Error; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1549 } else { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1550 *p++ = ch; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1551 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1552 } else { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1553 charsleft = (charsleft << 6) | UB64(ch); |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1554 bitsleft += 6; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1555 s++; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1556 /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1557 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1558 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1559 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
|
1560 startinpos = s-starts; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1561 s++; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1562 if (s < e && *s == '-') { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1563 s++; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1564 *p++ = '+'; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1565 } else |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1566 { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1567 inShift = 1; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1568 bitsleft = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1569 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1570 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1571 else if (SPECIAL(ch,0,0)) { |
|
43053
6fcb873f396c
Set startinpos before calling the error handler.
Walter Dörwald <walter@livinglogic.de>
parents:
43052
diff
changeset
|
1572 startinpos = s-starts; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1573 errmsg = "unexpected special character"; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1574 s++; |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
1575 goto utf7Error; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1576 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1577 else { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1578 *p++ = ch; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1579 s++; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1580 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1581 continue; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1582 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
|
1583 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
|
1584 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
|
1585 if (unicode_decode_call_errorhandler( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1586 errors, &errorHandler, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1587 "utf7", errmsg, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1588 starts, size, &startinpos, &endinpos, &exc, &s, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1589 (PyObject **)&unicode, &outpos, &p)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1590 goto onError; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1591 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1592 |
|
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
|
1593 if (inShift && !consumed) { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1594 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
|
1595 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
|
1596 if (unicode_decode_call_errorhandler( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1597 errors, &errorHandler, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1598 "utf7", "unterminated shift sequence", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1599 starts, size, &startinpos, &endinpos, &exc, &s, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1600 (PyObject **)&unicode, &outpos, &p)) |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1601 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
|
1602 if (s < e) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1603 goto restart; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1604 } |
|
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
|
1605 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
|
1606 if(inShift) |
|
e8ac1ae4503c
The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43888
diff
changeset
|
1607 *consumed = startinpos; |
|
e8ac1ae4503c
The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43888
diff
changeset
|
1608 else |
|
e8ac1ae4503c
The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43888
diff
changeset
|
1609 *consumed = s-starts; |
|
e8ac1ae4503c
The incremental decoder for utf-7 must preserve its state between calls.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43888
diff
changeset
|
1610 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1611 |
|
30431
cd0c3f916b34
Change checks of PyUnicode_Resize() return value for clarity.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
30216
diff
changeset
|
1612 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
|
1613 goto onError; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1614 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1615 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
|
1616 Py_XDECREF(exc); |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1617 return (PyObject *)unicode; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1618 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1619 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
|
1620 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
|
1621 Py_XDECREF(exc); |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1622 Py_DECREF(unicode); |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1623 return NULL; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1624 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1625 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1626 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1627 PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1628 Py_ssize_t size, |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1629 int encodeSetO, |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1630 int encodeWhiteSpace, |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1631 const char *errors) |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1632 { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1633 PyObject *v; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1634 /* It might be possible to tighten this worst case */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1635 Py_ssize_t cbAllocated = 5 * size; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1636 int inShift = 0; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1637 Py_ssize_t i = 0; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1638 unsigned int bitsleft = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1639 unsigned long charsleft = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1640 char * out; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1641 char * start; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1642 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1643 if (size == 0) |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1644 return PyString_FromStringAndSize(NULL, 0); |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1645 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1646 v = PyString_FromStringAndSize(NULL, cbAllocated); |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1647 if (v == NULL) |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1648 return NULL; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1649 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1650 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
|
1651 for (;i < size; ++i) { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1652 Py_UNICODE ch = s[i]; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1653 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1654 if (!inShift) { |
|
31325
b4532c246a3a
Cosmetic fix for wrongly indented tabs with ts=4.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31252
diff
changeset
|
1655 if (ch == '+') { |
|
b4532c246a3a
Cosmetic fix for wrongly indented tabs with ts=4.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31252
diff
changeset
|
1656 *out++ = '+'; |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1657 *out++ = '-'; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1658 } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1659 charsleft = ch; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1660 bitsleft = 16; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1661 *out++ = '+'; |
|
31325
b4532c246a3a
Cosmetic fix for wrongly indented tabs with ts=4.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31252
diff
changeset
|
1662 /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1663 inShift = bitsleft > 0; |
|
31325
b4532c246a3a
Cosmetic fix for wrongly indented tabs with ts=4.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31252
diff
changeset
|
1664 } else { |
|
b4532c246a3a
Cosmetic fix for wrongly indented tabs with ts=4.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31252
diff
changeset
|
1665 *out++ = (char) ch; |
|
b4532c246a3a
Cosmetic fix for wrongly indented tabs with ts=4.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31252
diff
changeset
|
1666 } |
|
b4532c246a3a
Cosmetic fix for wrongly indented tabs with ts=4.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31252
diff
changeset
|
1667 } else { |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1668 if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1669 *out++ = B64(charsleft << (6-bitsleft)); |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1670 charsleft = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1671 bitsleft = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1672 /* Characters not in the BASE64 set implicitly unshift the sequence |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1673 so no '-' is required, except if the character is itself a '-' */ |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1674 if (B64CHAR(ch) || ch == '-') { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1675 *out++ = '-'; |
|
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 inShift = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1678 *out++ = (char) ch; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1679 } else { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1680 bitsleft += 16; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1681 charsleft = (charsleft << 16) | ch; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1682 /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1683 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1684 /* If the next character is special then we dont' need to terminate |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
1685 the shift sequence. If the next character is not a BASE64 character |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1686 or '-' then the shift sequence will be terminated implicitly and we |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1687 don't have to insert a '-'. */ |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1688 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1689 if (bitsleft == 0) { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1690 if (i + 1 < size) { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1691 Py_UNICODE ch2 = s[i+1]; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1692 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1693 if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
1694 |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1695 } else if (B64CHAR(ch2) || ch2 == '-') { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1696 *out++ = '-'; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1697 inShift = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1698 } else { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1699 inShift = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1700 } |
|
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 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1703 else { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1704 *out++ = '-'; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1705 inShift = 0; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1706 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1707 } |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
1708 } |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1709 } |
|
31325
b4532c246a3a
Cosmetic fix for wrongly indented tabs with ts=4.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31252
diff
changeset
|
1710 } |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1711 if (bitsleft) { |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1712 *out++= B64(charsleft << (6-bitsleft) ); |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1713 *out++ = '-'; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1714 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1715 |
|
23295
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
23294
diff
changeset
|
1716 _PyString_Resize(&v, out - start); |
|
19998
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1717 return v; |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1718 } |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1719 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1720 #undef SPECIAL |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1721 #undef B64 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1722 #undef B64CHAR |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1723 #undef UB64 |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1724 #undef ENCODE |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1725 #undef DECODE |
|
8a0c97453df4
Patch #435971: UTF-7 codec by Brian Quinlan.
Marc-André Lemburg <mal@egenix.com>
parents:
19886
diff
changeset
|
1726 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1727 /* --- UTF-8 Codec -------------------------------------------------------- */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1728 |
|
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
|
1729 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1730 char utf8_code_length[256] = { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1731 /* Map UTF-8 encoded prefix byte to sequence length. zero means |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1732 illegal prefix. see RFC 2279 for details */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1733 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
|
1734 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
|
1735 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
|
1736 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
|
1737 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
|
1738 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
|
1739 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
|
1740 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
|
1741 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
|
1742 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
|
1743 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
|
1744 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
|
1745 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1746 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1747 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1748 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1749 }; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1750 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1751 PyObject *PyUnicode_DecodeUTF8(const char *s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1752 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
|
1753 const char *errors) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1754 { |
|
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
|
1755 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
|
1756 } |
|
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
|
1757 |
|
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
|
1758 PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1759 Py_ssize_t size, |
|
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
|
1760 const char *errors, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1761 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
|
1762 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1763 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
|
1764 int n; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1765 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1766 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1767 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
|
1768 const char *e; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1769 PyUnicodeObject *unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1770 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
|
1771 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
|
1772 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
|
1773 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
|
1774 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1775 /* 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
|
1776 character count */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1777 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
|
1778 if (!unicode) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1779 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
|
1780 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
|
1781 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
|
1782 *consumed = 0; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1783 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
|
1784 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1785 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1786 /* 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
|
1787 p = unicode->str; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1788 e = s + size; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1789 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1790 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
|
1791 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
|
1792 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1793 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
|
1794 *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
|
1795 s++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1796 continue; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1797 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1798 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1799 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
|
1800 |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1801 if (s + n > e) { |
|
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
|
1802 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
|
1803 break; |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
33311
diff
changeset
|
1804 else { |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
33311
diff
changeset
|
1805 errmsg = "unexpected end of data"; |
|
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
|
1806 startinpos = s-starts; |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
33311
diff
changeset
|
1807 endinpos = size; |
|
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
|
1808 goto utf8Error; |
|
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
|
1809 } |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1810 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1811 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1812 switch (n) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1813 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1814 case 0: |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1815 errmsg = "unexpected code byte"; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1816 startinpos = 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
|
1817 endinpos = startinpos+1; |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1818 goto utf8Error; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1819 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1820 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
|
1821 errmsg = "internal 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
|
1822 startinpos = 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
|
1823 endinpos = startinpos+1; |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1824 goto utf8Error; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1825 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1826 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
|
1827 if ((s[1] & 0xc0) != 0x80) { |
|
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1828 errmsg = "invalid data"; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1829 startinpos = 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
|
1830 endinpos = startinpos+2; |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1831 goto utf8Error; |
|
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1832 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1833 ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1834 if (ch < 0x80) { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1835 startinpos = 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
|
1836 endinpos = startinpos+2; |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1837 errmsg = "illegal encoding"; |
|
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1838 goto utf8Error; |
|
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1839 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1840 else |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1841 *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
|
1842 break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1843 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1844 case 3: |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
1845 if ((s[1] & 0xc0) != 0x80 || |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1846 (s[2] & 0xc0) != 0x80) { |
|
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1847 errmsg = "invalid data"; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1848 startinpos = 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
|
1849 endinpos = startinpos+3; |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1850 goto utf8Error; |
|
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1851 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1852 ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
|
21886
abedb2418f62
Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents:
21884
diff
changeset
|
1853 if (ch < 0x0800) { |
|
abedb2418f62
Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents:
21884
diff
changeset
|
1854 /* Note: UTF-8 encodings of surrogates are considered |
|
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
|
1855 legal UTF-8 sequences; |
|
21886
abedb2418f62
Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents:
21884
diff
changeset
|
1856 |
|
abedb2418f62
Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents:
21884
diff
changeset
|
1857 XXX For wide builds (UCS-4) we should probably try |
|
abedb2418f62
Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents:
21884
diff
changeset
|
1858 to recombine the surrogates into a single code |
|
abedb2418f62
Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents:
21884
diff
changeset
|
1859 unit. |
|
abedb2418f62
Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents:
21884
diff
changeset
|
1860 */ |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1861 errmsg = "illegal encoding"; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1862 startinpos = 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
|
1863 endinpos = startinpos+3; |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1864 goto utf8Error; |
|
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1865 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1866 else |
|
21886
abedb2418f62
Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents:
21884
diff
changeset
|
1867 *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
|
1868 break; |
|
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
1869 |
|
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
1870 case 4: |
|
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
1871 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
|
1872 (s[2] & 0xc0) != 0x80 || |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1873 (s[3] & 0xc0) != 0x80) { |
|
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1874 errmsg = "invalid data"; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1875 startinpos = 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
|
1876 endinpos = startinpos+4; |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1877 goto utf8Error; |
|
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1878 } |
|
13411
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
1879 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
|
1880 ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
|
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
1881 /* validate and convert to UTF-16 */ |
|
18292
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
1882 if ((ch < 0x10000) /* minimum value allowed for 4 |
|
21886
abedb2418f62
Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents:
21884
diff
changeset
|
1883 byte encoding */ |
|
18292
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
1884 || (ch > 0x10ffff)) /* maximum value allowed for |
|
21886
abedb2418f62
Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents:
21884
diff
changeset
|
1885 UTF-16 */ |
|
18292
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
1886 { |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1887 errmsg = "illegal encoding"; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1888 startinpos = 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
|
1889 endinpos = startinpos+4; |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1890 goto utf8Error; |
|
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1891 } |
|
18324
3ba7a358ce02
use Py_UNICODE_WIDE instead of USE_UCS4_STORAGE and Py_UNICODE_SIZE
Fredrik Lundh <fredrik@pythonware.com>
parents:
18298
diff
changeset
|
1892 #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
|
1893 *p++ = (Py_UNICODE)ch; |
|
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
1894 #else |
|
13411
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
1895 /* 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
|
1896 |
|
13411
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
1897 /* 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
|
1898 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
|
1899 |
|
13411
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
1900 /* 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
|
1901 *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
|
1902 |
|
13411
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
1903 /* 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
|
1904 *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
|
1905 #endif |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1906 break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1907 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1908 default: |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1909 /* Other sizes are only needed for UCS-4 */ |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1910 errmsg = "unsupported Unicode code range"; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1911 startinpos = 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
|
1912 endinpos = startinpos+n; |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1913 goto utf8Error; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1914 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1915 s += n; |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1916 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
|
1917 |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
1918 utf8Error: |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1919 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
|
1920 if (unicode_decode_call_errorhandler( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1921 errors, &errorHandler, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1922 "utf8", errmsg, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1923 starts, size, &startinpos, &endinpos, &exc, &s, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1924 (PyObject **)&unicode, &outpos, &p)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1925 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1926 } |
|
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
|
1927 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
|
1928 *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
|
1929 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1930 /* Adjust length */ |
|
30431
cd0c3f916b34
Change checks of PyUnicode_Resize() return value for clarity.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
30216
diff
changeset
|
1931 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
|
1932 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1933 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
1934 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
|
1935 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
|
1936 return (PyObject *)unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1937 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1938 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
|
1939 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
|
1940 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
|
1941 Py_DECREF(unicode); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1942 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1943 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1944 |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1945 /* 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
|
1946 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
|
1947 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
|
1948 the excess memory at the end. |
| 23203 | 1949 */ |
|
23198
950a22eb8cc8
PyUnicode_EncodeUTF8: squash compiler wng. The difference of two
Tim Peters <tim.peters@gmail.com>
parents:
23192
diff
changeset
|
1950 PyObject * |
|
950a22eb8cc8
PyUnicode_EncodeUTF8: squash compiler wng. The difference of two
Tim Peters <tim.peters@gmail.com>
parents:
23192
diff
changeset
|
1951 PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1952 Py_ssize_t size, |
|
23198
950a22eb8cc8
PyUnicode_EncodeUTF8: squash compiler wng. The difference of two
Tim Peters <tim.peters@gmail.com>
parents:
23192
diff
changeset
|
1953 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
|
1954 { |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1955 #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
|
1956 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
1957 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
|
1958 PyObject *v; /* result string object */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1959 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
|
1960 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
|
1961 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
|
1962 char stackbuf[MAX_SHORT_UNICHARS * 4]; |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1963 |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1964 assert(s != NULL); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1965 assert(size >= 0); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1966 |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1967 if (size <= MAX_SHORT_UNICHARS) { |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1968 /* 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
|
1969 * 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
|
1970 * turns out we need. |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1971 */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1972 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
|
1973 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
|
1974 p = stackbuf; |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1975 } |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1976 else { |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1977 /* 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
|
1978 nallocated = size * 4; |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1979 if (nallocated / 4 != size) /* overflow! */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1980 return PyErr_NoMemory(); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1981 v = PyString_FromStringAndSize(NULL, nallocated); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1982 if (v == NULL) |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1983 return NULL; |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1984 p = PyString_AS_STRING(v); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1985 } |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1986 |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1987 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
|
1988 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
|
1989 |
| 23203 | 1990 if (ch < 0x80) |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1991 /* Encode ASCII */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1992 *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
|
1993 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1994 else if (ch < 0x0800) { |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
1995 /* Encode Latin-1 */ |
| 21884 | 1996 *p++ = (char)(0xc0 | (ch >> 6)); |
| 1997 *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
|
1998 } |
|
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
|
1999 else { |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2000 /* Encode UCS2 Unicode ordinals */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2001 if (ch < 0x10000) { |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2002 /* Special case: check for high surrogate */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2003 if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2004 Py_UCS4 ch2 = s[i]; |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2005 /* 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
|
2006 form a UCS4 value */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2007 if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 23203 | 2008 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
|
2009 i++; |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2010 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
|
2011 } |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2012 /* 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
|
2013 } |
|
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
2014 *p++ = (char)(0xe0 | (ch >> 12)); |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2015 *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2016 *p++ = (char)(0x80 | (ch & 0x3f)); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2017 continue; |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2018 } |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2019 encodeUCS4: |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2020 /* Encode UCS4 Unicode ordinals */ |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2021 *p++ = (char)(0xf0 | (ch >> 18)); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2022 *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2023 *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2024 *p++ = (char)(0x80 | (ch & 0x3f)); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2025 } |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2026 } |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2027 |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2028 if (v == NULL) { |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2029 /* 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
|
2030 nneeded = p - stackbuf; |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2031 assert(nneeded <= nallocated); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2032 v = PyString_FromStringAndSize(stackbuf, nneeded); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2033 } |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2034 else { |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2035 /* 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
|
2036 nneeded = p - PyString_AS_STRING(v); |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2037 assert(nneeded <= nallocated); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2038 _PyString_Resize(&v, nneeded); |
|
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2039 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2040 return v; |
| 23203 | 2041 |
|
23294
b735e89ccda7
SF patch 549375: Compromise PyUnicode_EncodeUTF8
Tim Peters <tim.peters@gmail.com>
parents:
23228
diff
changeset
|
2042 #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
|
2043 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2044 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2045 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
|
2046 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2047 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
|
2048 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2049 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2050 } |
|
14059
d0348a26b093
PyUnicode_AsUTF8String(): Don't need to explicitly incref str since
Barry Warsaw <barry@python.org>
parents:
13966
diff
changeset
|
2051 return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
|
d0348a26b093
PyUnicode_AsUTF8String(): Don't need to explicitly incref str since
Barry Warsaw <barry@python.org>
parents:
13966
diff
changeset
|
2052 PyUnicode_GET_SIZE(unicode), |
|
d0348a26b093
PyUnicode_AsUTF8String(): Don't need to explicitly incref str since
Barry Warsaw <barry@python.org>
parents:
13966
diff
changeset
|
2053 NULL); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2054 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2055 |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2056 /* --- 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
|
2057 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2058 PyObject * |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2059 PyUnicode_DecodeUTF32(const char *s, |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2060 Py_ssize_t size, |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2061 const char *errors, |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2062 int *byteorder) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2063 { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2064 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
|
2065 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2066 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2067 PyObject * |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2068 PyUnicode_DecodeUTF32Stateful(const char *s, |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2069 Py_ssize_t size, |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2070 const char *errors, |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2071 int *byteorder, |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2072 Py_ssize_t *consumed) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2073 { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2074 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
|
2075 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
|
2076 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
|
2077 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
|
2078 PyUnicodeObject *unicode; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2079 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
|
2080 #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
|
2081 int i, pairs; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2082 #else |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2083 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
|
2084 #endif |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2085 const unsigned char *q, *e; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2086 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
|
2087 const char *errmsg = ""; |
|
42650
f2f0e95a797b
Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42649
diff
changeset
|
2088 /* 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
|
2089 #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
|
f2f0e95a797b
Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42649
diff
changeset
|
2090 int iorder[] = {0, 1, 2, 3}; |
|
f2f0e95a797b
Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42649
diff
changeset
|
2091 #else |
|
f2f0e95a797b
Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42649
diff
changeset
|
2092 int iorder[] = {3, 2, 1, 0}; |
|
f2f0e95a797b
Move variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42649
diff
changeset
|
2093 #endif |
|
42652
9c4f5b1d6b9d
Move another variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42650
diff
changeset
|
2094 PyObject *errorHandler = NULL; |
|
9c4f5b1d6b9d
Move another variable declaration up.
Walter Dörwald <walter@livinglogic.de>
parents:
42650
diff
changeset
|
2095 PyObject *exc = NULL; |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2096 /* On narrow builds we split characters outside the BMP into two |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2097 codepoints => count how much extra space we need. */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2098 #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
|
2099 for (i = pairs = 0; i < size/4; i++) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2100 if (((Py_UCS4 *)s)[i] >= 0x10000) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2101 pairs++; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2102 #endif |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2103 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2104 /* This might be one to much, because of a BOM */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2105 unicode = _PyUnicode_New((size+3)/4+pairs); |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2106 if (!unicode) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2107 return NULL; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2108 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
|
2109 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
|
2110 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2111 /* Unpack UTF-32 encoded data */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2112 p = unicode->str; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2113 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
|
2114 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
|
2115 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2116 if (byteorder) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2117 bo = *byteorder; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2118 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2119 /* 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
|
2120 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
|
2121 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
|
2122 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
|
2123 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
|
2124 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
|
2125 const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2126 (q[iorder[1]] << 8) | q[iorder[0]]; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2127 #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
|
2128 if (bom == 0x0000FEFF) { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2129 q += 4; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2130 bo = -1; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2131 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2132 else if (bom == 0xFFFE0000) { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2133 q += 4; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2134 bo = 1; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2135 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2136 #else |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2137 if (bom == 0x0000FEFF) { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2138 q += 4; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2139 bo = 1; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2140 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2141 else if (bom == 0xFFFE0000) { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2142 q += 4; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2143 bo = -1; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2144 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2145 #endif |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2146 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2147 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2148 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2149 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
|
2150 /* force LE */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2151 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
|
2152 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
|
2153 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
|
2154 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
|
2155 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2156 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
|
2157 /* force BE */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2158 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
|
2159 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
|
2160 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
|
2161 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
|
2162 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2163 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2164 while (q < e) { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2165 Py_UCS4 ch; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2166 /* remaining bytes at the end? (size should be divisible by 4) */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2167 if (e-q<4) { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2168 if (consumed) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2169 break; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2170 errmsg = "truncated data"; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2171 startinpos = ((const char *)q)-starts; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2172 endinpos = ((const char *)e)-starts; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2173 goto utf32Error; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2174 /* The remaining input chars are ignored if the callback |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2175 chooses to skip the input */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2176 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2177 ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2178 (q[iorder[1]] << 8) | q[iorder[0]]; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2179 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2180 if (ch >= 0x110000) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2181 { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2182 errmsg = "codepoint not in range(0x110000)"; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2183 startinpos = ((const char *)q)-starts; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2184 endinpos = startinpos+4; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2185 goto utf32Error; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2186 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2187 #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
|
2188 if (ch >= 0x10000) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2189 { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2190 *p++ = 0xD800 | ((ch-0x10000) >> 10); |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2191 *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
|
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 else |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2194 #endif |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2195 *p++ = ch; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2196 q += 4; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2197 continue; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2198 utf32Error: |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2199 outpos = p-PyUnicode_AS_UNICODE(unicode); |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2200 if (unicode_decode_call_errorhandler( |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2201 errors, &errorHandler, |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2202 "utf32", errmsg, |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2203 starts, size, &startinpos, &endinpos, &exc, &s, |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2204 (PyObject **)&unicode, &outpos, &p)) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2205 goto onError; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2206 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2207 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2208 if (byteorder) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2209 *byteorder = bo; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2210 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2211 if (consumed) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2212 *consumed = (const char *)q-starts; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2213 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2214 /* Adjust length */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2215 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
|
2216 goto onError; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2217 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2218 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
|
2219 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
|
2220 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
|
2221 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2222 onError: |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2223 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
|
2224 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
|
2225 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
|
2226 return NULL; |
|
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 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2229 PyObject * |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2230 PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2231 Py_ssize_t size, |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2232 const char *errors, |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2233 int byteorder) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2234 { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2235 PyObject *v; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2236 unsigned char *p; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2237 #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
|
2238 int i, pairs; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2239 #else |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2240 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
|
2241 #endif |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2242 /* 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
|
2243 #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
|
2244 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
|
2245 #else |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2246 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
|
2247 #endif |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2248 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2249 #define STORECHAR(CH) \ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2250 do { \ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2251 p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2252 p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2253 p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2254 p[iorder[0]] = (CH) & 0xff; \ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2255 p += 4; \ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2256 } while(0) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2257 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2258 /* 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
|
2259 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
|
2260 #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
|
2261 for (i = pairs = 0; i < size-1; i++) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2262 if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2263 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2264 pairs++; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2265 #endif |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2266 v = PyString_FromStringAndSize(NULL, |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2267 4 * (size - pairs + (byteorder == 0))); |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2268 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
|
2269 return NULL; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2270 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2271 p = (unsigned char *)PyString_AS_STRING(v); |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2272 if (byteorder == 0) |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2273 STORECHAR(0xFEFF); |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2274 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
|
2275 return v; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2276 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2277 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
|
2278 /* force LE */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2279 iorder[0] = 0; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2280 iorder[1] = 1; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2281 iorder[2] = 2; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2282 iorder[3] = 3; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2283 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2284 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
|
2285 /* force BE */ |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2286 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
|
2287 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
|
2288 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
|
2289 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
|
2290 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2291 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2292 while (size-- > 0) { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2293 Py_UCS4 ch = *s++; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2294 #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
|
2295 if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2296 Py_UCS4 ch2 = *s; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2297 if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2298 ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2299 s++; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2300 size--; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2301 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2302 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2303 #endif |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2304 STORECHAR(ch); |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2305 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2306 return v; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2307 #undef STORECHAR |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2308 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2309 |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2310 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
|
2311 { |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2312 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
|
2313 PyErr_BadArgument(); |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2314 return NULL; |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2315 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2316 return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2317 PyUnicode_GET_SIZE(unicode), |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2318 NULL, |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2319 0); |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2320 } |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
42402
diff
changeset
|
2321 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2322 /* --- UTF-16 Codec ------------------------------------------------------- */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2323 |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2324 PyObject * |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2325 PyUnicode_DecodeUTF16(const char *s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2326 Py_ssize_t size, |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2327 const char *errors, |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2328 int *byteorder) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2329 { |
|
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
|
2330 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
|
2331 } |
|
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
|
2332 |
|
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
|
2333 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
|
2334 PyUnicode_DecodeUTF16Stateful(const char *s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2335 Py_ssize_t size, |
|
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
|
2336 const char *errors, |
|
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
|
2337 int *byteorder, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2338 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
|
2339 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2340 const char *starts = s; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2341 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2342 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2343 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
|
2344 PyUnicodeObject *unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2345 Py_UNICODE *p; |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2346 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
|
2347 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
|
2348 const char *errmsg = ""; |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2349 /* 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
|
2350 #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
|
2351 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
|
2352 #else |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2353 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
|
2354 #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
|
2355 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
|
2356 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
|
2357 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2358 /* 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
|
2359 character count */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2360 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
|
2361 if (!unicode) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2362 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2363 if (size == 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2364 return (PyObject *)unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2365 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2366 /* 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
|
2367 p = unicode->str; |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2368 q = (unsigned char *)s; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2369 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
|
2370 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2371 if (byteorder) |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2372 bo = *byteorder; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2373 |
|
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
|
2374 /* 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
|
2375 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
|
2376 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
|
2377 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
|
2378 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
|
2379 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
|
2380 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
|
2381 #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
|
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
|
2382 if (bom == 0xFEFF) { |
|
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
|
2383 q += 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
|
2384 bo = -1; |
|
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
|
2385 } |
|
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
|
2386 else if (bom == 0xFFFE) { |
|
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
|
2387 q += 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
|
2388 bo = 1; |
|
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
|
2389 } |
|
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
|
2390 #else |
|
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
|
2391 if (bom == 0xFEFF) { |
|
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
|
2392 q += 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
|
2393 bo = 1; |
|
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
|
2394 } |
|
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
|
2395 else if (bom == 0xFFFE) { |
|
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
|
2396 q += 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
|
2397 bo = -1; |
|
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
|
2398 } |
|
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
|
2399 #endif |
|
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
|
2400 } |
|
fd5f83188778
This patch changes the behaviour of the UTF-16 codec family. Only the
Marc-André Lemburg <mal@egenix.com>
parents:
17700
diff
changeset
|
2401 } |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2402 |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2403 if (bo == -1) { |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2404 /* force LE */ |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2405 ihi = 1; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2406 ilo = 0; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2407 } |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2408 else if (bo == 1) { |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2409 /* force BE */ |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2410 ihi = 0; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2411 ilo = 1; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2412 } |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2413 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2414 while (q < e) { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2415 Py_UNICODE ch; |
|
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
|
2416 /* remaining bytes at the end? (size should be even) */ |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2417 if (e-q<2) { |
|
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
|
2418 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
|
2419 break; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2420 errmsg = "truncated data"; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2421 startinpos = ((const char *)q)-starts; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2422 endinpos = ((const char *)e)-starts; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2423 goto utf16Error; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2424 /* The remaining input chars are ignored if the callback |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2425 chooses to skip the input */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2426 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2427 ch = (q[ihi] << 8) | q[ilo]; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2428 |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2429 q += 2; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2430 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2431 if (ch < 0xD800 || ch > 0xDFFF) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2432 *p++ = ch; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2433 continue; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2434 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2435 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2436 /* UTF-16 code pair: */ |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
2437 if (q >= e) { |
|
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
2438 errmsg = "unexpected end of data"; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2439 startinpos = (((const char *)q)-2)-starts; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2440 endinpos = ((const char *)e)-starts; |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
2441 goto utf16Error; |
|
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
2442 } |
|
18295
20e3a6e10c24
When decoding UTF-16, don't assume that the buffer is in native endianness
Martin v. Löwis <martin@v.loewis.de>
parents:
18292
diff
changeset
|
2443 if (0xD800 <= ch && ch <= 0xDBFF) { |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2444 Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2445 q += 2; |
|
18295
20e3a6e10c24
When decoding UTF-16, don't assume that the buffer is in native endianness
Martin v. Löwis <martin@v.loewis.de>
parents:
18292
diff
changeset
|
2446 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
|
2447 #ifndef Py_UNICODE_WIDE |
|
18774
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2448 *p++ = ch; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2449 *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
|
2450 #else |
|
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2451 *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
|
2452 #endif |
|
18292
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2453 continue; |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
2454 } |
|
18292
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2455 else { |
|
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2456 errmsg = "illegal UTF-16 surrogate"; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2457 startinpos = (((const char *)q)-4)-starts; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2458 endinpos = startinpos+2; |
|
18292
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2459 goto utf16Error; |
|
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2460 } |
|
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2461 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2462 } |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
2463 errmsg = "illegal encoding"; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2464 startinpos = (((const char *)q)-2)-starts; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2465 endinpos = startinpos+2; |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
2466 /* Fall through to report the error */ |
|
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
2467 |
|
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
2468 utf16Error: |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2469 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
|
2470 if (unicode_decode_call_errorhandler( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2471 errors, &errorHandler, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2472 "utf16", errmsg, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2473 starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2474 (PyObject **)&unicode, &outpos, &p)) |
|
13635
dfb7ecfffe3d
Fixed problems with UTF error reporting macros and some formatting bugs.
Marc-André Lemburg <mal@egenix.com>
parents:
13633
diff
changeset
|
2475 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2476 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2477 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2478 if (byteorder) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2479 *byteorder = bo; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2480 |
|
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
|
2481 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
|
2482 *consumed = (const char *)q-starts; |
|
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
|
2483 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2484 /* Adjust length */ |
|
30431
cd0c3f916b34
Change checks of PyUnicode_Resize() return value for clarity.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
30216
diff
changeset
|
2485 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
|
2486 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2487 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2488 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
|
2489 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
|
2490 return (PyObject *)unicode; |
|
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 onError: |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2493 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
|
2494 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
|
2495 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
|
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 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2498 |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2499 PyObject * |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2500 PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2501 Py_ssize_t size, |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2502 const char *errors, |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2503 int byteorder) |
|
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 PyObject *v; |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2506 unsigned char *p; |
|
31238
4edf200a8118
SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31199
diff
changeset
|
2507 #ifdef Py_UNICODE_WIDE |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2508 int 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
|
2509 #else |
|
4edf200a8118
SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31199
diff
changeset
|
2510 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
|
2511 #endif |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2512 /* 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
|
2513 #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
|
2514 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
|
2515 #else |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2516 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
|
2517 #endif |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2518 |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2519 #define STORECHAR(CH) \ |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2520 do { \ |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2521 p[ihi] = ((CH) >> 8) & 0xff; \ |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2522 p[ilo] = (CH) & 0xff; \ |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2523 p += 2; \ |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2524 } 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
|
2525 |
|
31238
4edf200a8118
SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31199
diff
changeset
|
2526 #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
|
2527 for (i = pairs = 0; i < size; i++) |
|
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2528 if (s[i] >= 0x10000) |
|
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2529 pairs++; |
|
31238
4edf200a8118
SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31199
diff
changeset
|
2530 #endif |
|
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
|
2531 v = PyString_FromStringAndSize(NULL, |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2532 2 * (size + pairs + (byteorder == 0))); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2533 if (v == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2534 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
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 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
|
2537 if (byteorder == 0) |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2538 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
|
2539 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
|
2540 return v; |
|
19121
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 if (byteorder == -1) { |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2543 /* force LE */ |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2544 ihi = 1; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2545 ilo = 0; |
|
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 else if (byteorder == 1) { |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2548 /* force BE */ |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2549 ihi = 0; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2550 ilo = 1; |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2551 } |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2552 |
|
18292
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2553 while (size-- > 0) { |
|
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2554 Py_UNICODE ch = *s++; |
|
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2555 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
|
2556 #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
|
2557 if (ch >= 0x10000) { |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2558 ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2559 ch = 0xD800 | ((ch-0x10000) >> 10); |
|
18292
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2560 } |
|
31238
4edf200a8118
SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31199
diff
changeset
|
2561 #endif |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2562 STORECHAR(ch); |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2563 if (ch2) |
|
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2564 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
|
2565 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2566 return v; |
|
19121
77e405f3a942
SF patch #438013 Remove 2-byte Py_UCS2 assumptions
Tim Peters <tim.peters@gmail.com>
parents:
18933
diff
changeset
|
2567 #undef STORECHAR |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2568 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2569 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2570 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
|
2571 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2572 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
|
2573 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2574 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2575 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2576 return PyUnicode_EncodeUTF16(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
|
2577 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
|
2578 NULL, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2579 0); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2580 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2581 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2582 /* --- Unicode Escape Codec ----------------------------------------------- */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2583 |
|
16200
3484670c37f2
Move uchhash functionality into unicodedata (after the recent
Fredrik Lundh <fredrik@pythonware.com>
parents:
16064
diff
changeset
|
2584 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
|
2585 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2586 PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2587 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
|
2588 const char *errors) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2589 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2590 const char *starts = s; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2591 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2592 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2593 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
|
2594 int i; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2595 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
|
2596 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
|
2597 const char *end; |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2598 char* message; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2599 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
|
2600 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
|
2601 PyObject *exc = NULL; |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2602 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2603 /* 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
|
2604 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
|
2605 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
|
2606 (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
|
2607 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
|
2608 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
|
2609 if (v == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2610 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2611 if (size == 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2612 return (PyObject *)v; |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2613 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2614 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
|
2615 end = s + size; |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2616 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2617 while (s < end) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2618 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
|
2619 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
|
2620 int digits; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2621 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2622 /* 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
|
2623 if (*s != '\\') { |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2624 *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
|
2625 continue; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2626 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2627 |
|
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 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
|
2629 /* \ - Escapes */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2630 s++; |
|
43710
124cbf1a0e3e
Backport fixes for the code that decodes octal escapes (and for PyString
Guido van Rossum <guido@python.org>
parents:
43053
diff
changeset
|
2631 c = *s++; |
|
124cbf1a0e3e
Backport fixes for the code that decodes octal escapes (and for PyString
Guido van Rossum <guido@python.org>
parents:
43053
diff
changeset
|
2632 if (s > end) |
|
124cbf1a0e3e
Backport fixes for the code that decodes octal escapes (and for PyString
Guido van Rossum <guido@python.org>
parents:
43053
diff
changeset
|
2633 c = '\0'; /* Invalid after \ */ |
|
124cbf1a0e3e
Backport fixes for the code that decodes octal escapes (and for PyString
Guido van Rossum <guido@python.org>
parents:
43053
diff
changeset
|
2634 switch (c) { |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2635 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2636 /* \x escapes */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2637 case '\n': break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2638 case '\\': *p++ = '\\'; break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2639 case '\'': *p++ = '\''; break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2640 case '\"': *p++ = '\"'; break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2641 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
|
2642 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
|
2643 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
|
2644 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
|
2645 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
|
2646 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
|
2647 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
|
2648 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2649 /* \OOO (octal) escapes */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2650 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
|
2651 case '4': case '5': case '6': case '7': |
| 12544 | 2652 x = s[-1] - '0'; |
|
43710
124cbf1a0e3e
Backport fixes for the code that decodes octal escapes (and for PyString
Guido van Rossum <guido@python.org>
parents:
43053
diff
changeset
|
2653 if (s < end && '0' <= *s && *s <= '7') { |
| 12544 | 2654 x = (x<<3) + *s++ - '0'; |
|
43710
124cbf1a0e3e
Backport fixes for the code that decodes octal escapes (and for PyString
Guido van Rossum <guido@python.org>
parents:
43053
diff
changeset
|
2655 if (s < end && '0' <= *s && *s <= '7') |
| 12544 | 2656 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
|
2657 } |
| 12544 | 2658 *p++ = x; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2659 break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2660 |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2661 /* hex escapes */ |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2662 /* \xXX */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2663 case 'x': |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2664 digits = 2; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2665 message = "truncated \\xXX escape"; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2666 goto hexescape; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2667 |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2668 /* \uXXXX */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2669 case 'u': |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2670 digits = 4; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2671 message = "truncated \\uXXXX escape"; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2672 goto hexescape; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2673 |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2674 /* \UXXXXXXXX */ |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2675 case 'U': |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2676 digits = 8; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2677 message = "truncated \\UXXXXXXXX escape"; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2678 hexescape: |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2679 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
|
2680 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
|
2681 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
|
2682 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
|
2683 if (unicode_decode_call_errorhandler( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2684 errors, &errorHandler, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2685 "unicodeescape", "end of string in escape sequence", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2686 starts, size, &startinpos, &endinpos, &exc, &s, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2687 (PyObject **)&v, &outpos, &p)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2688 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
|
2689 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
|
2690 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2691 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
|
2692 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
|
2693 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
|
2694 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
|
2695 if (unicode_decode_call_errorhandler( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2696 errors, &errorHandler, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2697 "unicodeescape", message, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2698 starts, size, &startinpos, &endinpos, &exc, &s, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2699 (PyObject **)&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
|
2700 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
|
2701 goto nextByte; |
|
14388
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2702 } |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2703 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
|
2704 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
|
2705 chr += c - '0'; |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2706 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
|
2707 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
|
2708 else |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2709 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
|
2710 } |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2711 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
|
2712 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
|
2713 /* _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
|
2714 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
|
2715 break; |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2716 store: |
|
14388
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2717 /* 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
|
2718 if (chr <= 0xffff) |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2719 /* UCS-2 character */ |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2720 *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
|
2721 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
|
2722 /* UCS-4 character. Either store directly, or as |
| 22521 | 2723 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
|
2724 #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
|
2725 *p++ = chr; |
|
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2726 #else |
|
14388
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2727 chr -= 0x10000L; |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2728 *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
|
2729 *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
|
2730 #endif |
|
14388
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2731 } 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
|
2732 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
|
2733 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
|
2734 if (unicode_decode_call_errorhandler( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2735 errors, &errorHandler, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2736 "unicodeescape", "illegal Unicode character", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2737 starts, size, &startinpos, &endinpos, &exc, &s, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2738 (PyObject **)&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
|
2739 goto onError; |
|
ef2714bb0730
changed \x to consume exactly two hex digits, also for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
14067
diff
changeset
|
2740 } |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2741 break; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2742 |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2743 /* \N{name} */ |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2744 case 'N': |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2745 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
|
2746 if (ucnhash_CAPI == NULL) { |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2747 /* load the unicode data module */ |
|
36877
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36675
diff
changeset
|
2748 PyObject *m, *api; |
|
44439
0cbe94bea2c7
Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Christian Heimes <christian@cheimes.de>
parents:
44350
diff
changeset
|
2749 m = PyImport_ImportModuleNoBlock("unicodedata"); |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2750 if (m == NULL) |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2751 goto ucnhashError; |
|
36877
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36675
diff
changeset
|
2752 api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2753 Py_DECREF(m); |
|
36877
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36675
diff
changeset
|
2754 if (api == NULL) |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2755 goto ucnhashError; |
|
37596
1b078493d538
More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37306
diff
changeset
|
2756 ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
|
36877
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36675
diff
changeset
|
2757 Py_DECREF(api); |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2758 if (ucnhash_CAPI == NULL) |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2759 goto ucnhashError; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2760 } |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2761 if (*s == '{') { |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2762 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
|
2763 /* 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
|
2764 while (*s != '}' && s < end) |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2765 s++; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2766 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
|
2767 /* 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
|
2768 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
|
2769 s++; |
|
36924
48e0a5e39403
Update Unicode database to Unicode 4.1.
Martin v. Löwis <martin@v.loewis.de>
parents:
36882
diff
changeset
|
2770 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
|
2771 goto store; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2772 } |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2773 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2774 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
|
2775 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
|
2776 if (unicode_decode_call_errorhandler( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2777 errors, &errorHandler, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2778 "unicodeescape", message, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2779 starts, size, &startinpos, &endinpos, &exc, &s, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2780 (PyObject **)&v, &outpos, &p)) |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2781 goto onError; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2782 break; |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2783 |
|
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2784 default: |
| 22521 | 2785 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
|
2786 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
|
2787 s--; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2788 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
|
2789 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
|
2790 if (unicode_decode_call_errorhandler( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2791 errors, &errorHandler, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2792 "unicodeescape", message, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2793 starts, size, &startinpos, &endinpos, &exc, &s, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2794 (PyObject **)&v, &outpos, &p)) |
| 22521 | 2795 goto onError; |
| 2796 } | |
| 2797 else { | |
| 2798 *p++ = '\\'; | |
| 2799 *p++ = (unsigned char)s[-1]; | |
| 2800 } | |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2801 break; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2802 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2803 nextByte: |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2804 ; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
2805 } |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
2806 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
|
2807 goto onError; |
|
30160
3329b058db20
Fix another refcounting leak (in PyUnicode_DecodeUnicodeEscape()).
Walter Dörwald <walter@livinglogic.de>
parents:
30149
diff
changeset
|
2808 Py_XDECREF(errorHandler); |
|
3329b058db20
Fix another refcounting leak (in PyUnicode_DecodeUnicodeEscape()).
Walter Dörwald <walter@livinglogic.de>
parents:
30149
diff
changeset
|
2809 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
|
2810 return (PyObject *)v; |
| 22521 | 2811 |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2812 ucnhashError: |
|
16200
3484670c37f2
Move uchhash functionality into unicodedata (after the recent
Fredrik Lundh <fredrik@pythonware.com>
parents:
16064
diff
changeset
|
2813 PyErr_SetString( |
|
3484670c37f2
Move uchhash functionality into unicodedata (after the recent
Fredrik Lundh <fredrik@pythonware.com>
parents:
16064
diff
changeset
|
2814 PyExc_UnicodeError, |
|
3484670c37f2
Move uchhash functionality into unicodedata (after the recent
Fredrik Lundh <fredrik@pythonware.com>
parents:
16064
diff
changeset
|
2815 "\\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
|
2816 ); |
|
36877
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36675
diff
changeset
|
2817 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
|
2818 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
|
2819 Py_XDECREF(exc); |
|
16064
a97aa9238771
Better error message if ucnhash cannot be found (obscure attribute
Fredrik Lundh <fredrik@pythonware.com>
parents:
16029
diff
changeset
|
2820 return NULL; |
|
a97aa9238771
Better error message if ucnhash cannot be found (obscure attribute
Fredrik Lundh <fredrik@pythonware.com>
parents:
16029
diff
changeset
|
2821 |
|
16667
725238293869
reorganized PyUnicode_DecodeUnicodeEscape a bit (in order to make it
Fredrik Lundh <fredrik@pythonware.com>
parents:
16332
diff
changeset
|
2822 onError: |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2823 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
|
2824 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
|
2825 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
|
2826 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2827 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2828 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2829 /* 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
|
2830 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2831 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
|
2832 appropriate. |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2833 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2834 */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2835 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
2836 Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
|
38347
6d8615f9f1cc
use Py_LOCAL also for string and unicode objects
Fredrik Lundh <fredrik@pythonware.com>
parents:
38339
diff
changeset
|
2837 Py_ssize_t size, |
|
6d8615f9f1cc
use Py_LOCAL also for string and unicode objects
Fredrik Lundh <fredrik@pythonware.com>
parents:
38339
diff
changeset
|
2838 Py_UNICODE ch) |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2839 { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2840 /* 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
|
2841 |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2842 while (size-- > 0) { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2843 if (*s == ch) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2844 return s; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2845 s++; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2846 } |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2847 |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2848 return NULL; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
2849 } |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11997
diff
changeset
|
2850 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2851 static |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2852 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
|
2853 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
|
2854 int quotes) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2855 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2856 PyObject *repr; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2857 char *p; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2858 |
|
16226
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
2859 static const char *hexdigit = "0123456789abcdef"; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2860 |
|
39621
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2861 /* 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
|
2862 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
|
2863 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
|
2864 */ |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2865 /* Initial allocation is based on the longest-possible unichr |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2866 escape. |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2867 |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2868 In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2869 unichr, so in this case it's the longest unichr escape. In |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2870 narrow (UTF-16) builds this is five chars per source unichr |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2871 since there are two unichrs in the surrogate pair, so in narrow |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2872 (UTF-16) builds it's not the longest unichr escape. |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2873 |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2874 In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2875 so in the narrow (UTF-16) build case it's the longest unichr |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2876 escape. |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2877 */ |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2878 |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2879 repr = PyString_FromStringAndSize(NULL, |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2880 2 |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2881 #ifdef Py_UNICODE_WIDE |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2882 + 10*size |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2883 #else |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2884 + 6*size |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2885 #endif |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2886 + 1); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2887 if (repr == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2888 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2889 |
|
18828
081d96fbcf43
Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents:
18774
diff
changeset
|
2890 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
|
2891 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2892 if (quotes) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2893 *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
|
2894 *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
|
2895 !findchar(s, size, '"')) ? '"' : '\''; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2896 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2897 while (size-- > 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2898 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
|
2899 |
|
36056
f1ae11457d34
Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents:
35999
diff
changeset
|
2900 /* 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
|
2901 if ((quotes && |
|
f1ae11457d34
Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
Hye-Shik Chang <hyeshik@gmail.com>
parents:
35999
diff
changeset
|
2902 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
|
2903 *p++ = '\\'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2904 *p++ = (char) ch; |
|
20018
8aa1ada33f64
Fix a bug in rendering of \\ by repr() -- it rendered as \\\ instead
Guido van Rossum <guido@python.org>
parents:
20007
diff
changeset
|
2905 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
|
2906 } |
|
18828
081d96fbcf43
Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents:
18774
diff
changeset
|
2907 |
|
18773
743f8e1d953c
#ifdef out generation of \U escapes unless Py_UNICODE_WIDE. This
Guido van Rossum <guido@python.org>
parents:
18324
diff
changeset
|
2908 #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
|
2909 /* 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
|
2910 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
|
2911 *p++ = '\\'; |
|
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2912 *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
|
2913 *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
|
2914 *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
|
2915 *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
|
2916 *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
|
2917 *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
|
2918 *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
|
2919 *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
|
2920 *p++ = hexdigit[ch & 0x0000000F]; |
|
081d96fbcf43
Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents:
18774
diff
changeset
|
2921 continue; |
|
18292
ffbb9c918ac0
Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents:
18280
diff
changeset
|
2922 } |
|
39621
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2923 #else |
|
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2924 /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
|
18774
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2925 else if (ch >= 0xD800 && ch < 0xDC00) { |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2926 Py_UNICODE ch2; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2927 Py_UCS4 ucs; |
|
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
|
2928 |
|
18774
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2929 ch2 = *s++; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2930 size--; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2931 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2932 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2933 *p++ = '\\'; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2934 *p++ = 'U'; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2935 *p++ = hexdigit[(ucs >> 28) & 0x0000000F]; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2936 *p++ = hexdigit[(ucs >> 24) & 0x0000000F]; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2937 *p++ = hexdigit[(ucs >> 20) & 0x0000000F]; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2938 *p++ = hexdigit[(ucs >> 16) & 0x0000000F]; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2939 *p++ = hexdigit[(ucs >> 12) & 0x0000000F]; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2940 *p++ = hexdigit[(ucs >> 8) & 0x0000000F]; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2941 *p++ = hexdigit[(ucs >> 4) & 0x0000000F]; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2942 *p++ = hexdigit[ucs & 0x0000000F]; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2943 continue; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2944 } |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2945 /* Fall through: isolated surrogates are copied as-is */ |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2946 s--; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2947 size++; |
|
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2948 } |
|
39621
e8a65bbd6a29
Patch #1541585: fix buffer overrun when performing repr() on
Neal Norwitz <nnorwitz@gmail.com>
parents:
39491
diff
changeset
|
2949 #endif |
|
18774
57684d3cefce
Make the unicode-escape and the UTF-16 codecs handle surrogates
Marc-André Lemburg <mal@egenix.com>
parents:
18773
diff
changeset
|
2950 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2951 /* 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
|
2952 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
|
2953 *p++ = '\\'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2954 *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
|
2955 *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
|
2956 *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
|
2957 *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
|
2958 *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
|
2959 } |
|
18828
081d96fbcf43
Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents:
18774
diff
changeset
|
2960 |
|
16226
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
2961 /* 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
|
2962 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
|
2963 *p++ = '\\'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
2964 *p++ = 't'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
2965 } |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
2966 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
|
2967 *p++ = '\\'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
2968 *p++ = 'n'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
2969 } |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
2970 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
|
2971 *p++ = '\\'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
2972 *p++ = 'r'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
2973 } |
|
18828
081d96fbcf43
Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents:
18774
diff
changeset
|
2974 |
|
16226
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
2975 /* 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
|
2976 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
|
2977 *p++ = '\\'; |
|
16226
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
2978 *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
|
2979 *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
|
2980 *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
|
2981 } |
|
18828
081d96fbcf43
Fix for bug #444493: u'\U00010001' segfaults with current CVS on
Marc-André Lemburg <mal@egenix.com>
parents:
18774
diff
changeset
|
2982 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2983 /* 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
|
2984 else |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2985 *p++ = (char) ch; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2986 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2987 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
|
2988 *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
|
2989 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2990 *p = '\0'; |
|
23295
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
23294
diff
changeset
|
2991 _PyString_Resize(&repr, p - PyString_AS_STRING(repr)); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2992 return repr; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2993 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2994 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2995 PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
2996 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
|
2997 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2998 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
|
2999 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3000 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3001 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
|
3002 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3003 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
|
3004 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3005 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3006 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3007 return PyUnicode_EncodeUnicodeEscape(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
|
3008 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
|
3009 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3010 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3011 /* --- 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
|
3012 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3013 PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3014 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
|
3015 const char *errors) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3016 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3017 const char *starts = s; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3018 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3019 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3020 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
|
3021 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
|
3022 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
|
3023 const char *end; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3024 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
|
3025 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
|
3026 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
|
3027 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3028 /* 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
|
3029 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
|
3030 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
|
3031 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
|
3032 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
|
3033 if (v == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3034 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3035 if (size == 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3036 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
|
3037 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
|
3038 end = s + size; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3039 while (s < end) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3040 unsigned char c; |
|
22423
b523a470a0ce
Do not insert characters for unicode-escape decoders if the error mode
Martin v. Löwis <martin@v.loewis.de>
parents:
22087
diff
changeset
|
3041 Py_UCS4 x; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3042 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
|
3043 int count; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3044 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3045 /* 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
|
3046 if (*s != '\\') { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3047 *p++ = (unsigned char)*s++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3048 continue; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3049 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3050 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
|
3051 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3052 /* \u-escapes are only interpreted iff the number of leading |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3053 backslashes if odd */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3054 bs = s; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3055 for (;s < end;) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3056 if (*s != '\\') |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3057 break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3058 *p++ = (unsigned char)*s++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3059 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3060 if (((s - bs) & 1) == 0 || |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3061 s >= end || |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3062 (*s != 'u' && *s != 'U')) { |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3063 continue; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3064 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3065 p--; |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3066 count = *s=='u' ? 4 : 8; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3067 s++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3068 |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3069 /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3070 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
|
3071 for (x = 0, i = 0; i < count; ++i, ++s) { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3072 c = (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
|
3073 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
|
3074 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
|
3075 if (unicode_decode_call_errorhandler( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3076 errors, &errorHandler, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3077 "rawunicodeescape", "truncated \\uXXXX", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3078 starts, size, &startinpos, &endinpos, &exc, &s, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3079 (PyObject **)&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
|
3080 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
|
3081 goto nextByte; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3082 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3083 x = (x<<4) & ~0xF; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3084 if (c >= '0' && c <= '9') |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3085 x += c - '0'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3086 else if (c >= 'a' && c <= 'f') |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3087 x += 10 + c - 'a'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3088 else |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3089 x += 10 + c - 'A'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3090 } |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3091 #ifndef Py_UNICODE_WIDE |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3092 if (x > 0x10000) { |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3093 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
|
3094 errors, &errorHandler, |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3095 "rawunicodeescape", "\\Uxxxxxxxx out of range", |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3096 starts, size, &startinpos, &endinpos, &exc, &s, |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3097 (PyObject **)&v, &outpos, &p)) |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3098 goto onError; |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3099 } |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3100 #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
|
3101 *p++ = x; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3102 nextByte: |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3103 ; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3104 } |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
3105 if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 12329 | 3106 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
|
3107 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
|
3108 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
|
3109 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
|
3110 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3111 onError: |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3112 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
|
3113 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
|
3114 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
|
3115 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3116 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3117 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3118 PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3119 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
|
3120 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3121 PyObject *repr; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3122 char *p; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3123 char *q; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3124 |
|
16226
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16200
diff
changeset
|
3125 static const char *hexdigit = "0123456789abcdef"; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3126 |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3127 #ifdef Py_UNICODE_WIDE |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3128 repr = PyString_FromStringAndSize(NULL, 10 * size); |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3129 #else |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3130 repr = PyString_FromStringAndSize(NULL, 6 * size); |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3131 #endif |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3132 if (repr == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3133 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
|
3134 if (size == 0) |
|
91ddeed12fa7
Fixed a couple of instances where a 0-length string was being
Marc-André Lemburg <mal@egenix.com>
parents:
13946
diff
changeset
|
3135 return repr; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3136 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3137 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
|
3138 while (size-- > 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3139 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
|
3140 #ifdef Py_UNICODE_WIDE |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3141 /* Map 32-bit characters to '\Uxxxxxxxx' */ |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3142 if (ch >= 0x10000) { |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3143 *p++ = '\\'; |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3144 *p++ = 'U'; |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3145 *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
|
3146 *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
|
3147 *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
|
3148 *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
|
3149 *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
|
3150 *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
|
3151 *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
|
3152 *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
|
3153 } |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3154 else |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
3155 #endif |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3156 /* Map 16-bit characters to '\uxxxx' */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3157 if (ch >= 256) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3158 *p++ = '\\'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3159 *p++ = 'u'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3160 *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
|
3161 *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
|
3162 *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
|
3163 *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
|
3164 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3165 /* 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
|
3166 else |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3167 *p++ = (char) ch; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3168 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3169 *p = '\0'; |
|
23295
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
23294
diff
changeset
|
3170 _PyString_Resize(&repr, p - q); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3171 return repr; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3172 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3173 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3174 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
|
3175 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3176 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
|
3177 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3178 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3179 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3180 return PyUnicode_EncodeRawUnicodeEscape(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
|
3181 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
|
3182 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3183 |
| 35485 | 3184 /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3185 | |
| 3186 PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3187 Py_ssize_t size, |
| 35485 | 3188 const char *errors) |
| 3189 { | |
| 3190 const char *starts = s; | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3191 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3192 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3193 Py_ssize_t outpos; |
| 35485 | 3194 PyUnicodeObject *v; |
| 3195 Py_UNICODE *p; | |
| 3196 const char *end; | |
| 3197 const char *reason; | |
| 3198 PyObject *errorHandler = NULL; | |
| 3199 PyObject *exc = NULL; | |
| 3200 | |
|
36236
ecd423718a32
Fix icc warnings: remove (sometimes) unused variable conditionally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36203
diff
changeset
|
3201 #ifdef Py_UNICODE_WIDE |
|
ecd423718a32
Fix icc warnings: remove (sometimes) unused variable conditionally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36203
diff
changeset
|
3202 Py_UNICODE unimax = PyUnicode_GetMax(); |
|
ecd423718a32
Fix icc warnings: remove (sometimes) unused variable conditionally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36203
diff
changeset
|
3203 #endif |
|
ecd423718a32
Fix icc warnings: remove (sometimes) unused variable conditionally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36203
diff
changeset
|
3204 |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39734
diff
changeset
|
3205 /* XXX overflow detection missing */ |
| 35485 | 3206 v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3207 if (v == NULL) | |
| 3208 goto onError; | |
| 3209 if (PyUnicode_GetSize((PyObject *)v) == 0) | |
| 3210 return (PyObject *)v; | |
| 3211 p = PyUnicode_AS_UNICODE(v); | |
| 3212 end = s + size; | |
| 3213 | |
| 3214 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
|
3215 memcpy(p, s, sizeof(Py_UNICODE)); |
| 35485 | 3216 /* We have to sanity check the raw data, otherwise doom looms for |
| 3217 some malformed UCS-4 data. */ | |
| 3218 if ( | |
| 3219 #ifdef Py_UNICODE_WIDE | |
| 3220 *p > unimax || *p < 0 || | |
| 3221 #endif | |
| 3222 end-s < Py_UNICODE_SIZE | |
| 3223 ) | |
| 3224 { | |
| 3225 startinpos = s - starts; | |
| 3226 if (end-s < Py_UNICODE_SIZE) { | |
| 3227 endinpos = end-starts; | |
| 3228 reason = "truncated input"; | |
| 3229 } | |
| 3230 else { | |
| 3231 endinpos = s - starts + Py_UNICODE_SIZE; | |
| 3232 reason = "illegal code point (> 0x10FFFF)"; | |
| 3233 } | |
| 3234 outpos = p - PyUnicode_AS_UNICODE(v); | |
| 3235 if (unicode_decode_call_errorhandler( | |
| 3236 errors, &errorHandler, | |
| 3237 "unicode_internal", reason, | |
| 3238 starts, size, &startinpos, &endinpos, &exc, &s, | |
| 3239 (PyObject **)&v, &outpos, &p)) { | |
| 3240 goto onError; | |
| 3241 } | |
| 3242 } | |
| 3243 else { | |
| 3244 p++; | |
| 3245 s += Py_UNICODE_SIZE; | |
| 3246 } | |
| 3247 } | |
| 3248 | |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
3249 if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 35485 | 3250 goto onError; |
| 3251 Py_XDECREF(errorHandler); | |
| 3252 Py_XDECREF(exc); | |
| 3253 return (PyObject *)v; | |
| 3254 | |
| 3255 onError: | |
| 3256 Py_XDECREF(v); | |
| 3257 Py_XDECREF(errorHandler); | |
| 3258 Py_XDECREF(exc); | |
| 3259 return NULL; | |
| 3260 } | |
| 3261 | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3262 /* --- Latin-1 Codec ------------------------------------------------------ */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3263 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3264 PyObject *PyUnicode_DecodeLatin1(const char *s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3265 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
|
3266 const char *errors) |
|
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 PyUnicodeObject *v; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3269 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
|
3270 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3271 /* 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
|
3272 if (size == 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
|
3273 Py_UNICODE r = *(unsigned char*)s; |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
3274 return PyUnicode_FromUnicode(&r, 1); |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
3275 } |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
3276 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3277 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
|
3278 if (v == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3279 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3280 if (size == 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3281 return (PyObject *)v; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3282 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
|
3283 while (size-- > 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3284 *p++ = (unsigned char)*s++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3285 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
|
3286 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3287 onError: |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3288 Py_XDECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3289 return NULL; |
|
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 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3292 /* 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
|
3293 static void make_encode_exception(PyObject **exceptionObject, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3294 const char *encoding, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3295 const Py_UNICODE *unicode, Py_ssize_t size, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3296 Py_ssize_t startpos, Py_ssize_t endpos, |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3297 const char *reason) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3298 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3299 if (*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
|
3300 *exceptionObject = PyUnicodeEncodeError_Create( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3301 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
|
3302 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3303 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
|
3304 if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3305 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
|
3306 if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3307 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
|
3308 if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3309 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
|
3310 return; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3311 onError: |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3312 Py_DECREF(*exceptionObject); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3313 *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
|
3314 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3315 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3316 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3317 /* 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
|
3318 static void raise_encode_exception(PyObject **exceptionObject, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3319 const char *encoding, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3320 const Py_UNICODE *unicode, Py_ssize_t size, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3321 Py_ssize_t startpos, Py_ssize_t endpos, |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3322 const char *reason) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3323 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3324 make_encode_exception(exceptionObject, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3325 encoding, unicode, size, startpos, endpos, reason); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3326 if (*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
|
3327 PyCodec_StrictErrors(*exceptionObject); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3328 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3329 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3330 /* 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
|
3331 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
|
3332 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
|
3333 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
|
3334 static PyObject *unicode_encode_call_errorhandler(const char *errors, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3335 PyObject **errorHandler, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3336 const char *encoding, const char *reason, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3337 const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3338 Py_ssize_t startpos, Py_ssize_t endpos, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3339 Py_ssize_t *newpos) |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3340 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3341 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
|
3342 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3343 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
|
3344 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
|
3345 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3346 if (*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
|
3347 *errorHandler = PyCodec_LookupError(errors); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3348 if (*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
|
3349 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3350 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3351 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3352 make_encode_exception(exceptionObject, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3353 encoding, unicode, size, startpos, endpos, reason); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3354 if (*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
|
3355 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3356 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3357 restuple = PyObject_CallFunctionObjArgs( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3358 *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
|
3359 if (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
|
3360 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3361 if (!PyTuple_Check(restuple)) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3362 PyErr_Format(PyExc_TypeError, &argparse[4]); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3363 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
|
3364 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3365 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3366 if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3367 &resunicode, newpos)) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3368 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
|
3369 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3370 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3371 if (*newpos<0) |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
3372 *newpos = size+*newpos; |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
3373 if (*newpos<0 || *newpos>size) { |
|
36510
0f3894bb2ada
Support %zd in PyErr_Format and PyString_FromFormat.
Martin v. Löwis <martin@v.loewis.de>
parents:
36508
diff
changeset
|
3374 PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
3375 Py_DECREF(restuple); |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
3376 return NULL; |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
3377 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3378 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
|
3379 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
|
3380 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
|
3381 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3382 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3383 static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3384 Py_ssize_t size, |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3385 const char *errors, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3386 int limit) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3387 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3388 /* 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
|
3389 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
|
3390 /* 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
|
3391 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
|
3392 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
|
3393 /* 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
|
3394 /* 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
|
3395 /* 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
|
3396 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
|
3397 /* current output position */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3398 Py_ssize_t respos = 0; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3399 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
|
3400 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
|
3401 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
|
3402 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
|
3403 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
|
3404 /* 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
|
3405 * -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
|
3406 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
|
3407 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3408 /* 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
|
3409 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
|
3410 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
|
3411 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
|
3412 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
|
3413 if (size == 0) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3414 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
|
3415 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
|
3416 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
|
3417 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3418 while (p<endp) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3419 Py_UNICODE c = *p; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3420 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3421 /* can we encode this? */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3422 if (c<limit) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3423 /* no overflow check, because we know that the space is enough */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3424 *str++ = (char)c; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3425 ++p; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3426 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3427 else { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3428 Py_ssize_t unicodepos = p-startp; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3429 Py_ssize_t requiredsize; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3430 PyObject *repunicode; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3431 Py_ssize_t repsize; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3432 Py_ssize_t newpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3433 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
|
3434 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
|
3435 /* startpos for collecting unencodable chars */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3436 const Py_UNICODE *collstart = p; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3437 const Py_UNICODE *collend = p; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3438 /* find all unecodable characters */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3439 while ((collend < endp) && ((*collend)>=limit)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3440 ++collend; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3441 /* cache callback name lookup (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
|
3442 if (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
|
3443 if ((errors==NULL) || (!strcmp(errors, "strict"))) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3444 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
|
3445 else if (!strcmp(errors, "replace")) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3446 known_errorHandler = 2; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3447 else if (!strcmp(errors, "ignore")) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3448 known_errorHandler = 3; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3449 else if (!strcmp(errors, "xmlcharrefreplace")) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3450 known_errorHandler = 4; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3451 else |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3452 known_errorHandler = 0; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3453 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3454 switch (known_errorHandler) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3455 case 1: /* strict */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3456 raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3457 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
|
3458 case 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
|
3459 while (collstart++<collend) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3460 *str++ = '?'; /* fall through */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3461 case 3: /* ignore */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3462 p = collend; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3463 break; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3464 case 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
|
3465 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
|
3466 /* determine replacement size (temporarily (mis)uses p) */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3467 for (p = collstart, repsize = 0; p < collend; ++p) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3468 if (*p<10) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3469 repsize += 2+1+1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3470 else if (*p<100) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3471 repsize += 2+2+1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3472 else if (*p<1000) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3473 repsize += 2+3+1; |
|
31248
353a16033f6a
Fix broken xmlcharrefreplace by rev 2.204.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31238
diff
changeset
|
3474 else if (*p<10000) |
|
353a16033f6a
Fix broken xmlcharrefreplace by rev 2.204.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31238
diff
changeset
|
3475 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
|
3476 #ifndef Py_UNICODE_WIDE |
|
4edf200a8118
SF #859573: Reduce compiler warnings on gcc 3.2 and above.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31199
diff
changeset
|
3477 else |
|
31248
353a16033f6a
Fix broken xmlcharrefreplace by rev 2.204.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31238
diff
changeset
|
3478 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
|
3479 #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
|
3480 else if (*p<100000) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3481 repsize += 2+5+1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3482 else if (*p<1000000) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3483 repsize += 2+6+1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3484 else |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3485 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
|
3486 #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
|
3487 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3488 requiredsize = respos+repsize+(endp-collend); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3489 if (requiredsize > ressize) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3490 if (requiredsize<2*ressize) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3491 requiredsize = 2*ressize; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3492 if (_PyString_Resize(&res, requiredsize)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3493 goto onError; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3494 str = PyString_AS_STRING(res) + respos; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3495 ressize = requiredsize; |
|
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 /* generate replacement (temporarily (mis)uses p) */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3498 for (p = collstart; p < collend; ++p) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3499 str += sprintf(str, "&#%d;", (int)*p); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3500 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3501 p = collend; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3502 break; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3503 default: |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3504 repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3505 encoding, reason, startp, size, &exc, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3506 collstart-startp, collend-startp, &newpos); |
|
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 (repunicode == NULL) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3508 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
|
3509 /* 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
|
3510 have+the replacement+the rest of the string, so |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3511 we won't have to check space for encodable characters) */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3512 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
|
3513 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
|
3514 requiredsize = respos+repsize+(endp-collend); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3515 if (requiredsize > ressize) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3516 if (requiredsize<2*ressize) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3517 requiredsize = 2*ressize; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3518 if (_PyString_Resize(&res, requiredsize)) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3519 Py_DECREF(repunicode); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3520 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
|
3521 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3522 str = PyString_AS_STRING(res) + respos; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3523 ressize = requiredsize; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3524 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3525 /* check if there is anything unencodable in the replacement |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3526 and copy it to 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
|
3527 for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3528 c = *uni2; |
|
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 (c >= limit) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3530 raise_encode_exception(&exc, encoding, startp, size, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3531 unicodepos, unicodepos+1, reason); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3532 Py_DECREF(repunicode); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3533 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
|
3534 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3535 *str = (char)c; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3536 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3537 p = startp + newpos; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3538 Py_DECREF(repunicode); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3539 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3540 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3541 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3542 /* 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
|
3543 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
|
3544 if (respos<ressize) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3545 /* If this falls res will be NULL */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3546 _PyString_Resize(&res, respos); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3547 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
|
3548 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
|
3549 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
|
3550 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3551 onError: |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3552 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
|
3553 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
|
3554 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
|
3555 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3556 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3557 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3558 PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3559 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
|
3560 const char *errors) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3561 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3562 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
|
3563 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3564 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3565 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
|
3566 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3567 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
|
3568 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3569 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3570 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3571 return PyUnicode_EncodeLatin1(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
|
3572 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
|
3573 NULL); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3574 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3575 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3576 /* --- 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
|
3577 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3578 PyObject *PyUnicode_DecodeASCII(const char *s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3579 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
|
3580 const char *errors) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3581 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3582 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
|
3583 PyUnicodeObject *v; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3584 Py_UNICODE *p; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3585 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3586 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3587 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
|
3588 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
|
3589 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
|
3590 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
|
3591 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3592 /* 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
|
3593 if (size == 1 && *(unsigned char*)s < 128) { |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
3594 Py_UNICODE r = *(unsigned char*)s; |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
3595 return PyUnicode_FromUnicode(&r, 1); |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
3596 } |
|
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
|
3597 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3598 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
|
3599 if (v == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3600 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3601 if (size == 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3602 return (PyObject *)v; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3603 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
|
3604 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
|
3605 while (s < e) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3606 register unsigned char c = (unsigned char)*s; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3607 if (c < 128) { |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3608 *p++ = 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
|
3609 ++s; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3610 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3611 else { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3612 startinpos = 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
|
3613 endinpos = startinpos + 1; |
|
30439
30e495efe90a
Double-fix of crash in Unicode freelist handling.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
30431
diff
changeset
|
3614 outpos = p - (Py_UNICODE *)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
|
3615 if (unicode_decode_call_errorhandler( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3616 errors, &errorHandler, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3617 "ascii", "ordinal not in range(128)", |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3618 starts, size, &startinpos, &endinpos, &exc, &s, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3619 (PyObject **)&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
|
3620 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
|
3621 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3622 } |
| 12329 | 3623 if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
3624 if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 12329 | 3625 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
|
3626 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
|
3627 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
|
3628 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
|
3629 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3630 onError: |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3631 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
|
3632 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
|
3633 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
|
3634 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3635 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3636 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3637 PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3638 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
|
3639 const char *errors) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3640 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3641 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
|
3642 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3643 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3644 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
|
3645 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3646 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
|
3647 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3648 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3649 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3650 return PyUnicode_EncodeASCII(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
|
3651 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
|
3652 NULL); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3653 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3654 |
|
24071
8142f8d668a0
Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents:
23837
diff
changeset
|
3655 #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 12193 | 3656 |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3657 /* --- MBCS codecs for Windows -------------------------------------------- */ |
| 12193 | 3658 |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3659 #if SIZEOF_INT < SIZEOF_SSIZE_T |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3660 #define NEED_RETRY |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3661 #endif |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3662 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3663 /* 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
|
3664 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
|
3665 b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3666 encodings, see IsDBCSLeadByteEx documentation. */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3667 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3668 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
|
3669 { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3670 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
|
3671 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3672 if (IsDBCSLeadByte(*curr)) { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3673 const char *prev = CharPrev(s, curr); |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3674 return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3675 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3676 return 0; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3677 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3678 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3679 /* |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3680 * 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
|
3681 * 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
|
3682 */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3683 static int decode_mbcs(PyUnicodeObject **v, |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3684 const char *s, /* MBCS string */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3685 int size, /* sizeof MBCS string */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3686 int final) |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3687 { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3688 Py_UNICODE *p; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3689 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
|
3690 int usize = 0; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3691 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3692 assert(size >= 0); |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3693 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3694 /* 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
|
3695 if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3696 --size; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3697 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3698 /* 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
|
3699 if (size > 0) { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3700 usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3701 if (usize == 0) { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3702 PyErr_SetFromWindowsErrWithFilename(0, NULL); |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3703 return -1; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3704 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3705 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3706 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3707 if (*v == NULL) { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3708 /* Create unicode object */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3709 *v = _PyUnicode_New(usize); |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3710 if (*v == NULL) |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3711 return -1; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3712 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3713 else { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3714 /* Extend unicode object */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3715 n = PyUnicode_GET_SIZE(*v); |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3716 if (_PyUnicode_Resize(v, n + usize) < 0) |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3717 return -1; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3718 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3719 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3720 /* Do the conversion */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3721 if (size > 0) { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3722 p = PyUnicode_AS_UNICODE(*v) + n; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3723 if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3724 PyErr_SetFromWindowsErrWithFilename(0, NULL); |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3725 return -1; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3726 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3727 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3728 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3729 return size; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3730 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3731 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3732 PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3733 Py_ssize_t size, |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3734 const char *errors, |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3735 Py_ssize_t *consumed) |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3736 { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3737 PyUnicodeObject *v = NULL; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3738 int done; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3739 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3740 if (consumed) |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3741 *consumed = 0; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3742 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3743 #ifdef NEED_RETRY |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3744 retry: |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3745 if (size > INT_MAX) |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3746 done = decode_mbcs(&v, s, INT_MAX, 0); |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3747 else |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3748 #endif |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3749 done = decode_mbcs(&v, s, (int)size, !consumed); |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3750 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3751 if (done < 0) { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3752 Py_XDECREF(v); |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3753 return NULL; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3754 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3755 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3756 if (consumed) |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3757 *consumed += done; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3758 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3759 #ifdef NEED_RETRY |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3760 if (size > INT_MAX) { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3761 s += done; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3762 size -= done; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3763 goto retry; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3764 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3765 #endif |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3766 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3767 return (PyObject *)v; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3768 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3769 |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3770 PyObject *PyUnicode_DecodeMBCS(const char *s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3771 Py_ssize_t size, |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3772 const char *errors) |
|
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3773 { |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3774 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
|
3775 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3776 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3777 /* |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3778 * 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
|
3779 * 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
|
3780 */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3781 static int encode_mbcs(PyObject **repr, |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3782 const Py_UNICODE *p, /* unicode */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3783 int size) /* size of unicode */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3784 { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3785 int mbcssize = 0; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3786 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
|
3787 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3788 assert(size >= 0); |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3789 |
|
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3790 /* 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
|
3791 if (size > 0) { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3792 mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3793 if (mbcssize == 0) { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3794 PyErr_SetFromWindowsErrWithFilename(0, NULL); |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3795 return -1; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3796 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3797 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3798 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3799 if (*repr == NULL) { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3800 /* Create string object */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3801 *repr = PyString_FromStringAndSize(NULL, mbcssize); |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3802 if (*repr == NULL) |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3803 return -1; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3804 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3805 else { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3806 /* Extend string object */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3807 n = PyString_Size(*repr); |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3808 if (_PyString_Resize(repr, n + mbcssize) < 0) |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3809 return -1; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3810 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3811 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3812 /* Do the conversion */ |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3813 if (size > 0) { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3814 char *s = PyString_AS_STRING(*repr) + n; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3815 if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3816 PyErr_SetFromWindowsErrWithFilename(0, NULL); |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3817 return -1; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3818 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3819 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3820 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3821 return 0; |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3822 } |
|
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3823 |
|
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3824 PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3825 Py_ssize_t size, |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3826 const char *errors) |
|
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3827 { |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3828 PyObject *repr = NULL; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3829 int ret; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3830 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3831 #ifdef NEED_RETRY |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3832 retry: |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3833 if (size > INT_MAX) |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3834 ret = encode_mbcs(&repr, p, INT_MAX); |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3835 else |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3836 #endif |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3837 ret = encode_mbcs(&repr, p, (int)size); |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3838 |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3839 if (ret < 0) { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3840 Py_XDECREF(repr); |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3841 return NULL; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3842 } |
|
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 #ifdef NEED_RETRY |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3845 if (size > INT_MAX) { |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3846 p += INT_MAX; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3847 size -= INT_MAX; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3848 goto retry; |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3849 } |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3850 #endif |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3851 |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3852 return repr; |
|
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3853 } |
| 12193 | 3854 |
|
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
|
3855 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
|
3856 { |
|
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
|
3857 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
|
3858 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
|
3859 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
|
3860 } |
|
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
|
3861 return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(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
|
3862 PyUnicode_GET_SIZE(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
|
3863 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
|
3864 } |
|
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
|
3865 |
|
38817
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3866 #undef NEED_RETRY |
|
549c547700af
Patch #1455898: Incremental mode for "mbcs" codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
38786
diff
changeset
|
3867 |
|
24071
8142f8d668a0
Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents:
23837
diff
changeset
|
3868 #endif /* MS_WINDOWS */ |
|
12067
bcab189ddf86
MBCS codecs. (Win32 only.) By Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
3869 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3870 /* --- Character Mapping Codec -------------------------------------------- */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3871 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3872 PyObject *PyUnicode_DecodeCharmap(const char *s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3873 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
|
3874 PyObject *mapping, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3875 const char *errors) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3876 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
3877 const char *starts = s; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3878 Py_ssize_t startinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3879 Py_ssize_t endinpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3880 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
|
3881 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
|
3882 PyUnicodeObject *v; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3883 Py_UNICODE *p; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3884 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
|
3885 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
|
3886 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
|
3887 Py_UNICODE *mapstring = NULL; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3888 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
|
3889 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3890 /* 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
|
3891 if (mapping == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3892 return PyUnicode_DecodeLatin1(s, size, errors); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3893 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3894 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
|
3895 if (v == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3896 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3897 if (size == 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3898 return (PyObject *)v; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3899 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
|
3900 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
|
3901 if (PyUnicode_CheckExact(mapping)) { |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3902 mapstring = PyUnicode_AS_UNICODE(mapping); |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3903 maplen = PyUnicode_GET_SIZE(mapping); |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3904 while (s < e) { |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3905 unsigned char ch = *s; |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3906 Py_UNICODE x = 0xfffe; /* illegal value */ |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3907 |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3908 if (ch < maplen) |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3909 x = mapstring[ch]; |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3910 |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3911 if (x == 0xfffe) { |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3912 /* undefined mapping */ |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3913 outpos = p-PyUnicode_AS_UNICODE(v); |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3914 startinpos = s-starts; |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3915 endinpos = startinpos+1; |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3916 if (unicode_decode_call_errorhandler( |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3917 errors, &errorHandler, |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3918 "charmap", "character maps to <undefined>", |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3919 starts, size, &startinpos, &endinpos, &exc, &s, |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3920 (PyObject **)&v, &outpos, &p)) { |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3921 goto onError; |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3922 } |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3923 continue; |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3924 } |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3925 *p++ = x; |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3926 ++s; |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3927 } |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3928 } |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3929 else { |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3930 while (s < e) { |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3931 unsigned char ch = *s; |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3932 PyObject *w, *x; |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3933 |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3934 /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3935 w = PyInt_FromLong((long)ch); |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3936 if (w == NULL) |
|
15910
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15749
diff
changeset
|
3937 goto onError; |
|
35702
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3938 x = PyObject_GetItem(mapping, w); |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3939 Py_DECREF(w); |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3940 if (x == NULL) { |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3941 if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3942 /* No mapping found means: mapping is undefined. */ |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3943 PyErr_Clear(); |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3944 x = Py_None; |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3945 Py_INCREF(x); |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3946 } else |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3947 goto onError; |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3948 } |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3949 |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3950 /* Apply mapping */ |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3951 if (PyInt_Check(x)) { |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3952 long value = PyInt_AS_LONG(x); |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3953 if (value < 0 || value > 65535) { |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3954 PyErr_SetString(PyExc_TypeError, |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3955 "character mapping must be in range(65536)"); |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3956 Py_DECREF(x); |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3957 goto onError; |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3958 } |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3959 *p++ = (Py_UNICODE)value; |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3960 } |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3961 else if (x == Py_None) { |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3962 /* undefined mapping */ |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3963 outpos = p-PyUnicode_AS_UNICODE(v); |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3964 startinpos = s-starts; |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3965 endinpos = startinpos+1; |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3966 if (unicode_decode_call_errorhandler( |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3967 errors, &errorHandler, |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3968 "charmap", "character maps to <undefined>", |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3969 starts, size, &startinpos, &endinpos, &exc, &s, |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3970 (PyObject **)&v, &outpos, &p)) { |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3971 Py_DECREF(x); |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3972 goto onError; |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3973 } |
|
35962
73ebdc62bf8f
Fix leaked reference to None.
Walter Dörwald <walter@livinglogic.de>
parents:
35827
diff
changeset
|
3974 Py_DECREF(x); |
|
35702
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3975 continue; |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3976 } |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3977 else if (PyUnicode_Check(x)) { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3978 Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
|
35702
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3979 |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3980 if (targetsize == 1) |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3981 /* 1-1 mapping */ |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3982 *p++ = *PyUnicode_AS_UNICODE(x); |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3983 |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3984 else if (targetsize > 1) { |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3985 /* 1-n mapping */ |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3986 if (targetsize > extrachars) { |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3987 /* resize first */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3988 Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
3989 Py_ssize_t needed = (targetsize - extrachars) + \ |
|
35702
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3990 (targetsize << 2); |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3991 extrachars += needed; |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39734
diff
changeset
|
3992 /* XXX overflow detection missing */ |
|
35702
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3993 if (_PyUnicode_Resize(&v, |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3994 PyUnicode_GET_SIZE(v) + needed) < 0) { |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3995 Py_DECREF(x); |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3996 goto onError; |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3997 } |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3998 p = PyUnicode_AS_UNICODE(v) + oldpos; |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
3999 } |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
4000 Py_UNICODE_COPY(p, |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
4001 PyUnicode_AS_UNICODE(x), |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
4002 targetsize); |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
4003 p += targetsize; |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
4004 extrachars -= targetsize; |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
4005 } |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
4006 /* 1-0 mapping: skip the character */ |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
4007 } |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
4008 else { |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
4009 /* wrong return value */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4010 PyErr_SetString(PyExc_TypeError, |
|
35702
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
4011 "character mapping must return integer, None or unicode"); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4012 Py_DECREF(x); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4013 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4014 } |
|
35702
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
4015 Py_DECREF(x); |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
4016 ++s; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4017 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4018 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4019 if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
4020 if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4021 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
|
4022 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
|
4023 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
|
4024 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
|
4025 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4026 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
|
4027 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
|
4028 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
|
4029 Py_XDECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4030 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4031 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4032 |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4033 /* Charmap encoding: the lookup table */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4034 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4035 struct encoding_map{ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4036 PyObject_HEAD |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4037 unsigned char level1[32]; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4038 int count2, count3; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4039 unsigned char level23[1]; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4040 }; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4041 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4042 static PyObject* |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4043 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
|
4044 { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4045 struct encoding_map *map = (struct encoding_map*)obj; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4046 return PyInt_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4047 128*map->count3); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4048 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4049 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4050 static PyMethodDef encoding_map_methods[] = { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4051 {"size", encoding_map_size, METH_NOARGS, |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4052 PyDoc_STR("Return the size (in bytes) of this object") }, |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4053 { 0 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4054 }; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4055 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4056 static void |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4057 encoding_map_dealloc(PyObject* o) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4058 { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4059 PyObject_FREE(o); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4060 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4061 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4062 static PyTypeObject EncodingMapType = { |
|
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
|
4063 PyVarObject_HEAD_INIT(NULL, 0) |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4064 "EncodingMap", /*tp_name*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4065 sizeof(struct encoding_map), /*tp_basicsize*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4066 0, /*tp_itemsize*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4067 /* methods */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4068 encoding_map_dealloc, /*tp_dealloc*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4069 0, /*tp_print*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4070 0, /*tp_getattr*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4071 0, /*tp_setattr*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4072 0, /*tp_compare*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4073 0, /*tp_repr*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4074 0, /*tp_as_number*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4075 0, /*tp_as_sequence*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4076 0, /*tp_as_mapping*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4077 0, /*tp_hash*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4078 0, /*tp_call*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4079 0, /*tp_str*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4080 0, /*tp_getattro*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4081 0, /*tp_setattro*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4082 0, /*tp_as_buffer*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4083 Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4084 0, /*tp_doc*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4085 0, /*tp_traverse*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4086 0, /*tp_clear*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4087 0, /*tp_richcompare*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4088 0, /*tp_weaklistoffset*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4089 0, /*tp_iter*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4090 0, /*tp_iternext*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4091 encoding_map_methods, /*tp_methods*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4092 0, /*tp_members*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4093 0, /*tp_getset*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4094 0, /*tp_base*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4095 0, /*tp_dict*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4096 0, /*tp_descr_get*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4097 0, /*tp_descr_set*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4098 0, /*tp_dictoffset*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4099 0, /*tp_init*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4100 0, /*tp_alloc*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4101 0, /*tp_new*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4102 0, /*tp_free*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4103 0, /*tp_is_gc*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4104 }; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4105 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4106 PyObject* |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4107 PyUnicode_BuildEncodingMap(PyObject* string) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4108 { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4109 Py_UNICODE *decode; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4110 PyObject *result; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4111 struct encoding_map *mresult; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4112 int i; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4113 int need_dict = 0; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4114 unsigned char level1[32]; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4115 unsigned char level2[512]; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4116 unsigned char *mlevel1, *mlevel2, *mlevel3; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4117 int count2 = 0, count3 = 0; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4118 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4119 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
|
4120 PyErr_BadArgument(); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4121 return NULL; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4122 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4123 decode = PyUnicode_AS_UNICODE(string); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4124 memset(level1, 0xFF, sizeof level1); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4125 memset(level2, 0xFF, sizeof level2); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4126 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4127 /* 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
|
4128 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
|
4129 a mapping dictionary. */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4130 if (decode[0] != 0) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4131 need_dict = 1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4132 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
|
4133 int l1, l2; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4134 if (decode[i] == 0 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4135 #ifdef Py_UNICODE_WIDE |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4136 || decode[i] > 0xFFFF |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4137 #endif |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4138 ) { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4139 need_dict = 1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4140 break; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4141 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4142 if (decode[i] == 0xFFFE) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4143 /* unmapped character */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4144 continue; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4145 l1 = decode[i] >> 11; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4146 l2 = decode[i] >> 7; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4147 if (level1[l1] == 0xFF) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4148 level1[l1] = count2++; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4149 if (level2[l2] == 0xFF) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4150 level2[l2] = count3++; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4151 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4152 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4153 if (count2 >= 0xFF || count3 >= 0xFF) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4154 need_dict = 1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4155 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4156 if (need_dict) { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4157 PyObject *result = PyDict_New(); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4158 PyObject *key, *value; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4159 if (!result) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4160 return NULL; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4161 for (i = 0; i < 256; i++) { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4162 key = value = NULL; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4163 key = PyInt_FromLong(decode[i]); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4164 value = PyInt_FromLong(i); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4165 if (!key || !value) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4166 goto failed1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4167 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
|
4168 goto failed1; |
|
38587
21969b71c36d
Repair refleaks in unicodeobject.
Georg Brandl <georg@python.org>
parents:
38586
diff
changeset
|
4169 Py_DECREF(key); |
|
21969b71c36d
Repair refleaks in unicodeobject.
Georg Brandl <georg@python.org>
parents:
38586
diff
changeset
|
4170 Py_DECREF(value); |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4171 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4172 return result; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4173 failed1: |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4174 Py_XDECREF(key); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4175 Py_XDECREF(value); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4176 Py_DECREF(result); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4177 return NULL; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4178 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4179 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4180 /* Create a three-level trie */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4181 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
|
4182 16*count2 + 128*count3 - 1); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4183 if (!result) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4184 return PyErr_NoMemory(); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4185 PyObject_Init(result, &EncodingMapType); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4186 mresult = (struct encoding_map*)result; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4187 mresult->count2 = count2; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4188 mresult->count3 = count3; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4189 mlevel1 = mresult->level1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4190 mlevel2 = mresult->level23; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4191 mlevel3 = mresult->level23 + 16*count2; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4192 memcpy(mlevel1, level1, 32); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4193 memset(mlevel2, 0xFF, 16*count2); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4194 memset(mlevel3, 0, 128*count3); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4195 count3 = 0; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4196 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
|
4197 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
|
4198 if (decode[i] == 0xFFFE) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4199 /* unmapped character */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4200 continue; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4201 o1 = decode[i]>>11; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4202 o2 = (decode[i]>>7) & 0xF; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4203 i2 = 16*mlevel1[o1] + o2; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4204 if (mlevel2[i2] == 0xFF) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4205 mlevel2[i2] = count3++; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4206 o3 = decode[i] & 0x7F; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4207 i3 = 128*mlevel2[i2] + o3; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4208 mlevel3[i3] = i; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4209 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4210 return result; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4211 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4212 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4213 static int |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4214 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
|
4215 { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4216 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
|
4217 int l1 = c>>11; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4218 int l2 = (c>>7) & 0xF; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4219 int l3 = c & 0x7F; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4220 int i; |
|
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 #ifdef Py_UNICODE_WIDE |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4223 if (c > 0xFFFF) { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4224 return -1; |
|
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 #endif |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4227 if (c == 0) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4228 return 0; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4229 /* level 1*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4230 i = map->level1[l1]; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4231 if (i == 0xFF) { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4232 return -1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4233 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4234 /* level 2*/ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4235 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
|
4236 if (i == 0xFF) { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4237 return -1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4238 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4239 /* level 3 */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4240 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
|
4241 if (i == 0) { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4242 return -1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4243 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4244 return i; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4245 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4246 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4247 /* 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
|
4248 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
|
4249 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
|
4250 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
|
4251 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4252 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
|
4253 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
|
4254 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4255 if (w == NULL) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4256 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4257 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
|
4258 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
|
4259 if (x == NULL) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4260 if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4261 /* No mapping found means: mapping is undefined. */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4262 PyErr_Clear(); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4263 x = Py_None; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4264 Py_INCREF(x); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4265 return x; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4266 } else |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4267 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4268 } |
|
27175
47b134bb51c3
Fix charmapencode_lookup(), so that a None value in the mapping
Walter Dörwald <walter@livinglogic.de>
parents:
27170
diff
changeset
|
4269 else if (x == Py_None) |
|
47b134bb51c3
Fix charmapencode_lookup(), so that a None value in the mapping
Walter Dörwald <walter@livinglogic.de>
parents:
27170
diff
changeset
|
4270 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
|
4271 else if (PyInt_Check(x)) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4272 long value = PyInt_AS_LONG(x); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4273 if (value < 0 || value > 255) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4274 PyErr_SetString(PyExc_TypeError, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4275 "character mapping must be in range(256)"); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4276 Py_DECREF(x); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4277 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4278 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4279 return x; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4280 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4281 else if (PyString_Check(x)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4282 return x; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4283 else { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4284 /* wrong return value */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4285 PyErr_SetString(PyExc_TypeError, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4286 "character mapping must return integer, None or str"); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4287 Py_DECREF(x); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4288 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4289 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4290 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4291 |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4292 static int |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4293 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
|
4294 { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4295 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
|
4296 /* exponentially overallocate to minimize reallocations */ |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4297 if (requiredsize < 2*outsize) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4298 requiredsize = 2*outsize; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4299 if (_PyString_Resize(outobj, requiredsize)) { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4300 return 0; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4301 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4302 return 1; |
|
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 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4305 typedef enum charmapencode_result { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4306 enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4307 }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
|
4308 /* 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
|
4309 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
|
4310 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
|
4311 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
|
4312 (in which case no character was written) or NULL, if a |
| 35827 | 4313 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
|
4314 static |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4315 charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4316 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
|
4317 { |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4318 PyObject *rep; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4319 char *outstart; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4320 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
|
4321 |
|
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
|
4322 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
|
4323 int res = encoding_map_lookup(c, mapping); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4324 Py_ssize_t requiredsize = *outpos+1; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4325 if (res == -1) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4326 return enc_FAILED; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4327 if (outsize<requiredsize) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4328 if (!charmapencode_resize(outobj, outpos, requiredsize)) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4329 return enc_EXCEPTION; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4330 outstart = PyString_AS_STRING(*outobj); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4331 outstart[(*outpos)++] = (char)res; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4332 return enc_SUCCESS; |
|
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 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4335 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
|
4336 if (rep==NULL) |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4337 return enc_EXCEPTION; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4338 else if (rep==Py_None) { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4339 Py_DECREF(rep); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4340 return enc_FAILED; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4341 } 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
|
4342 if (PyInt_Check(rep)) { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4343 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
|
4344 if (outsize<requiredsize) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4345 if (!charmapencode_resize(outobj, outpos, requiredsize)) { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4346 Py_DECREF(rep); |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4347 return enc_EXCEPTION; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4348 } |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4349 outstart = PyString_AS_STRING(*outobj); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4350 outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4351 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4352 else { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4353 const char *repchars = PyString_AS_STRING(rep); |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4354 Py_ssize_t repsize = PyString_GET_SIZE(rep); |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4355 Py_ssize_t requiredsize = *outpos+repsize; |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4356 if (outsize<requiredsize) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4357 if (!charmapencode_resize(outobj, outpos, requiredsize)) { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4358 Py_DECREF(rep); |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4359 return enc_EXCEPTION; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4360 } |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4361 outstart = PyString_AS_STRING(*outobj); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4362 memcpy(outstart + *outpos, repchars, repsize); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4363 *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
|
4364 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4365 } |
|
38587
21969b71c36d
Repair refleaks in unicodeobject.
Georg Brandl <georg@python.org>
parents:
38586
diff
changeset
|
4366 Py_DECREF(rep); |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4367 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
|
4368 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4369 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4370 /* 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
|
4371 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
|
4372 static |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4373 int charmap_encoding_error( |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4374 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
|
4375 PyObject **exceptionObject, |
|
30149
59bad7efff02
Fix refcount leak in PyUnicode_EncodeCharmap(). The bug surfaces
Walter Dörwald <walter@livinglogic.de>
parents:
29706
diff
changeset
|
4376 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
|
4377 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
|
4378 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4379 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
|
4380 Py_ssize_t repsize; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4381 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
|
4382 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
|
4383 /* startpos for collecting unencodable chars */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4384 Py_ssize_t collstartpos = *inpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4385 Py_ssize_t collendpos = *inpos+1; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4386 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
|
4387 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
|
4388 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
|
4389 charmapencode_result x; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4390 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4391 /* 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
|
4392 while (collendpos < size) { |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4393 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
|
4394 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
|
4395 int res = encoding_map_lookup(p[collendpos], mapping); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4396 if (res != -1) |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4397 break; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4398 ++collendpos; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4399 continue; |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4400 } |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4401 |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4402 rep = charmapencode_lookup(p[collendpos], mapping); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4403 if (rep==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
|
4404 return -1; |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4405 else if (rep!=Py_None) { |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4406 Py_DECREF(rep); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4407 break; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4408 } |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4409 Py_DECREF(rep); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4410 ++collendpos; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4411 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4412 /* 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
|
4413 * (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
|
4414 if (*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
|
4415 if ((errors==NULL) || (!strcmp(errors, "strict"))) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4416 *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
|
4417 else if (!strcmp(errors, "replace")) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4418 *known_errorHandler = 2; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4419 else if (!strcmp(errors, "ignore")) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4420 *known_errorHandler = 3; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4421 else if (!strcmp(errors, "xmlcharrefreplace")) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4422 *known_errorHandler = 4; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4423 else |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4424 *known_errorHandler = 0; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4425 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4426 switch (*known_errorHandler) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4427 case 1: /* strict */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4428 raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4429 return -1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4430 case 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
|
4431 for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4432 x = charmapencode_output('?', mapping, res, respos); |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4433 if (x==enc_EXCEPTION) { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4434 return -1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4435 } |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4436 else if (x==enc_FAILED) { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4437 raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4438 return -1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4439 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4440 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4441 /* fall through */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4442 case 3: /* ignore */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4443 *inpos = collendpos; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4444 break; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4445 case 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
|
4446 /* generate replacement (temporarily (mis)uses p) */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4447 for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4448 char buffer[2+29+1+1]; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4449 char *cp; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4450 sprintf(buffer, "&#%d;", (int)p[collpos]); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4451 for (cp = buffer; *cp; ++cp) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4452 x = charmapencode_output(*cp, mapping, res, respos); |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4453 if (x==enc_EXCEPTION) |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4454 return -1; |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4455 else if (x==enc_FAILED) { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4456 raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4457 return -1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4458 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4459 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4460 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4461 *inpos = collendpos; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4462 break; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4463 default: |
|
30149
59bad7efff02
Fix refcount leak in PyUnicode_EncodeCharmap(). The bug surfaces
Walter Dörwald <walter@livinglogic.de>
parents:
29706
diff
changeset
|
4464 repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4465 encoding, reason, p, size, exceptionObject, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4466 collstartpos, collendpos, &newpos); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4467 if (repunicode == NULL) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4468 return -1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4469 /* generate replacement */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4470 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
|
4471 for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4472 x = charmapencode_output(*uni2, mapping, res, respos); |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4473 if (x==enc_EXCEPTION) { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4474 return -1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4475 } |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4476 else if (x==enc_FAILED) { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4477 Py_DECREF(repunicode); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4478 raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4479 return -1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4480 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4481 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4482 *inpos = newpos; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4483 Py_DECREF(repunicode); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4484 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4485 return 0; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4486 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4487 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4488 PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4489 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
|
4490 PyObject *mapping, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4491 const char *errors) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4492 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4493 /* 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
|
4494 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
|
4495 /* current input position */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4496 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
|
4497 /* current output position */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4498 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
|
4499 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
|
4500 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
|
4501 /* 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
|
4502 * -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
|
4503 * 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
|
4504 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
|
4505 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4506 /* 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
|
4507 if (mapping == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4508 return PyUnicode_EncodeLatin1(p, size, errors); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4509 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4510 /* 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
|
4511 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
|
4512 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
|
4513 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
|
4514 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
|
4515 if (size == 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
|
4516 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
|
4517 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4518 while (inpos<size) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4519 /* try to encode it */ |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4520 charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
|
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4521 if (x==enc_EXCEPTION) /* error */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4522 goto onError; |
|
38586
9e0b050ce46f
Patch #1359618: Speed-up charmap encoder.
Martin v. Löwis <martin@v.loewis.de>
parents:
38441
diff
changeset
|
4523 if (x==enc_FAILED) { /* unencodable character */ |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4524 if (charmap_encoding_error(p, size, &inpos, mapping, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4525 &exc, |
|
30149
59bad7efff02
Fix refcount leak in PyUnicode_EncodeCharmap(). The bug surfaces
Walter Dörwald <walter@livinglogic.de>
parents:
29706
diff
changeset
|
4526 &known_errorHandler, &errorHandler, errors, |
|
30162
443533d08783
Fix another refcounting leak in PyUnicode_EncodeCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30160
diff
changeset
|
4527 &res, &respos)) { |
|
15910
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15749
diff
changeset
|
4528 goto onError; |
|
30162
443533d08783
Fix another refcounting leak in PyUnicode_EncodeCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30160
diff
changeset
|
4529 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4530 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4531 else |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4532 /* done with this character => adjust input position */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4533 ++inpos; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4534 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4535 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4536 /* 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
|
4537 if (respos<PyString_GET_SIZE(res)) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4538 if (_PyString_Resize(&res, respos)) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4539 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
|
4540 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4541 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
|
4542 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
|
4543 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
|
4544 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4545 onError: |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4546 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
|
4547 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
|
4548 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
|
4549 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4550 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4551 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4552 PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4553 PyObject *mapping) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4554 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4555 if (!PyUnicode_Check(unicode) || mapping == NULL) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4556 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4557 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4558 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4559 return PyUnicode_EncodeCharmap(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
|
4560 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
|
4561 mapping, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4562 NULL); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4563 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4564 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4565 /* 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
|
4566 static void make_translate_exception(PyObject **exceptionObject, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4567 const Py_UNICODE *unicode, Py_ssize_t size, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4568 Py_ssize_t startpos, Py_ssize_t endpos, |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4569 const char *reason) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4570 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4571 if (*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
|
4572 *exceptionObject = PyUnicodeTranslateError_Create( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4573 unicode, size, startpos, endpos, reason); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4574 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4575 else { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4576 if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4577 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
|
4578 if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4579 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
|
4580 if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4581 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
|
4582 return; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4583 onError: |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4584 Py_DECREF(*exceptionObject); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4585 *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
|
4586 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4587 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4588 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4589 /* 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
|
4590 static void raise_translate_exception(PyObject **exceptionObject, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4591 const Py_UNICODE *unicode, Py_ssize_t size, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4592 Py_ssize_t startpos, Py_ssize_t endpos, |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4593 const char *reason) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4594 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4595 make_translate_exception(exceptionObject, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4596 unicode, size, startpos, endpos, reason); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4597 if (*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
|
4598 PyCodec_StrictErrors(*exceptionObject); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4599 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4600 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4601 /* 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
|
4602 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
|
4603 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
|
4604 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
|
4605 static PyObject *unicode_translate_call_errorhandler(const char *errors, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4606 PyObject **errorHandler, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4607 const char *reason, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4608 const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4609 Py_ssize_t startpos, Py_ssize_t endpos, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4610 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
|
4611 { |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
4612 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
|
4613 |
|
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
4614 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
|
4615 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
|
4616 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
|
4617 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4618 if (*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
|
4619 *errorHandler = PyCodec_LookupError(errors); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4620 if (*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
|
4621 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4622 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4623 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4624 make_translate_exception(exceptionObject, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4625 unicode, size, startpos, endpos, reason); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4626 if (*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
|
4627 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4628 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4629 restuple = PyObject_CallFunctionObjArgs( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4630 *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
|
4631 if (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
|
4632 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4633 if (!PyTuple_Check(restuple)) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4634 PyErr_Format(PyExc_TypeError, &argparse[4]); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4635 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
|
4636 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4637 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4638 if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4639 &resunicode, &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
|
4640 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
|
4641 return NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4642 } |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4643 if (i_newpos<0) |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4644 *newpos = size+i_newpos; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4645 else |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4646 *newpos = i_newpos; |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
4647 if (*newpos<0 || *newpos>size) { |
|
36510
0f3894bb2ada
Support %zd in PyErr_Format and PyString_FromFormat.
Martin v. Löwis <martin@v.loewis.de>
parents:
36508
diff
changeset
|
4648 PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
4649 Py_DECREF(restuple); |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
4650 return NULL; |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
4651 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4652 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
|
4653 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
|
4654 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
|
4655 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4656 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4657 /* 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
|
4658 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
|
4659 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
|
4660 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
|
4661 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
|
4662 { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4663 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
|
4664 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
|
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 if (w == NULL) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4667 return -1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4668 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
|
4669 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
|
4670 if (x == NULL) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4671 if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4672 /* No mapping found means: use 1:1 mapping. */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4673 PyErr_Clear(); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4674 *result = NULL; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4675 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
|
4676 } else |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4677 return -1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4678 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4679 else if (x == Py_None) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4680 *result = x; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4681 return 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4682 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4683 else if (PyInt_Check(x)) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4684 long value = PyInt_AS_LONG(x); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4685 long max = PyUnicode_GetMax(); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4686 if (value < 0 || value > max) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4687 PyErr_Format(PyExc_TypeError, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4688 "character mapping must be in range(0x%lx)", max+1); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4689 Py_DECREF(x); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4690 return -1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4691 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4692 *result = x; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4693 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
|
4694 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4695 else if (PyUnicode_Check(x)) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4696 *result = x; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4697 return 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4698 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4699 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
|
4700 /* wrong return value */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4701 PyErr_SetString(PyExc_TypeError, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4702 "character mapping must return integer, None or unicode"); |
|
30164
7cb4ad5932e6
Fix refcounting leak in charmaptranslate_lookup()
Walter Dörwald <walter@livinglogic.de>
parents:
30162
diff
changeset
|
4703 Py_DECREF(x); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4704 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4705 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4706 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4707 /* ensure that *outobj is at least requiredsize characters long, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4708 if not reallocate 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
|
4709 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
|
4710 static |
|
30765
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4711 int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4712 Py_ssize_t requiredsize) |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4713 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4714 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
|
4715 if (requiredsize > oldsize) { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4716 /* remember old output position */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4717 Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4718 /* exponentially overallocate to minimize reallocations */ |
|
30765
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4719 if (requiredsize < 2 * oldsize) |
|
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4720 requiredsize = 2 * oldsize; |
|
30431
cd0c3f916b34
Change checks of PyUnicode_Resize() return value for clarity.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
30216
diff
changeset
|
4721 if (_PyUnicode_Resize(outobj, requiredsize) < 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
|
4722 return -1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4723 *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4724 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4725 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
|
4726 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4727 /* 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
|
4728 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
|
4729 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
|
4730 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
|
4731 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
|
4732 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
|
4733 static |
|
30765
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4734 int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4735 Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
|
30765
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4736 PyObject **res) |
|
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4737 { |
|
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4738 if (charmaptranslate_lookup(*curinp, mapping, 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
|
4739 return -1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4740 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
|
4741 /* not found => default to 1:1 mapping */ |
|
30765
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4742 *(*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
|
4743 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4744 else if (*res==Py_None) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4745 ; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4746 else if (PyInt_Check(*res)) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4747 /* no overflow check, because we know that the space is enough */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4748 *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4749 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4750 else if (PyUnicode_Check(*res)) { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4751 Py_ssize_t repsize = PyUnicode_GET_SIZE(*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
|
4752 if (repsize==1) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4753 /* no overflow check, because we know that the space is enough */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4754 *(*outp)++ = *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
|
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 if (repsize!=0) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4757 /* more than one character */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4758 Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
|
31455
37d501b9e2d5
Fix reallocation bug in unicode.translate(): The code was comparing
Walter Dörwald <walter@livinglogic.de>
parents:
31325
diff
changeset
|
4759 (insize - (curinp-startinp)) + |
|
30765
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4760 repsize - 1; |
|
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4761 if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4762 return -1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4763 memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4764 *outp += repsize; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4765 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4766 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4767 else |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4768 return -1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4769 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
|
4770 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4771 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4772 PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4773 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
|
4774 PyObject *mapping, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4775 const char *errors) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4776 { |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4777 /* 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
|
4778 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
|
4779 /* 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
|
4780 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
|
4781 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
|
4782 /* 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
|
4783 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
|
4784 /* current output position */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4785 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
|
4786 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
|
4787 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
|
4788 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
|
4789 /* 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
|
4790 * -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
|
4791 * 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
|
4792 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
|
4793 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4794 if (mapping == NULL) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4795 PyErr_BadArgument(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4796 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4797 } |
|
25091
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 /* 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
|
4800 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
|
4801 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
|
4802 if (res == NULL) |
|
30765
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4803 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4804 if (size == 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
|
4805 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
|
4806 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
|
4807 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4808 while (p<endp) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4809 /* try to encode it */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4810 PyObject *x = NULL; |
|
30765
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4811 if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &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
|
4812 Py_XDECREF(x); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4813 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4814 } |
|
27774
bfc524379307
Fix two refcounting bugs
Walter Dörwald <walter@livinglogic.de>
parents:
27555
diff
changeset
|
4815 Py_XDECREF(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
|
4816 if (x!=Py_None) /* it worked => adjust input pointer */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4817 ++p; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4818 else { /* untranslatable character */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4819 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
|
4820 Py_ssize_t repsize; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4821 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
|
4822 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
|
4823 /* startpos for collecting untranslatable chars */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4824 const Py_UNICODE *collstart = p; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4825 const Py_UNICODE *collend = p+1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4826 const Py_UNICODE *coll; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4827 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4828 /* find all untranslatable characters */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4829 while (collend < endp) { |
|
30765
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4830 if (charmaptranslate_lookup(*collend, mapping, &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
|
4831 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
|
4832 Py_XDECREF(x); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4833 if (x!=Py_None) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4834 break; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4835 ++collend; |
|
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 /* 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
|
4838 * (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
|
4839 if (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
|
4840 if ((errors==NULL) || (!strcmp(errors, "strict"))) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4841 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
|
4842 else if (!strcmp(errors, "replace")) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4843 known_errorHandler = 2; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4844 else if (!strcmp(errors, "ignore")) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4845 known_errorHandler = 3; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4846 else if (!strcmp(errors, "xmlcharrefreplace")) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4847 known_errorHandler = 4; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4848 else |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4849 known_errorHandler = 0; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4850 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4851 switch (known_errorHandler) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4852 case 1: /* strict */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4853 raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4854 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
|
4855 case 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
|
4856 /* No need to check for space, this is a 1:1 replacement */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4857 for (coll = collstart; coll<collend; ++coll) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4858 *str++ = '?'; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4859 /* fall through */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4860 case 3: /* ignore */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4861 p = collend; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4862 break; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4863 case 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
|
4864 /* generate replacement (temporarily (mis)uses p) */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4865 for (p = collstart; p < collend; ++p) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4866 char buffer[2+29+1+1]; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4867 char *cp; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4868 sprintf(buffer, "&#%d;", (int)*p); |
|
30765
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4869 if (charmaptranslate_makespace(&res, &str, |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4870 (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4871 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
|
4872 for (cp = buffer; *cp; ++cp) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4873 *str++ = *cp; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4874 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4875 p = collend; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4876 break; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4877 default: |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4878 repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4879 reason, startp, size, &exc, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4880 collstart-startp, collend-startp, &newpos); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4881 if (repunicode == NULL) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4882 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
|
4883 /* generate replacement */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4884 repsize = PyUnicode_GET_SIZE(repunicode); |
|
30765
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30707
diff
changeset
|
4885 if (charmaptranslate_makespace(&res, &str, |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4886 (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4887 Py_DECREF(repunicode); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4888 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
|
4889 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4890 for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4891 *str++ = *uni2; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4892 p = startp + newpos; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4893 Py_DECREF(repunicode); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4894 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4895 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4896 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4897 /* 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
|
4898 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
|
4899 if (respos<PyUnicode_GET_SIZE(res)) { |
|
30431
cd0c3f916b34
Change checks of PyUnicode_Resize() return value for clarity.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
30216
diff
changeset
|
4900 if (_PyUnicode_Resize(&res, respos) < 0) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4901 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
|
4902 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4903 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
|
4904 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
|
4905 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
|
4906 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4907 onError: |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4908 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
|
4909 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
|
4910 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
|
4911 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4912 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4913 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4914 PyObject *PyUnicode_Translate(PyObject *str, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4915 PyObject *mapping, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4916 const char *errors) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4917 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4918 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
|
4919 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4920 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
|
4921 if (str == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4922 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4923 result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4924 PyUnicode_GET_SIZE(str), |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4925 mapping, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4926 errors); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4927 Py_DECREF(str); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4928 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
|
4929 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4930 onError: |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4931 Py_XDECREF(str); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4932 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4933 } |
|
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
|
4934 |
|
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
|
4935 /* --- 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
|
4936 |
|
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
|
4937 int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4938 Py_ssize_t length, |
|
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
|
4939 char *output, |
|
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
|
4940 const char *errors) |
|
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
|
4941 { |
|
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
|
4942 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
|
4943 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
|
4944 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
|
4945 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
|
4946 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
|
4947 /* 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
|
4948 * -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
|
4949 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
|
4950 |
|
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
|
4951 if (output == NULL) { |
|
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
|
4952 PyErr_BadArgument(); |
|
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
|
4953 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
|
4954 } |
|
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
|
4955 |
|
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
|
4956 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
|
4957 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
|
4958 while (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
|
4959 register Py_UNICODE ch = *p; |
|
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
|
4960 int decimal; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4961 PyObject *repunicode; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4962 Py_ssize_t repsize; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
4963 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
|
4964 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
|
4965 Py_UNICODE *collstart; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4966 Py_UNICODE *collend; |
|
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
|
4967 |
|
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
|
4968 if (Py_UNICODE_ISSPACE(ch)) { |
|
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
|
4969 *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
|
4970 ++p; |
|
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
|
4971 continue; |
|
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
|
4972 } |
|
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
|
4973 decimal = Py_UNICODE_TODECIMAL(ch); |
|
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
|
4974 if (decimal >= 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
|
4975 *output++ = '0' + decimal; |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4976 ++p; |
|
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
|
4977 continue; |
|
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
|
4978 } |
|
12275
cbfa95a6a03e
Conrad Huang points out that "if (0 < ch < 256)", while legal C,
Guido van Rossum <guido@python.org>
parents:
12249
diff
changeset
|
4979 if (0 < ch && ch < 256) { |
|
12577
85f00f8cd674
Fix warning detected by VC++ on assignment of Py_UNICODE to char.
Guido van Rossum <guido@python.org>
parents:
12576
diff
changeset
|
4980 *output++ = (char)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
|
4981 ++p; |
|
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
|
4982 continue; |
|
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
|
4983 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4984 /* All other characters are considered unencodable */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4985 collstart = p; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4986 collend = p+1; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4987 while (collend < end) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4988 if ((0 < *collend && *collend < 256) || |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4989 !Py_UNICODE_ISSPACE(*collend) || |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4990 Py_UNICODE_TODECIMAL(*collend)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4991 break; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4992 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4993 /* 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
|
4994 * (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
|
4995 if (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
|
4996 if ((errors==NULL) || (!strcmp(errors, "strict"))) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4997 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
|
4998 else if (!strcmp(errors, "replace")) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
4999 known_errorHandler = 2; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5000 else if (!strcmp(errors, "ignore")) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5001 known_errorHandler = 3; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5002 else if (!strcmp(errors, "xmlcharrefreplace")) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5003 known_errorHandler = 4; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5004 else |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5005 known_errorHandler = 0; |
|
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
|
5006 } |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5007 switch (known_errorHandler) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5008 case 1: /* strict */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5009 raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5010 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
|
5011 case 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
|
5012 for (p = collstart; p < collend; ++p) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5013 *output++ = '?'; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5014 /* fall through */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5015 case 3: /* ignore */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5016 p = collend; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5017 break; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5018 case 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
|
5019 /* generate replacement (temporarily (mis)uses p) */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5020 for (p = collstart; p < collend; ++p) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5021 output += sprintf(output, "&#%d;", (int)*p); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5022 p = collend; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5023 break; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5024 default: |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5025 repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5026 encoding, reason, s, length, &exc, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5027 collstart-s, collend-s, &newpos); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5028 if (repunicode == NULL) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5029 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
|
5030 /* generate replacement */ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5031 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
|
5032 for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5033 Py_UNICODE ch = *uni2; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5034 if (Py_UNICODE_ISSPACE(ch)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5035 *output++ = ' '; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5036 else { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5037 decimal = Py_UNICODE_TODECIMAL(ch); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5038 if (decimal >= 0) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5039 *output++ = '0' + decimal; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5040 else if (0 < ch && ch < 256) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5041 *output++ = (char)ch; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5042 else { |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5043 Py_DECREF(repunicode); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5044 raise_encode_exception(&exc, encoding, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5045 s, length, collstart-s, collend-s, reason); |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5046 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
|
5047 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5048 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5049 } |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5050 p = s + newpos; |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25005
diff
changeset
|
5051 Py_DECREF(repunicode); |
|
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
|
5052 } |
|
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
|
5053 } |
|
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
|
5054 /* 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
|
5055 *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
|
5056 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
|
5057 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
|
5058 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
|
5059 |
|
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
|
5060 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
|
5061 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
|
5062 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
|
5063 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
|
5064 } |
|
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
|
5065 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5066 /* --- Helpers ------------------------------------------------------------ */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5067 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
5068 #include "stringlib/unicodedefs.h" |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
5069 |
|
43888
1fb353776dfb
Made _ParseTupleFinds only defined to unicodeobject.c
Facundo Batista <facundobatista@gmail.com>
parents:
43886
diff
changeset
|
5070 #define FROM_UNICODE |
|
38383
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
5071 |
|
38381
b32f9bb2e077
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38347
diff
changeset
|
5072 #include "stringlib/fastsearch.h" |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5073 |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5074 #include "stringlib/count.h" |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5075 #include "stringlib/find.h" |
|
38383
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
5076 #include "stringlib/partition.h" |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
5077 |
|
38402
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5078 /* helper macro to fixup start/end slice values */ |
|
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5079 #define FIX_START_END(obj) \ |
|
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5080 if (start < 0) \ |
|
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5081 start += (obj)->length; \ |
|
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5082 if (start < 0) \ |
|
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5083 start = 0; \ |
|
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5084 if (end > (obj)->length) \ |
|
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5085 end = (obj)->length; \ |
|
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5086 if (end < 0) \ |
|
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5087 end += (obj)->length; \ |
|
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5088 if (end < 0) \ |
|
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5089 end = 0; |
|
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5090 |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5091 Py_ssize_t PyUnicode_Count(PyObject *str, |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5092 PyObject *substr, |
|
38347
6d8615f9f1cc
use Py_LOCAL also for string and unicode objects
Fredrik Lundh <fredrik@pythonware.com>
parents:
38339
diff
changeset
|
5093 Py_ssize_t start, |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5094 Py_ssize_t end) |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5095 { |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5096 Py_ssize_t result; |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5097 PyUnicodeObject* str_obj; |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5098 PyUnicodeObject* sub_obj; |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5099 |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5100 str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5101 if (!str_obj) |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5102 return -1; |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5103 sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5104 if (!sub_obj) { |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5105 Py_DECREF(str_obj); |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5106 return -1; |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5107 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5108 |
|
38402
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5109 FIX_START_END(str_obj); |
|
15910
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15749
diff
changeset
|
5110 |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5111 result = stringlib_count( |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5112 str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
|
38275
43b1474b512b
needforspeed: use "fastsearch" for count and findstring helpers. this
Fredrik Lundh <fredrik@pythonware.com>
parents:
38274
diff
changeset
|
5113 ); |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
5114 |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5115 Py_DECREF(sub_obj); |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5116 Py_DECREF(str_obj); |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5117 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5118 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5119 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5120 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5121 Py_ssize_t PyUnicode_Find(PyObject *str, |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5122 PyObject *sub, |
|
38401
adc214fc1e1c
needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
38400
diff
changeset
|
5123 Py_ssize_t start, |
|
adc214fc1e1c
needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
38400
diff
changeset
|
5124 Py_ssize_t end, |
|
adc214fc1e1c
needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
38400
diff
changeset
|
5125 int direction) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5126 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5127 Py_ssize_t result; |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5128 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5129 str = PyUnicode_FromObject(str); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5130 if (!str) |
|
23561
c5e8f89fd358
Fix for bug [ 561796 ] string.find causes lazy error
Marc-André Lemburg <mal@egenix.com>
parents:
23542
diff
changeset
|
5131 return -2; |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5132 sub = PyUnicode_FromObject(sub); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5133 if (!sub) { |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5134 Py_DECREF(str); |
|
23561
c5e8f89fd358
Fix for bug [ 561796 ] string.find causes lazy error
Marc-André Lemburg <mal@egenix.com>
parents:
23542
diff
changeset
|
5135 return -2; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5136 } |
|
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
|
5137 |
|
38401
adc214fc1e1c
needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
38400
diff
changeset
|
5138 if (direction > 0) |
|
38441
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
5139 result = stringlib_find_slice( |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
5140 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
|
5141 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
|
5142 start, end |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
5143 ); |
|
38401
adc214fc1e1c
needforspeed: stringlib refactoring: use stringlib/find for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents:
38400
diff
changeset
|
5144 else |
|
38441
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
5145 result = stringlib_rfind_slice( |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
5146 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
|
5147 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
|
5148 start, end |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
5149 ); |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5150 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5151 Py_DECREF(str); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5152 Py_DECREF(sub); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
5153 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5154 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5155 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5156 |
|
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
|
5157 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5158 int tailmatch(PyUnicodeObject *self, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5159 PyUnicodeObject *substring, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5160 Py_ssize_t start, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5161 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
|
5162 int direction) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5163 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5164 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
|
5165 return 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5166 |
|
38402
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
5167 FIX_START_END(self); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5168 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5169 end -= substring->length; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5170 if (end < start) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5171 return 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5172 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5173 if (direction > 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5174 if (Py_UNICODE_MATCH(self, end, substring)) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5175 return 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5176 } else { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5177 if (Py_UNICODE_MATCH(self, start, substring)) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5178 return 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5179 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5180 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5181 return 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5182 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5183 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5184 Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5185 PyObject *substr, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5186 Py_ssize_t start, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5187 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
|
5188 int direction) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5189 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5190 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
|
5191 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5192 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
|
5193 if (str == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5194 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5195 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
|
5196 if (substr == NULL) { |
|
36877
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36675
diff
changeset
|
5197 Py_DECREF(str); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5198 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5199 } |
|
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
|
5200 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5201 result = tailmatch((PyUnicodeObject *)str, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5202 (PyUnicodeObject *)substr, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5203 start, end, direction); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5204 Py_DECREF(str); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5205 Py_DECREF(substr); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5206 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5207 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5208 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5209 /* 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
|
5210 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
|
5211 |
|
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
|
5212 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5213 PyObject *fixup(PyUnicodeObject *self, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5214 int (*fixfct)(PyUnicodeObject *s)) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5215 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5216 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5217 PyUnicodeObject *u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5218 |
|
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
|
5219 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
|
5220 if (u == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5221 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
|
5222 |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
5223 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
|
5224 |
|
19885
72e6eb653312
More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19849
diff
changeset
|
5225 if (!fixfct(u) && 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
|
5226 /* fixfct should return TRUE if it modified the buffer. If |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5227 FALSE, return a reference to the original buffer instead |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5228 (to save space, not time) */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5229 Py_INCREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5230 Py_DECREF(u); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5231 return (PyObject*) self; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5232 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5233 return (PyObject*) u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5234 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5235 |
|
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
|
5236 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5237 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
|
5238 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5239 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
|
5240 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
|
5241 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
|
5242 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5243 while (len-- > 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5244 register Py_UNICODE ch; |
|
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
|
5245 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5246 ch = 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
|
5247 if (ch != *s) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5248 status = 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5249 *s = ch; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5250 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5251 s++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5252 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5253 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5254 return status; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5255 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5256 |
|
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
|
5257 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5258 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
|
5259 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5260 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
|
5261 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
|
5262 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
|
5263 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5264 while (len-- > 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5265 register Py_UNICODE ch; |
|
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
|
5266 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5267 ch = 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
|
5268 if (ch != *s) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5269 status = 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5270 *s = ch; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5271 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5272 s++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5273 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5274 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5275 return status; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5276 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5277 |
|
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
|
5278 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5279 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
|
5280 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5281 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
|
5282 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
|
5283 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
|
5284 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5285 while (len-- > 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5286 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
|
5287 *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
|
5288 status = 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5289 } 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
|
5290 *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
|
5291 status = 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5292 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5293 s++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5294 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5295 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5296 return status; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5297 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5298 |
|
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
|
5299 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5300 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
|
5301 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5302 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
|
5303 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
|
5304 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
|
5305 |
|
16332
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5306 if (len == 0) |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5307 return 0; |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5308 if (Py_UNICODE_ISLOWER(*s)) { |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5309 *s = Py_UNICODE_TOUPPER(*s); |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5310 status = 1; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5311 } |
|
16332
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5312 s++; |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5313 while (--len > 0) { |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5314 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
|
5315 *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
|
5316 status = 1; |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5317 } |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5318 s++; |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5319 } |
|
bacbb45fe844
Fixed .capitalize() method of Unicode objects to work like the
Marc-André Lemburg <mal@egenix.com>
parents:
16226
diff
changeset
|
5320 return status; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5321 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5322 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5323 static |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5324 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
|
5325 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5326 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
|
5327 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
|
5328 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
|
5329 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5330 /* 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
|
5331 if (PyUnicode_GET_SIZE(self) == 1) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5332 Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5333 if (*p != ch) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5334 *p = ch; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5335 return 1; |
|
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 else |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5338 return 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5339 } |
|
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
|
5340 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5341 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
|
5342 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
|
5343 for (; p < e; p++) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5344 register const Py_UNICODE ch = *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
|
5345 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5346 if (previous_is_cased) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5347 *p = Py_UNICODE_TOLOWER(ch); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5348 else |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5349 *p = Py_UNICODE_TOTITLE(ch); |
|
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
|
5350 |
|
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
5351 if (Py_UNICODE_ISLOWER(ch) || |
|
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
5352 Py_UNICODE_ISUPPER(ch) || |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5353 Py_UNICODE_ISTITLE(ch)) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5354 previous_is_cased = 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5355 else |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5356 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
|
5357 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5358 return 1; |
|
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 |
|
33300
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5361 PyObject * |
|
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5362 PyUnicode_Join(PyObject *separator, PyObject *seq) |
|
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5363 { |
|
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5364 PyObject *internal_separator = NULL; |
|
33499
a4d3673bcd66
Initialize sep and seplen to suppress warning from gcc.
Skip Montanaro <skip@pobox.com>
parents:
33495
diff
changeset
|
5365 const Py_UNICODE blank = ' '; |
|
a4d3673bcd66
Initialize sep and seplen to suppress warning from gcc.
Skip Montanaro <skip@pobox.com>
parents:
33495
diff
changeset
|
5366 const Py_UNICODE *sep = ␣ |
|
38240
0373a074004c
PyUnicode_Join(): Recent code changes introduced new
Tim Peters <tim.peters@gmail.com>
parents:
38239
diff
changeset
|
5367 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
|
5368 PyUnicodeObject *res = NULL; /* the result */ |
|
38240
0373a074004c
PyUnicode_Join(): Recent code changes introduced new
Tim Peters <tim.peters@gmail.com>
parents:
38239
diff
changeset
|
5369 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
|
5370 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
|
5371 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
|
5372 PyObject *fseq; /* PySequence_Fast(seq) */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5373 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
|
5374 PyObject *item; |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5375 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
|
5376 |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5377 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
|
5378 if (fseq == NULL) { |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5379 return NULL; |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5380 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5381 |
|
33311
59bd121eeeb7
PyUnicode_Join(): Bozo Alert. While this is chugging along, it may
Tim Peters <tim.peters@gmail.com>
parents:
33310
diff
changeset
|
5382 /* 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
|
5383 * 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
|
5384 * 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
|
5385 * 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
|
5386 * 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
|
5387 * is invariant. |
|
59bd121eeeb7
PyUnicode_Join(): Bozo Alert. While this is chugging along, it may
Tim Peters <tim.peters@gmail.com>
parents:
33310
diff
changeset
|
5388 */ |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5389 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
|
5390 /* 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
|
5391 if (seqlen == 0) { |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5392 res = _PyUnicode_New(0); /* 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
|
5393 goto Done; |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5394 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5395 /* 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
|
5396 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
|
5397 item = PySequence_Fast_GET_ITEM(fseq, 0); |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5398 if (PyUnicode_CheckExact(item)) { |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5399 Py_INCREF(item); |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5400 res = (PyUnicodeObject *)item; |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5401 goto Done; |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5402 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5403 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5404 |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5405 /* 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
|
5406 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
|
5407 /* Set up sep and seplen -- they're needed. */ |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5408 if (separator == NULL) { |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5409 sep = ␣ |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5410 seplen = 1; |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5411 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5412 else { |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5413 internal_separator = PyUnicode_FromObject(separator); |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5414 if (internal_separator == NULL) |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5415 goto onError; |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5416 sep = PyUnicode_AS_UNICODE(internal_separator); |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5417 seplen = PyUnicode_GET_SIZE(internal_separator); |
|
33311
59bd121eeeb7
PyUnicode_Join(): Bozo Alert. While this is chugging along, it may
Tim Peters <tim.peters@gmail.com>
parents:
33310
diff
changeset
|
5418 /* In case PyUnicode_FromObject() mutated seq. */ |
|
59bd121eeeb7
PyUnicode_Join(): Bozo Alert. While this is chugging along, it may
Tim Peters <tim.peters@gmail.com>
parents:
33310
diff
changeset
|
5419 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
|
5420 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5421 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5422 |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5423 /* Get space. */ |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5424 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
|
5425 if (res == NULL) |
|
33300
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5426 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
|
5427 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
|
5428 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
|
5429 |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5430 for (i = 0; i < seqlen; ++i) { |
|
38240
0373a074004c
PyUnicode_Join(): Recent code changes introduced new
Tim Peters <tim.peters@gmail.com>
parents:
38239
diff
changeset
|
5431 Py_ssize_t itemlen; |
|
0373a074004c
PyUnicode_Join(): Recent code changes introduced new
Tim Peters <tim.peters@gmail.com>
parents:
38239
diff
changeset
|
5432 Py_ssize_t 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
|
5433 |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5434 item = PySequence_Fast_GET_ITEM(fseq, i); |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5435 /* Convert item to Unicode. */ |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5436 if (! PyUnicode_Check(item) && ! PyString_Check(item)) { |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5437 PyErr_Format(PyExc_TypeError, |
|
37775
d8b23ba4a274
Use %zd instead of %i as format character (in call to PyErr_Format) for
Thomas Wouters <thomas@python.org>
parents:
37702
diff
changeset
|
5438 "sequence item %zd: expected string or Unicode," |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5439 " %.80s found", |
|
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
|
5440 i, Py_TYPE(item)->tp_name); |
|
17687
9e093bf8f6a9
Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents:
17640
diff
changeset
|
5441 goto onError; |
|
9e093bf8f6a9
Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents:
17640
diff
changeset
|
5442 } |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5443 item = PyUnicode_FromObject(item); |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5444 if (item == NULL) |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5445 goto onError; |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5446 /* We own a reference to item from here on. */ |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5447 |
|
33311
59bd121eeeb7
PyUnicode_Join(): Bozo Alert. While this is chugging along, it may
Tim Peters <tim.peters@gmail.com>
parents:
33310
diff
changeset
|
5448 /* In case PyUnicode_FromObject() mutated seq. */ |
|
59bd121eeeb7
PyUnicode_Join(): Bozo Alert. While this is chugging along, it may
Tim Peters <tim.peters@gmail.com>
parents:
33310
diff
changeset
|
5449 seqlen = PySequence_Fast_GET_SIZE(fseq); |
|
59bd121eeeb7
PyUnicode_Join(): Bozo Alert. While this is chugging along, it may
Tim Peters <tim.peters@gmail.com>
parents:
33310
diff
changeset
|
5450 |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5451 /* Make sure we have enough space for the separator and the item. */ |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5452 itemlen = PyUnicode_GET_SIZE(item); |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5453 new_res_used = res_used + itemlen; |
|
38709
67450db26f81
Apply perky's fix for #1503157: "/".join([u"", u""]) raising OverflowError.
Georg Brandl <georg@python.org>
parents:
38697
diff
changeset
|
5454 if (new_res_used < 0) |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5455 goto Overflow; |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5456 if (i < seqlen - 1) { |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5457 new_res_used += seplen; |
|
38709
67450db26f81
Apply perky's fix for #1503157: "/".join([u"", u""]) raising OverflowError.
Georg Brandl <georg@python.org>
parents:
38697
diff
changeset
|
5458 if (new_res_used < 0) |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5459 goto Overflow; |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5460 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5461 if (new_res_used > res_alloc) { |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5462 /* double allocated size until it's big enough */ |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5463 do { |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5464 res_alloc += res_alloc; |
|
38240
0373a074004c
PyUnicode_Join(): Recent code changes introduced new
Tim Peters <tim.peters@gmail.com>
parents:
38239
diff
changeset
|
5465 if (res_alloc <= 0) |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5466 goto Overflow; |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5467 } while (new_res_used > res_alloc); |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5468 if (_PyUnicode_Resize(&res, res_alloc) < 0) { |
|
20007
56ffee1eeeec
Fix Unicode .join() method to raise a TypeError for sequence
Marc-André Lemburg <mal@egenix.com>
parents:
19999
diff
changeset
|
5469 Py_DECREF(item); |
|
56ffee1eeeec
Fix Unicode .join() method to raise a TypeError for sequence
Marc-André Lemburg <mal@egenix.com>
parents:
19999
diff
changeset
|
5470 goto onError; |
|
56ffee1eeeec
Fix Unicode .join() method to raise a TypeError for sequence
Marc-André Lemburg <mal@egenix.com>
parents:
19999
diff
changeset
|
5471 } |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5472 res_p = PyUnicode_AS_UNICODE(res) + res_used; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5473 } |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5474 |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5475 /* Copy item, and maybe the separator. */ |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5476 Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5477 res_p += itemlen; |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5478 if (i < seqlen - 1) { |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5479 Py_UNICODE_COPY(res_p, sep, seplen); |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5480 res_p += seplen; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5481 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5482 Py_DECREF(item); |
|
33310
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5483 res_used = new_res_used; |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5484 } |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5485 |
|
8579859f198c
PyUnicode_Join(): Rewrote to use PySequence_Fast(). This doesn't do
Tim Peters <tim.peters@gmail.com>
parents:
33304
diff
changeset
|
5486 /* 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
|
5487 * 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
|
5488 */ |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5489 if (_PyUnicode_Resize(&res, res_used) < 0) |
|
33300
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5490 goto onError; |
|
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5491 |
|
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5492 Done: |
|
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5493 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
|
5494 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
|
5495 return (PyObject *)res; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5496 |
|
33300
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5497 Overflow: |
|
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5498 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
|
5499 "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
|
5500 Py_DECREF(item); |
|
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5501 /* fall through */ |
|
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5502 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5503 onError: |
|
33300
77de38534d26
PyUnicode_Join(): Two primary aims:
Tim Peters <tim.peters@gmail.com>
parents:
32951
diff
changeset
|
5504 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
|
5505 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
|
5506 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
|
5507 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5508 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5509 |
|
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
|
5510 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
|
5511 PyUnicodeObject *pad(PyUnicodeObject *self, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5512 Py_ssize_t left, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5513 Py_ssize_t right, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5514 Py_UNICODE fill) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5515 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5516 PyUnicodeObject *u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5517 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5518 if (left < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5519 left = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5520 if (right < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5521 right = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5522 |
|
19885
72e6eb653312
More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19849
diff
changeset
|
5523 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
|
5524 Py_INCREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5525 return self; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5526 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5527 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5528 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
|
5529 if (u) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5530 if (left) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5531 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
|
5532 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
|
5533 if (right) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5534 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
|
5535 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5536 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5537 return u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5538 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5539 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5540 #define SPLIT_APPEND(data, left, right) \ |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5541 str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5542 if (!str) \ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5543 goto onError; \ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5544 if (PyList_Append(list, str)) { \ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5545 Py_DECREF(str); \ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5546 goto onError; \ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5547 } \ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5548 else \ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5549 Py_DECREF(str); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5550 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5551 static |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5552 PyObject *split_whitespace(PyUnicodeObject *self, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5553 PyObject *list, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5554 Py_ssize_t maxcount) |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5555 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5556 register Py_ssize_t i; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5557 register Py_ssize_t j; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5558 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
|
5559 PyObject *str; |
|
45053
26278796d4da
Patch #1970 by Antoine Pitrou: Speedup unicode whitespace and linebreak detection. The speedup is about 25% for split() (571 / 457 usec) and 35% (175 / 127 usec )for splitlines()
Christian Heimes <christian@cheimes.de>
parents:
44945
diff
changeset
|
5560 register const Py_UNICODE *buf = self->str; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5561 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5562 for (i = j = 0; i < len; ) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5563 /* find a token */ |
|
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
|
5564 while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5565 i++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5566 j = i; |
|
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
|
5567 while (i < len && !Py_UNICODE_ISSPACE(buf[i])) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5568 i++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5569 if (j < i) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5570 if (maxcount-- <= 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5571 break; |
|
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
|
5572 SPLIT_APPEND(buf, j, i); |
|
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
|
5573 while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5574 i++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5575 j = i; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5576 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5577 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5578 if (j < len) { |
|
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
|
5579 SPLIT_APPEND(buf, j, len); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5580 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5581 return list; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5582 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5583 onError: |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5584 Py_DECREF(list); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5585 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5586 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5587 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5588 PyObject *PyUnicode_Splitlines(PyObject *string, |
| 12369 | 5589 int keepends) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5590 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5591 register Py_ssize_t i; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5592 register Py_ssize_t j; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5593 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
|
5594 PyObject *list; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5595 PyObject *str; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5596 Py_UNICODE *data; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5597 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5598 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
|
5599 if (string == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5600 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5601 data = PyUnicode_AS_UNICODE(string); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5602 len = PyUnicode_GET_SIZE(string); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5603 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5604 list = PyList_New(0); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5605 if (!list) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5606 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5607 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5608 for (i = j = 0; i < len; ) { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5609 Py_ssize_t eol; |
|
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
|
5610 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5611 /* Find a line and append it */ |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
5612 while (i < len && !BLOOM_LINEBREAK(data[i])) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5613 i++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5614 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5615 /* Skip the line break reading CRLF as one line break */ |
| 12369 | 5616 eol = i; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5617 if (i < len) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5618 if (data[i] == '\r' && i + 1 < len && |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5619 data[i+1] == '\n') |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5620 i += 2; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5621 else |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5622 i++; |
| 12369 | 5623 if (keepends) |
| 5624 eol = i; | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5625 } |
| 12369 | 5626 SPLIT_APPEND(data, j, eol); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5627 j = i; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5628 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5629 if (j < len) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5630 SPLIT_APPEND(data, j, len); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5631 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5632 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5633 Py_DECREF(string); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5634 return list; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5635 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5636 onError: |
|
36877
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36675
diff
changeset
|
5637 Py_XDECREF(list); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5638 Py_DECREF(string); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5639 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5640 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5641 |
|
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
|
5642 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5643 PyObject *split_char(PyUnicodeObject *self, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5644 PyObject *list, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5645 Py_UNICODE ch, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5646 Py_ssize_t maxcount) |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5647 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5648 register Py_ssize_t i; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5649 register Py_ssize_t j; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5650 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
|
5651 PyObject *str; |
|
45053
26278796d4da
Patch #1970 by Antoine Pitrou: Speedup unicode whitespace and linebreak detection. The speedup is about 25% for split() (571 / 457 usec) and 35% (175 / 127 usec )for splitlines()
Christian Heimes <christian@cheimes.de>
parents:
44945
diff
changeset
|
5652 register const Py_UNICODE *buf = self->str; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5653 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5654 for (i = j = 0; i < len; ) { |
|
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
|
5655 if (buf[i] == ch) { |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5656 if (maxcount-- <= 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5657 break; |
|
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
|
5658 SPLIT_APPEND(buf, j, i); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5659 i = j = i + 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5660 } else |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5661 i++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5662 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5663 if (j <= len) { |
|
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
|
5664 SPLIT_APPEND(buf, j, len); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5665 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5666 return list; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5667 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5668 onError: |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5669 Py_DECREF(list); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5670 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5671 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5672 |
|
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
|
5673 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5674 PyObject *split_substring(PyUnicodeObject *self, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5675 PyObject *list, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5676 PyUnicodeObject *substring, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5677 Py_ssize_t maxcount) |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5678 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5679 register Py_ssize_t i; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5680 register Py_ssize_t j; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5681 Py_ssize_t len = self->length; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5682 Py_ssize_t sublen = substring->length; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5683 PyObject *str; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5684 |
|
15609
0906c8e2c961
Fix off-by-one error in split_substring(). Fixes SF bug #122162.
Guido van Rossum <guido@python.org>
parents:
15579
diff
changeset
|
5685 for (i = j = 0; i <= len - sublen; ) { |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5686 if (Py_UNICODE_MATCH(self, i, substring)) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5687 if (maxcount-- <= 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5688 break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5689 SPLIT_APPEND(self->str, j, i); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5690 i = j = i + sublen; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5691 } else |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5692 i++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5693 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5694 if (j <= len) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5695 SPLIT_APPEND(self->str, j, len); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5696 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5697 return list; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5698 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5699 onError: |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5700 Py_DECREF(list); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5701 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5702 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5703 |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5704 static |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5705 PyObject *rsplit_whitespace(PyUnicodeObject *self, |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5706 PyObject *list, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5707 Py_ssize_t maxcount) |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5708 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5709 register Py_ssize_t i; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5710 register Py_ssize_t j; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5711 Py_ssize_t len = self->length; |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5712 PyObject *str; |
|
45053
26278796d4da
Patch #1970 by Antoine Pitrou: Speedup unicode whitespace and linebreak detection. The speedup is about 25% for split() (571 / 457 usec) and 35% (175 / 127 usec )for splitlines()
Christian Heimes <christian@cheimes.de>
parents:
44945
diff
changeset
|
5713 register const Py_UNICODE *buf = self->str; |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5714 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5715 for (i = j = len - 1; i >= 0; ) { |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5716 /* find a token */ |
|
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
|
5717 while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5718 i--; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5719 j = i; |
|
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
|
5720 while (i >= 0 && !Py_UNICODE_ISSPACE(buf[i])) |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5721 i--; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5722 if (j > i) { |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5723 if (maxcount-- <= 0) |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5724 break; |
|
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
|
5725 SPLIT_APPEND(buf, i + 1, j + 1); |
|
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
|
5726 while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5727 i--; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5728 j = i; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5729 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5730 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5731 if (j >= 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
|
5732 SPLIT_APPEND(buf, 0, j + 1); |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
5733 } |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
5734 if (PyList_Reverse(list) < 0) |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
5735 goto onError; |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5736 return list; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5737 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5738 onError: |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5739 Py_DECREF(list); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5740 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5741 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5742 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5743 static |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5744 PyObject *rsplit_char(PyUnicodeObject *self, |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5745 PyObject *list, |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5746 Py_UNICODE ch, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5747 Py_ssize_t maxcount) |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5748 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5749 register Py_ssize_t i; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5750 register Py_ssize_t j; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5751 Py_ssize_t len = self->length; |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5752 PyObject *str; |
|
45053
26278796d4da
Patch #1970 by Antoine Pitrou: Speedup unicode whitespace and linebreak detection. The speedup is about 25% for split() (571 / 457 usec) and 35% (175 / 127 usec )for splitlines()
Christian Heimes <christian@cheimes.de>
parents:
44945
diff
changeset
|
5753 register const Py_UNICODE *buf = self->str; |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5754 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5755 for (i = j = len - 1; i >= 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
|
5756 if (buf[i] == ch) { |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5757 if (maxcount-- <= 0) |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5758 break; |
|
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
|
5759 SPLIT_APPEND(buf, i + 1, j + 1); |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5760 j = i = i - 1; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5761 } else |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5762 i--; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5763 } |
|
31252
892116a886e0
Fix unicode.rsplit()'s bug that ignores separater on the end of string when
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31248
diff
changeset
|
5764 if (j >= -1) { |
|
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
|
5765 SPLIT_APPEND(buf, 0, j + 1); |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
5766 } |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
5767 if (PyList_Reverse(list) < 0) |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
5768 goto onError; |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5769 return list; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5770 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5771 onError: |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5772 Py_DECREF(list); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5773 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5774 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5775 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5776 static |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5777 PyObject *rsplit_substring(PyUnicodeObject *self, |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5778 PyObject *list, |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5779 PyUnicodeObject *substring, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5780 Py_ssize_t maxcount) |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5781 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5782 register Py_ssize_t i; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5783 register Py_ssize_t j; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5784 Py_ssize_t len = self->length; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5785 Py_ssize_t sublen = substring->length; |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5786 PyObject *str; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5787 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5788 for (i = len - sublen, j = len; i >= 0; ) { |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5789 if (Py_UNICODE_MATCH(self, i, substring)) { |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5790 if (maxcount-- <= 0) |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5791 break; |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
5792 SPLIT_APPEND(self->str, i + sublen, j); |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5793 j = i; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5794 i -= sublen; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5795 } else |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5796 i--; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5797 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5798 if (j >= 0) { |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
5799 SPLIT_APPEND(self->str, 0, j); |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
5800 } |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
5801 if (PyList_Reverse(list) < 0) |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
5802 goto onError; |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5803 return list; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5804 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5805 onError: |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5806 Py_DECREF(list); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5807 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5808 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5809 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5810 #undef SPLIT_APPEND |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5811 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5812 static |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5813 PyObject *split(PyUnicodeObject *self, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5814 PyUnicodeObject *substring, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5815 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
|
5816 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5817 PyObject *list; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5818 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5819 if (maxcount < 0) |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5820 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
|
5821 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5822 list = PyList_New(0); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5823 if (!list) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5824 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5825 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5826 if (substring == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5827 return split_whitespace(self,list,maxcount); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5828 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5829 else if (substring->length == 1) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5830 return split_char(self,list,substring->str[0],maxcount); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5831 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5832 else 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
|
5833 Py_DECREF(list); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5834 PyErr_SetString(PyExc_ValueError, "empty separator"); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5835 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5836 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5837 else |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5838 return split_substring(self,list,substring,maxcount); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5839 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5840 |
|
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
|
5841 static |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5842 PyObject *rsplit(PyUnicodeObject *self, |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5843 PyUnicodeObject *substring, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5844 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
|
5845 { |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5846 PyObject *list; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5847 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5848 if (maxcount < 0) |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5849 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
|
5850 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5851 list = PyList_New(0); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5852 if (!list) |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5853 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5854 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5855 if (substring == NULL) |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5856 return rsplit_whitespace(self,list,maxcount); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5857 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5858 else if (substring->length == 1) |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5859 return rsplit_char(self,list,substring->str[0],maxcount); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5860 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5861 else if (substring->length == 0) { |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5862 Py_DECREF(list); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5863 PyErr_SetString(PyExc_ValueError, "empty separator"); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5864 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5865 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5866 else |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5867 return rsplit_substring(self,list,substring,maxcount); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5868 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5869 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
5870 static |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5871 PyObject *replace(PyUnicodeObject *self, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5872 PyUnicodeObject *str1, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5873 PyUnicodeObject *str2, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
5874 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
|
5875 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5876 PyUnicodeObject *u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5877 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5878 if (maxcount < 0) |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5879 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
|
5880 |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5881 if (str1->length == str2->length) { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5882 /* same length */ |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
5883 Py_ssize_t i; |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5884 if (str1->length == 1) { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5885 /* replace characters */ |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5886 Py_UNICODE u1, u2; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5887 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
|
5888 goto nothing; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5889 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
|
5890 if (!u) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5891 return NULL; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5892 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
|
5893 u1 = str1->str[0]; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5894 u2 = str2->str[0]; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5895 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
|
5896 if (u->str[i] == u1) { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5897 if (--maxcount < 0) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5898 break; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5899 u->str[i] = u2; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5900 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5901 } else { |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5902 i = fastsearch( |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5903 self->str, self->length, str1->str, str1->length, FAST_SEARCH |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5904 ); |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5905 if (i < 0) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5906 goto nothing; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5907 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
|
5908 if (!u) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5909 return NULL; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5910 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
|
5911 while (i <= self->length - str1->length) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5912 if (Py_UNICODE_MATCH(self, i, str1)) { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5913 if (--maxcount < 0) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5914 break; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5915 Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5916 i += str1->length; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5917 } else |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5918 i++; |
|
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 } else { |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5921 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5922 Py_ssize_t n, i, j, e; |
|
38299
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5923 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
|
5924 Py_UNICODE *p; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5925 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5926 /* replace strings */ |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
5927 n = stringlib_count(self->str, self->length, str1->str, str1->length); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5928 if (n > maxcount) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5929 n = maxcount; |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5930 if (n == 0) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5931 goto nothing; |
|
38299
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5932 /* 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
|
5933 delta = (str2->length - str1->length); |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5934 if (delta == 0) { |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5935 new_size = self->length; |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5936 } else { |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5937 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
|
5938 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
|
5939 PyErr_SetString(PyExc_OverflowError, |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5940 "replace string is too long"); |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5941 return NULL; |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5942 } |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5943 new_size = self->length + product; |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5944 if (new_size < 0) { |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5945 PyErr_SetString(PyExc_OverflowError, |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5946 "replace string is too long"); |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5947 return NULL; |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5948 } |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5949 } |
|
ae6bbf2f2a41
needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38278
diff
changeset
|
5950 u = _PyUnicode_New(new_size); |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5951 if (!u) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5952 return NULL; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5953 i = 0; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5954 p = u->str; |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5955 e = self->length - str1->length; |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5956 if (str1->length > 0) { |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5957 while (n-- > 0) { |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5958 /* look for next match */ |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5959 j = i; |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5960 while (j <= e) { |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5961 if (Py_UNICODE_MATCH(self, j, str1)) |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5962 break; |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5963 j++; |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5964 } |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5965 if (j > i) { |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5966 if (j > e) |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5967 break; |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5968 /* copy unchanged part [i:j] */ |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5969 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
|
5970 p += j - i; |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5971 } |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5972 /* copy substitution string */ |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5973 if (str2->length > 0) { |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5974 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
|
5975 p += str2->length; |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5976 } |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5977 i = j + str1->length; |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5978 } |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5979 if (i < self->length) |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5980 /* copy tail [i:] */ |
|
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5981 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
|
5982 } else { |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38427
diff
changeset
|
5983 /* interleave */ |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5984 while (n > 0) { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5985 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
|
5986 p += str2->length; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5987 if (--n <= 0) |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5988 break; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5989 *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
|
5990 } |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5991 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
|
5992 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5993 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5994 return (PyObject *) u; |
|
38278
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5995 |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5996 nothing: |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5997 /* 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
|
5998 if (PyUnicode_CheckExact(self)) { |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
5999 Py_INCREF(self); |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6000 return (PyObject *) self; |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6001 } |
|
260d5a6ce30f
needforspeed: refactored the replace code slightly; special-case
Fredrik Lundh <fredrik@pythonware.com>
parents:
38276
diff
changeset
|
6002 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
|
6003 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6004 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6005 /* --- Unicode Object Methods --------------------------------------------- */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6006 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6007 PyDoc_STRVAR(title__doc__, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6008 "S.title() -> unicode\n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6009 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6010 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
|
6011 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
|
6012 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6013 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
|
6014 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
|
6015 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6016 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
|
6017 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6018 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6019 PyDoc_STRVAR(capitalize__doc__, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6020 "S.capitalize() -> unicode\n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6021 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6022 Return a capitalized version of S, i.e. make the first character\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6023 have upper case."); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6024 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6025 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
|
6026 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
|
6027 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6028 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
|
6029 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6030 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6031 #if 0 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6032 PyDoc_STRVAR(capwords__doc__, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6033 "S.capwords() -> unicode\n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6034 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6035 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
|
6036 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
|
6037 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6038 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
|
6039 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
|
6040 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6041 PyObject *list; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6042 PyObject *item; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6043 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
|
6044 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6045 /* Split into words */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6046 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
|
6047 if (!list) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6048 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6049 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6050 /* Capitalize each word */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6051 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
|
6052 item = fixup((PyUnicodeObject *)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
|
6053 fixcapitalize); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6054 if (item == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6055 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6056 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
|
6057 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
|
6058 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6059 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6060 /* 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
|
6061 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
|
6062 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6063 onError: |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6064 Py_DECREF(list); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6065 return (PyObject *)item; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6066 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6067 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6068 |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6069 /* 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
|
6070 |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6071 static int |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6072 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
|
6073 { |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6074 Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6075 PyObject *uniobj; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6076 Py_UNICODE *unistr; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6077 |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6078 uniobj = PyUnicode_FromObject(obj); |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6079 if (uniobj == NULL) { |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6080 PyErr_SetString(PyExc_TypeError, |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6081 "The fill character cannot be converted to Unicode"); |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6082 return 0; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6083 } |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6084 if (PyUnicode_GET_SIZE(uniobj) != 1) { |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6085 PyErr_SetString(PyExc_TypeError, |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6086 "The fill character must be exactly one character long"); |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6087 Py_DECREF(uniobj); |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6088 return 0; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6089 } |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6090 unistr = PyUnicode_AS_UNICODE(uniobj); |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6091 *fillcharloc = unistr[0]; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6092 Py_DECREF(uniobj); |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6093 return 1; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6094 } |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6095 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6096 PyDoc_STRVAR(center__doc__, |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6097 "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
|
6098 \n\ |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6099 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
|
6100 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
|
6101 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6102 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6103 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
|
6104 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6105 Py_ssize_t marg, left; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6106 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
|
6107 Py_UNICODE fillchar = ' '; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6108 |
|
36532
0c096be27000
Use correct PyArg_Parse format char for Py_ssize_t in unicode.center().
Thomas Wouters <thomas@python.org>
parents:
36519
diff
changeset
|
6109 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
|
6110 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6111 |
|
19885
72e6eb653312
More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19849
diff
changeset
|
6112 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
|
6113 Py_INCREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6114 return (PyObject*) self; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6115 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6116 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6117 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
|
6118 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
|
6119 |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6120 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
|
6121 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6122 |
|
13890
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6123 #if 0 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6124 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6125 /* 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
|
6126 module. The basic comparison should compare ordinals on a naive |
|
13946
7a99450da2d6
Clean up warning from Monterey compiler.
Trent Mick <trentm@activestate.com>
parents:
13890
diff
changeset
|
6127 basis (this is what Java does and thus JPython too). */ |
|
13890
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6128 |
| 13337 | 6129 /* speedy UTF-16 code point order comparison */ |
| 6130 /* gleaned from: */ | |
| 6131 /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ | |
| 6132 | |
|
13411
9a8391054840
New surrogate support in the UTF-8 codec. By Bill Tutt.
Marc-André Lemburg <mal@egenix.com>
parents:
13401
diff
changeset
|
6133 static short utf16Fixup[32] = |
| 13337 | 6134 { |
|
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
|
6135 0, 0, 0, 0, 0, 0, 0, 0, |
| 13337 | 6136 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
|
6137 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
|
6138 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
| 13337 | 6139 }; |
| 6140 | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6141 static int |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6142 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
|
6143 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6144 Py_ssize_t len1, len2; |
| 13337 | 6145 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6146 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
|
6147 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
|
6148 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6149 len1 = str1->length; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6150 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
|
6151 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6152 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
|
6153 Py_UNICODE c1, c2; |
| 13337 | 6154 |
| 6155 c1 = *s1++; | |
| 6156 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
|
6157 |
| 13337 | 6158 if (c1 > (1<<11) * 26) |
| 6159 c1 += utf16Fixup[c1>>11]; | |
| 6160 if (c2 > (1<<11) * 26) | |
| 6161 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
|
6162 /* 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
|
6163 |
|
8b7ff44b4669
experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents:
18278
diff
changeset
|
6164 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
|
6165 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
|
6166 |
| 13337 | 6167 len1--; len2--; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6168 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6169 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6170 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
|
6171 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6172 |
|
13890
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6173 #else |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6174 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6175 static int |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6176 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
|
6177 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6178 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
|
6179 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6180 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
|
6181 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
|
6182 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6183 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
|
6184 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
|
6185 |
|
13890
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6186 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
|
6187 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
|
6188 |
|
8b7ff44b4669
experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents:
18278
diff
changeset
|
6189 c1 = *s1++; |
|
8b7ff44b4669
experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents:
18278
diff
changeset
|
6190 c2 = *s2++; |
|
8b7ff44b4669
experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents:
18278
diff
changeset
|
6191 |
|
8b7ff44b4669
experimental UCS-4 support: made compare a bit more robust, in case
Fredrik Lundh <fredrik@pythonware.com>
parents:
18278
diff
changeset
|
6192 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
|
6193 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
|
6194 |
|
13890
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6195 len1--; len2--; |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6196 } |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6197 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6198 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
|
6199 } |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6200 |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6201 #endif |
|
8f0fc0b5f784
Removing UTF-16 aware Unicode comparison code. This kind of compare
Marc-André Lemburg <mal@egenix.com>
parents:
13837
diff
changeset
|
6202 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6203 int PyUnicode_Compare(PyObject *left, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6204 PyObject *right) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6205 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6206 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
|
6207 int result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6208 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6209 /* 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
|
6210 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
|
6211 if (u == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6212 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6213 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
|
6214 if (v == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6215 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6216 |
|
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
|
6217 /* 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
|
6218 if (v == u) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6219 Py_DECREF(u); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6220 Py_DECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6221 return 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6222 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6223 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6224 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
|
6225 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6226 Py_DECREF(u); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6227 Py_DECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6228 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6229 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6230 onError: |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6231 Py_XDECREF(u); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6232 Py_XDECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6233 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6234 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6235 |
|
39487
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6236 PyObject *PyUnicode_RichCompare(PyObject *left, |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6237 PyObject *right, |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6238 int op) |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6239 { |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6240 int result; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6241 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6242 result = PyUnicode_Compare(left, right); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6243 if (result == -1 && PyErr_Occurred()) |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6244 goto onError; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6245 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6246 /* Convert the return value to a Boolean */ |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6247 switch (op) { |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6248 case Py_EQ: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6249 result = (result == 0); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6250 break; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6251 case Py_NE: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6252 result = (result != 0); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6253 break; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6254 case Py_LE: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6255 result = (result <= 0); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6256 break; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6257 case Py_GE: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6258 result = (result >= 0); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6259 break; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6260 case Py_LT: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6261 result = (result == -1); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6262 break; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6263 case Py_GT: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6264 result = (result == 1); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6265 break; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6266 } |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6267 return PyBool_FromLong(result); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6268 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6269 onError: |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6270 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6271 /* Standard case |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6272 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6273 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
|
6274 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
|
6275 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
|
6276 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
|
6277 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
|
6278 chance. |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6279 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6280 */ |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6281 if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6282 PyErr_Clear(); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6283 Py_INCREF(Py_NotImplemented); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6284 return Py_NotImplemented; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6285 } |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6286 if (op != Py_EQ && op != Py_NE) |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6287 return NULL; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6288 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6289 /* Equality comparison. |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6290 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6291 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
|
6292 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
|
6293 |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6294 */ |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6295 if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6296 return NULL; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6297 PyErr_Clear(); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6298 if (PyErr_Warn(PyExc_UnicodeWarning, |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6299 (op == Py_EQ) ? |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6300 "Unicode equal comparison " |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6301 "failed to convert both arguments to Unicode - " |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6302 "interpreting them as being unequal" : |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6303 "Unicode unequal comparison " |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6304 "failed to convert both arguments to Unicode - " |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6305 "interpreting them as being unequal" |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6306 ) < 0) |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6307 return NULL; |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6308 result = (op == Py_NE); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6309 return PyBool_FromLong(result); |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6310 } |
|
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
6311 |
|
11997
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6312 int PyUnicode_Contains(PyObject *container, |
|
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6313 PyObject *element) |
|
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6314 { |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6315 PyObject *str, *sub; |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6316 int result; |
|
11997
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6317 |
|
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6318 /* Coerce the two arguments */ |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6319 sub = PyUnicode_FromObject(element); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6320 if (!sub) { |
|
13049
0be4b7b650e3
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12957
diff
changeset
|
6321 PyErr_SetString(PyExc_TypeError, |
|
24598
12e146171f3d
Committing patch #591250 which provides "str1 in str2" when str1 is a
Barry Warsaw <barry@python.org>
parents:
24392
diff
changeset
|
6322 "'in <string>' requires string as left operand"); |
|
38243
0ba711a496e6
needforspeed: fixed unicode "in" operator to use same implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38241
diff
changeset
|
6323 return -1; |
|
0ba711a496e6
needforspeed: fixed unicode "in" operator to use same implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38241
diff
changeset
|
6324 } |
|
0ba711a496e6
needforspeed: fixed unicode "in" operator to use same implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38241
diff
changeset
|
6325 |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6326 str = PyUnicode_FromObject(container); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6327 if (!str) { |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6328 Py_DECREF(sub); |
|
38243
0ba711a496e6
needforspeed: fixed unicode "in" operator to use same implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38241
diff
changeset
|
6329 return -1; |
|
0ba711a496e6
needforspeed: fixed unicode "in" operator to use same implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38241
diff
changeset
|
6330 } |
|
11997
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6331 |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6332 result = stringlib_contains_obj(str, sub); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6333 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6334 Py_DECREF(str); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6335 Py_DECREF(sub); |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6336 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6337 return result; |
|
11997
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6338 } |
|
38310eb0659e
Add sq_contains implementation.
Guido van Rossum <guido@python.org>
parents:
11968
diff
changeset
|
6339 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6340 /* 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
|
6341 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6342 PyObject *PyUnicode_Concat(PyObject *left, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6343 PyObject *right) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6344 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6345 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
|
6346 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6347 /* 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
|
6348 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
|
6349 if (u == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6350 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6351 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
|
6352 if (v == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6353 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6354 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6355 /* Shortcuts */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6356 if (v == unicode_empty) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6357 Py_DECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6358 return (PyObject *)u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6359 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6360 if (u == unicode_empty) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6361 Py_DECREF(u); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6362 return (PyObject *)v; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6363 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6364 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6365 /* 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
|
6366 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
|
6367 if (w == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6368 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6369 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
|
6370 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
|
6371 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6372 Py_DECREF(u); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6373 Py_DECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6374 return (PyObject *)w; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6375 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6376 onError: |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6377 Py_XDECREF(u); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6378 Py_XDECREF(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6379 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6380 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6381 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6382 PyDoc_STRVAR(count__doc__, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6383 "S.count(sub[, start[, end]]) -> int\n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6384 \n\ |
|
38236
7e16fcce34a1
docstring tweaks: count counts non-overlapping substrings, not
Fredrik Lundh <fredrik@pythonware.com>
parents:
38182
diff
changeset
|
6385 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
|
6386 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
|
6387 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
|
6388 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6389 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6390 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
|
6391 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6392 PyUnicodeObject *substring; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6393 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
|
6394 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
|
6395 PyObject *result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6396 |
| 12629 | 6397 if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 6398 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) | |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6399 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6400 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6401 substring = (PyUnicodeObject *)PyUnicode_FromObject( |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
6402 (PyObject *)substring); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6403 if (substring == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6404 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
|
6405 |
|
38402
6b03a47eb54d
needforspeed: use a macro to fix slice indexes
Fredrik Lundh <fredrik@pythonware.com>
parents:
38401
diff
changeset
|
6406 FIX_START_END(self); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6407 |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
6408 result = PyInt_FromSsize_t( |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
6409 stringlib_count(self->str + start, end - start, |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
6410 substring->str, substring->length) |
|
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
6411 ); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6412 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6413 Py_DECREF(substring); |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
6414 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6415 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6416 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6417 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6418 PyDoc_STRVAR(encode__doc__, |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6419 "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
|
6420 \n\ |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6421 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
|
6422 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
|
6423 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
|
6424 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
|
6425 '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
|
6426 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
|
6427 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6428 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6429 unicode_encode(PyUnicodeObject *self, PyObject *args) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6430 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6431 char *encoding = NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6432 char *errors = NULL; |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6433 PyObject *v; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6434 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6435 if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6436 return NULL; |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6437 v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6438 if (v == NULL) |
|
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6439 goto onError; |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6440 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
|
6441 PyErr_Format(PyExc_TypeError, |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6442 "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
|
6443 "(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
|
6444 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
|
6445 Py_DECREF(v); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6446 return NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6447 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6448 return v; |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6449 |
|
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6450 onError: |
|
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6451 return NULL; |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6452 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6453 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6454 PyDoc_STRVAR(decode__doc__, |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6455 "S.decode([encoding[,errors]]) -> string or unicode\n\ |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6456 \n\ |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6457 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
|
6458 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
|
6459 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
|
6460 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
|
6461 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
|
6462 able to handle UnicodeDecodeErrors."); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6463 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6464 static PyObject * |
|
32571
bec0c7618625
Fix a copy&paste typo.
Marc-André Lemburg <mal@egenix.com>
parents:
32552
diff
changeset
|
6465 unicode_decode(PyUnicodeObject *self, PyObject *args) |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6466 { |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6467 char *encoding = NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6468 char *errors = NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6469 PyObject *v; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6470 |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6471 if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6472 return NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6473 v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6474 if (v == NULL) |
|
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6475 goto onError; |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6476 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
|
6477 PyErr_Format(PyExc_TypeError, |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6478 "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
|
6479 "(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
|
6480 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
|
6481 Py_DECREF(v); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6482 return NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6483 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
6484 return v; |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6485 |
|
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
6486 onError: |
|
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
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(expandtabs__doc__, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6491 "S.expandtabs([tabsize]) -> unicode\n\ |
|
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 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
|
6494 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
|
6495 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6496 static PyObject* |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6497 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
|
6498 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6499 Py_UNICODE *e; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6500 Py_UNICODE *p; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6501 Py_UNICODE *q; |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6502 Py_UNICODE *qe; |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6503 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
|
6504 PyUnicodeObject *u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6505 int tabsize = 8; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6506 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6507 if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6508 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6509 |
|
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
|
6510 /* First pass: determine size of output string */ |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6511 i = 0; /* chars up to and including most recent \n or \r */ |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6512 j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6513 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
|
6514 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
|
6515 if (*p == '\t') { |
|
42004
e62b606eff0b
Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents:
40908
diff
changeset
|
6516 if (tabsize > 0) { |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6517 incr = tabsize - (j % tabsize); /* cannot overflow */ |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6518 if (j > PY_SSIZE_T_MAX - incr) |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6519 goto overflow1; |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6520 j += incr; |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6521 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6522 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6523 else { |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6524 if (j > PY_SSIZE_T_MAX - 1) |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6525 goto overflow1; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6526 j++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6527 if (*p == '\n' || *p == '\r') { |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6528 if (i > PY_SSIZE_T_MAX - j) |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6529 goto overflow1; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6530 i += j; |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6531 j = 0; |
|
11968
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 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6534 |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6535 if (i > PY_SSIZE_T_MAX - j) |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6536 goto overflow1; |
|
42004
e62b606eff0b
Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents:
40908
diff
changeset
|
6537 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6538 /* 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
|
6539 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
|
6540 if (!u) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6541 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6542 |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6543 j = 0; /* same as in first pass */ |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6544 q = u->str; /* next output char */ |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6545 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
|
6546 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6547 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
|
6548 if (*p == '\t') { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6549 if (tabsize > 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6550 i = tabsize - (j % tabsize); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6551 j += i; |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6552 while (i--) { |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6553 if (q >= qe) |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6554 goto overflow2; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6555 *q++ = ' '; |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6556 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6557 } |
|
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 else { |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6560 if (q >= qe) |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6561 goto overflow2; |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6562 *q++ = *p; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6563 j++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6564 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
|
6565 j = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6566 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6567 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6568 return (PyObject*) u; |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6569 |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6570 overflow2: |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6571 Py_DECREF(u); |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6572 overflow1: |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6573 PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
6574 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6575 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6576 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6577 PyDoc_STRVAR(find__doc__, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6578 "S.find(sub [,start [,end]]) -> int\n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6579 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6580 Return the lowest index in S where substring sub is found,\n\ |
|
42402
251033a1c2e3
Bug #1763149: use proper slice syntax in docstring.
Georg Brandl <georg@python.org>
parents:
42333
diff
changeset
|
6581 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
|
6582 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
|
6583 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6584 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
|
6585 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6586 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6587 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
|
6588 { |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6589 PyObject *substring; |
|
43886
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43710
diff
changeset
|
6590 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
|
6591 Py_ssize_t end; |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6592 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
|
6593 |
|
43886
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43710
diff
changeset
|
6594 if (!_ParseTupleFinds(args, &substring, &start, &end)) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6595 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6596 |
|
38441
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
6597 result = stringlib_find_slice( |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
6598 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
|
6599 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
|
6600 start, end |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
6601 ); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6602 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6603 Py_DECREF(substring); |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6604 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6605 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
|
6606 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6607 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6608 static PyObject * |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6609 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
|
6610 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6611 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
|
6612 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
|
6613 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6614 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6615 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6616 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
|
6617 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6618 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6619 static long |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6620 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
|
6621 { |
|
13522
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6622 /* 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
|
6623 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
|
6624 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
|
6625 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
|
6626 dictionary keys. */ |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6627 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6628 register Py_ssize_t len; |
|
13522
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6629 register Py_UNICODE *p; |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6630 register long x; |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6631 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6632 if (self->hash != -1) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6633 return self->hash; |
|
13522
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6634 len = PyUnicode_GET_SIZE(self); |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6635 p = PyUnicode_AS_UNICODE(self); |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6636 x = *p << 7; |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6637 while (--len >= 0) |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6638 x = (1000003*x) ^ *p++; |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6639 x ^= PyUnicode_GET_SIZE(self); |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6640 if (x == -1) |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6641 x = -2; |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6642 self->hash = x; |
|
acc4638f37d5
- changed hash calculation for unicode strings. the new
Fredrik Lundh <fredrik@pythonware.com>
parents:
13411
diff
changeset
|
6643 return x; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6644 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6645 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6646 PyDoc_STRVAR(index__doc__, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6647 "S.index(sub [,start [,end]]) -> int\n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6648 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6649 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
|
6650 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6651 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6652 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
|
6653 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6654 Py_ssize_t result; |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6655 PyObject *substring; |
|
43886
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43710
diff
changeset
|
6656 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
|
6657 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
|
6658 |
|
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43710
diff
changeset
|
6659 if (!_ParseTupleFinds(args, &substring, &start, &end)) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6660 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6661 |
|
38441
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
6662 result = stringlib_find_slice( |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
6663 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
|
6664 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
|
6665 start, end |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
6666 ); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6667 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6668 Py_DECREF(substring); |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6669 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6670 if (result < 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6671 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
|
6672 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6673 } |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
6674 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6675 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
|
6676 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6677 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6678 PyDoc_STRVAR(islower__doc__, |
|
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
|
6679 "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
|
6680 \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
|
6681 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
|
6682 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
|
6683 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6684 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
|
6685 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
|
6686 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6687 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
|
6688 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
|
6689 int cased; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6690 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6691 /* 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
|
6692 if (PyUnicode_GET_SIZE(self) == 1) |
|
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
|
6693 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
|
6694 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6695 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6696 if (PyUnicode_GET_SIZE(self) == 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
|
6697 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6698 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6699 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
|
6700 cased = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6701 for (; p < e; p++) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6702 register const Py_UNICODE ch = *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
|
6703 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6704 if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
|
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
|
6705 return PyBool_FromLong(0); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6706 else if (!cased && Py_UNICODE_ISLOWER(ch)) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6707 cased = 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6708 } |
|
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
|
6709 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
|
6710 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6711 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6712 PyDoc_STRVAR(isupper__doc__, |
|
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
|
6713 "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
|
6714 \n\ |
|
30707
9e21e56bf5f3
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30653
diff
changeset
|
6715 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
|
6716 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
|
6717 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6718 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
|
6719 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
|
6720 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6721 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
|
6722 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
|
6723 int cased; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6724 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6725 /* 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
|
6726 if (PyUnicode_GET_SIZE(self) == 1) |
|
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
|
6727 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
|
6728 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6729 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6730 if (PyUnicode_GET_SIZE(self) == 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
|
6731 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6732 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6733 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
|
6734 cased = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6735 for (; p < e; p++) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6736 register const Py_UNICODE ch = *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
|
6737 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6738 if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
|
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
|
6739 return PyBool_FromLong(0); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6740 else if (!cased && Py_UNICODE_ISUPPER(ch)) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6741 cased = 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6742 } |
|
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
|
6743 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
|
6744 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6745 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6746 PyDoc_STRVAR(istitle__doc__, |
|
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
|
6747 "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
|
6748 \n\ |
|
30707
9e21e56bf5f3
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30653
diff
changeset
|
6749 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
|
6750 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
|
6751 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
|
6752 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
|
6753 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6754 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
|
6755 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
|
6756 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6757 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
|
6758 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
|
6759 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
|
6760 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6761 /* 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
|
6762 if (PyUnicode_GET_SIZE(self) == 1) |
|
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 return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
|
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 (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
|
6765 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6766 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6767 if (PyUnicode_GET_SIZE(self) == 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
|
6768 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6769 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6770 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
|
6771 cased = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6772 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
|
6773 for (; p < e; p++) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6774 register const Py_UNICODE ch = *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
|
6775 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6776 if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6777 if (previous_is_cased) |
|
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
|
6778 return PyBool_FromLong(0); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6779 previous_is_cased = 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6780 cased = 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6781 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6782 else if (Py_UNICODE_ISLOWER(ch)) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6783 if (!previous_is_cased) |
|
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
|
6784 return PyBool_FromLong(0); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6785 previous_is_cased = 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6786 cased = 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6787 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6788 else |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6789 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
|
6790 } |
|
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
|
6791 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
|
6792 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6793 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6794 PyDoc_STRVAR(isspace__doc__, |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6795 "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
|
6796 \n\ |
|
30707
9e21e56bf5f3
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30653
diff
changeset
|
6797 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
|
6798 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
|
6799 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6800 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
|
6801 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
|
6802 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6803 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
|
6804 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
|
6805 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6806 /* 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
|
6807 if (PyUnicode_GET_SIZE(self) == 1 && |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6808 Py_UNICODE_ISSPACE(*p)) |
|
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
|
6809 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
|
6810 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6811 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6812 if (PyUnicode_GET_SIZE(self) == 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
|
6813 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6814 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6815 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
|
6816 for (; p < e; p++) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6817 if (!Py_UNICODE_ISSPACE(*p)) |
|
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
|
6818 return PyBool_FromLong(0); |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6819 } |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6820 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
|
6821 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6822 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6823 PyDoc_STRVAR(isalpha__doc__, |
|
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
|
6824 "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
|
6825 \n\ |
|
30707
9e21e56bf5f3
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30653
diff
changeset
|
6826 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
|
6827 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
|
6828 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6829 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
|
6830 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
|
6831 { |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6832 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
|
6833 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
|
6834 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6835 /* 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
|
6836 if (PyUnicode_GET_SIZE(self) == 1 && |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6837 Py_UNICODE_ISALPHA(*p)) |
|
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
|
6838 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
|
6839 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6840 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6841 if (PyUnicode_GET_SIZE(self) == 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
|
6842 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
|
6843 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6844 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
|
6845 for (; p < e; p++) { |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6846 if (!Py_UNICODE_ISALPHA(*p)) |
|
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
|
6847 return PyBool_FromLong(0); |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6848 } |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6849 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
|
6850 } |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6851 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6852 PyDoc_STRVAR(isalnum__doc__, |
|
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
|
6853 "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
|
6854 \n\ |
|
30707
9e21e56bf5f3
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30653
diff
changeset
|
6855 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
|
6856 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
|
6857 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6858 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
|
6859 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
|
6860 { |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6861 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
|
6862 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
|
6863 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6864 /* 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
|
6865 if (PyUnicode_GET_SIZE(self) == 1 && |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6866 Py_UNICODE_ISALNUM(*p)) |
|
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
|
6867 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
|
6868 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6869 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6870 if (PyUnicode_GET_SIZE(self) == 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
|
6871 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
|
6872 |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6873 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
|
6874 for (; p < e; p++) { |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6875 if (!Py_UNICODE_ISALNUM(*p)) |
|
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
|
6876 return PyBool_FromLong(0); |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6877 } |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6878 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
|
6879 } |
|
9a1b961e0c3a
Added new .isalpha() and .isalnum() methods which provide interfaces
Marc-André Lemburg <mal@egenix.com>
parents:
13337
diff
changeset
|
6880 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6881 PyDoc_STRVAR(isdecimal__doc__, |
|
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
|
6882 "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
|
6883 \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
|
6884 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
|
6885 False otherwise."); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6886 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6887 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
|
6888 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
|
6889 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6890 register const Py_UNICODE *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
|
6891 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
|
6892 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6893 /* 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
|
6894 if (PyUnicode_GET_SIZE(self) == 1 && |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6895 Py_UNICODE_ISDECIMAL(*p)) |
|
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
|
6896 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
|
6897 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6898 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6899 if (PyUnicode_GET_SIZE(self) == 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
|
6900 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6901 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6902 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
|
6903 for (; p < e; p++) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6904 if (!Py_UNICODE_ISDECIMAL(*p)) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6905 return PyBool_FromLong(0); |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6906 } |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6907 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
|
6908 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6909 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6910 PyDoc_STRVAR(isdigit__doc__, |
|
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
|
6911 "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
|
6912 \n\ |
|
30707
9e21e56bf5f3
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30653
diff
changeset
|
6913 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
|
6914 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
|
6915 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6916 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
|
6917 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
|
6918 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6919 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
|
6920 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
|
6921 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6922 /* 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
|
6923 if (PyUnicode_GET_SIZE(self) == 1 && |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6924 Py_UNICODE_ISDIGIT(*p)) |
|
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
|
6925 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
|
6926 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6927 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6928 if (PyUnicode_GET_SIZE(self) == 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
|
6929 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6930 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6931 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
|
6932 for (; p < e; p++) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6933 if (!Py_UNICODE_ISDIGIT(*p)) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6934 return PyBool_FromLong(0); |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6935 } |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6936 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
|
6937 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6938 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6939 PyDoc_STRVAR(isnumeric__doc__, |
|
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
|
6940 "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
|
6941 \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
|
6942 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
|
6943 False otherwise."); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6944 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6945 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
|
6946 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
|
6947 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6948 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
|
6949 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
|
6950 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6951 /* 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
|
6952 if (PyUnicode_GET_SIZE(self) == 1 && |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6953 Py_UNICODE_ISNUMERIC(*p)) |
|
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
|
6954 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
|
6955 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6956 /* Special case for empty strings */ |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
6957 if (PyUnicode_GET_SIZE(self) == 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
|
6958 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12909
diff
changeset
|
6959 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6960 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
|
6961 for (; p < e; p++) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6962 if (!Py_UNICODE_ISNUMERIC(*p)) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6963 return PyBool_FromLong(0); |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6964 } |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22521
diff
changeset
|
6965 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
|
6966 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6967 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6968 PyDoc_STRVAR(join__doc__, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6969 "S.join(sequence) -> unicode\n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6970 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6971 Return a string which is the concatenation of the strings in the\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6972 sequence. The separator between elements is S."); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6973 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6974 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
|
6975 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
|
6976 { |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19121
diff
changeset
|
6977 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
|
6978 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6979 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
6980 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
|
6981 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
|
6982 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6983 return self->length; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6984 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6985 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
6986 PyDoc_STRVAR(ljust__doc__, |
|
32197
3a84e23b042f
- SF #962502: Add two more methods for unicode type; width() and
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31873
diff
changeset
|
6987 "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
|
6988 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6989 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
|
6990 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
|
6991 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6992 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6993 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
|
6994 { |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
6995 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
|
6996 Py_UNICODE fillchar = ' '; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
6997 |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
6998 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
|
6999 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7000 |
|
19885
72e6eb653312
More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19849
diff
changeset
|
7001 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
|
7002 Py_INCREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7003 return (PyObject*) self; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7004 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7005 |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
7006 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
|
7007 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7008 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7009 PyDoc_STRVAR(lower__doc__, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7010 "S.lower() -> unicode\n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7011 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7012 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
|
7013 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7014 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
|
7015 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
|
7016 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7017 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
|
7018 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7019 |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7020 #define LEFTSTRIP 0 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7021 #define RIGHTSTRIP 1 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7022 #define BOTHSTRIP 2 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7023 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7024 /* Arrays indexed by above */ |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7025 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
|
7026 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7027 #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
|
7028 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7029 /* 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
|
7030 PyObject * |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7031 _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
|
7032 { |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7033 Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7034 Py_ssize_t len = PyUnicode_GET_SIZE(self); |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7035 Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7036 Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7037 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
|
7038 |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
7039 BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
7040 |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7041 i = 0; |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7042 if (striptype != RIGHTSTRIP) { |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
7043 while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
7044 i++; |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
7045 } |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7046 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7047 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7048 j = len; |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7049 if (striptype != LEFTSTRIP) { |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
7050 do { |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
7051 j--; |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
7052 } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
7053 j++; |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7054 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7055 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7056 if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
7057 Py_INCREF(self); |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
7058 return (PyObject*)self; |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7059 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7060 else |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
7061 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
|
7062 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7063 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7064 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7065 static PyObject * |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7066 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
|
7067 { |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7068 Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7069 Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7070 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7071 i = 0; |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7072 if (striptype != RIGHTSTRIP) { |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7073 while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7074 i++; |
|
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 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7077 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7078 j = len; |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7079 if (striptype != LEFTSTRIP) { |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7080 do { |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7081 j--; |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7082 } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7083 j++; |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7084 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7085 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7086 if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7087 Py_INCREF(self); |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7088 return (PyObject*)self; |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7089 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7090 else |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7091 return PyUnicode_FromUnicode(s+i, j-i); |
|
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 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7094 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7095 static PyObject * |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7096 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
|
7097 { |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7098 PyObject *sep = NULL; |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7099 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7100 if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7101 return NULL; |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7102 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7103 if (sep != NULL && sep != Py_None) { |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7104 if (PyUnicode_Check(sep)) |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7105 return _PyUnicode_XStrip(self, striptype, sep); |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7106 else if (PyString_Check(sep)) { |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7107 PyObject *res; |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7108 sep = PyUnicode_FromObject(sep); |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7109 if (sep==NULL) |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7110 return NULL; |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7111 res = _PyUnicode_XStrip(self, striptype, sep); |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7112 Py_DECREF(sep); |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7113 return res; |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7114 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7115 else { |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7116 PyErr_Format(PyExc_TypeError, |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7117 "%s arg must be None, unicode or str", |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7118 STRIPNAME(striptype)); |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7119 return NULL; |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7120 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7121 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7122 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7123 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
|
7124 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7125 |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7126 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7127 PyDoc_STRVAR(strip__doc__, |
|
28575
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
28559
diff
changeset
|
7128 "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
|
7129 \n\ |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7130 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
|
7131 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
|
7132 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
|
7133 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
|
7134 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7135 static PyObject * |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7136 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
|
7137 { |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7138 if (PyTuple_GET_SIZE(args) == 0) |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7139 return do_strip(self, BOTHSTRIP); /* Common case */ |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7140 else |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7141 return do_argstrip(self, BOTHSTRIP, args); |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7142 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7143 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7144 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7145 PyDoc_STRVAR(lstrip__doc__, |
|
28575
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
28559
diff
changeset
|
7146 "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
|
7147 \n\ |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7148 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
|
7149 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
|
7150 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
|
7151 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7152 static PyObject * |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7153 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
|
7154 { |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7155 if (PyTuple_GET_SIZE(args) == 0) |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7156 return do_strip(self, LEFTSTRIP); /* Common case */ |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7157 else |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7158 return do_argstrip(self, LEFTSTRIP, args); |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7159 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7160 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7161 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7162 PyDoc_STRVAR(rstrip__doc__, |
|
28575
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
28559
diff
changeset
|
7163 "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
|
7164 \n\ |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7165 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
|
7166 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
|
7167 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
|
7168 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7169 static PyObject * |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7170 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
|
7171 { |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7172 if (PyTuple_GET_SIZE(args) == 0) |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7173 return do_strip(self, RIGHTSTRIP); /* Common case */ |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7174 else |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7175 return do_argstrip(self, RIGHTSTRIP, args); |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7176 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7177 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23205
diff
changeset
|
7178 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7179 static PyObject* |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7180 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
|
7181 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7182 PyUnicodeObject *u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7183 Py_UNICODE *p; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7184 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
|
7185 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
|
7186 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7187 if (len < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7188 len = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7189 |
|
19885
72e6eb653312
More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19849
diff
changeset
|
7190 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
|
7191 /* 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
|
7192 Py_INCREF(str); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7193 return (PyObject*) str; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7194 } |
|
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
|
7195 |
|
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
|
7196 /* 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
|
7197 * 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
|
7198 */ |
|
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
|
7199 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
|
7200 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
|
7201 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
|
7202 "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
|
7203 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
|
7204 } |
|
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
|
7205 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
|
7206 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
|
7207 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
|
7208 "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
|
7209 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
|
7210 } |
|
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
|
7211 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
|
7212 if (!u) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7213 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7214 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7215 p = u->str; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7216 |
|
38238
feb00df91922
needforspeed: speed up unicode repeat, unicode string copy
Fredrik Lundh <fredrik@pythonware.com>
parents:
38236
diff
changeset
|
7217 if (str->length == 1 && len > 0) { |
|
feb00df91922
needforspeed: speed up unicode repeat, unicode string copy
Fredrik Lundh <fredrik@pythonware.com>
parents:
38236
diff
changeset
|
7218 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
|
7219 } else { |
|
38241
5eaf8b9587d8
unicode_repeat(): Change type of local to Py_ssize_t,
Tim Peters <tim.peters@gmail.com>
parents:
38240
diff
changeset
|
7220 Py_ssize_t done = 0; /* number of characters copied this far */ |
|
38239
604af867c65f
needforspeed: use memcpy for "long" strings; use a better algorithm
Fredrik Lundh <fredrik@pythonware.com>
parents:
38238
diff
changeset
|
7221 if (done < nchars) { |
|
38238
feb00df91922
needforspeed: speed up unicode repeat, unicode string copy
Fredrik Lundh <fredrik@pythonware.com>
parents:
38236
diff
changeset
|
7222 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
|
7223 done = str->length; |
|
604af867c65f
needforspeed: use memcpy for "long" strings; use a better algorithm
Fredrik Lundh <fredrik@pythonware.com>
parents:
38238
diff
changeset
|
7224 } |
|
604af867c65f
needforspeed: use memcpy for "long" strings; use a better algorithm
Fredrik Lundh <fredrik@pythonware.com>
parents:
38238
diff
changeset
|
7225 while (done < nchars) { |
|
604af867c65f
needforspeed: use memcpy for "long" strings; use a better algorithm
Fredrik Lundh <fredrik@pythonware.com>
parents:
38238
diff
changeset
|
7226 int n = (done <= nchars-done) ? done : nchars-done; |
|
604af867c65f
needforspeed: use memcpy for "long" strings; use a better algorithm
Fredrik Lundh <fredrik@pythonware.com>
parents:
38238
diff
changeset
|
7227 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
|
7228 done += n; |
|
604af867c65f
needforspeed: use memcpy for "long" strings; use a better algorithm
Fredrik Lundh <fredrik@pythonware.com>
parents:
38238
diff
changeset
|
7229 } |
|
604af867c65f
needforspeed: use memcpy for "long" strings; use a better algorithm
Fredrik Lundh <fredrik@pythonware.com>
parents:
38238
diff
changeset
|
7230 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7231 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7232 return (PyObject*) u; |
|
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 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7235 PyObject *PyUnicode_Replace(PyObject *obj, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7236 PyObject *subobj, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7237 PyObject *replobj, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7238 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
|
7239 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7240 PyObject *self; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7241 PyObject *str1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7242 PyObject *str2; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7243 PyObject *result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7244 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7245 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
|
7246 if (self == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7247 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7248 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
|
7249 if (str1 == NULL) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7250 Py_DECREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7251 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7252 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7253 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
|
7254 if (str2 == NULL) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7255 Py_DECREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7256 Py_DECREF(str1); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7257 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7258 } |
|
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
|
7259 result = replace((PyUnicodeObject *)self, |
|
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
7260 (PyUnicodeObject *)str1, |
|
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
7261 (PyUnicodeObject *)str2, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7262 maxcount); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7263 Py_DECREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7264 Py_DECREF(str1); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7265 Py_DECREF(str2); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7266 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7267 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7268 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7269 PyDoc_STRVAR(replace__doc__, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7270 "S.replace (old, new[, maxsplit]) -> unicode\n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7271 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7272 Return a copy of S with all occurrences of substring\n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7273 old replaced by new. If the optional argument maxsplit is\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7274 given, only the first maxsplit 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
|
7275 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7276 static PyObject* |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7277 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
|
7278 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7279 PyUnicodeObject *str1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7280 PyUnicodeObject *str2; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7281 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
|
7282 PyObject *result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7283 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7284 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
|
7285 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7286 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
|
7287 if (str1 == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7288 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7289 str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
|
27774
bfc524379307
Fix two refcounting bugs
Walter Dörwald <walter@livinglogic.de>
parents:
27555
diff
changeset
|
7290 if (str2 == NULL) { |
|
bfc524379307
Fix two refcounting bugs
Walter Dörwald <walter@livinglogic.de>
parents:
27555
diff
changeset
|
7291 Py_DECREF(str1); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7292 return NULL; |
|
27774
bfc524379307
Fix two refcounting bugs
Walter Dörwald <walter@livinglogic.de>
parents:
27555
diff
changeset
|
7293 } |
|
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 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
|
7296 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7297 Py_DECREF(str1); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7298 Py_DECREF(str2); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7299 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7300 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7301 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7302 static |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7303 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
|
7304 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7305 return unicodeescape_string(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
|
7306 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
|
7307 1); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7308 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7309 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7310 PyDoc_STRVAR(rfind__doc__, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7311 "S.rfind(sub [,start [,end]]) -> int\n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7312 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7313 Return the highest index in S where substring sub is found,\n\ |
|
42402
251033a1c2e3
Bug #1763149: use proper slice syntax in docstring.
Georg Brandl <georg@python.org>
parents:
42333
diff
changeset
|
7314 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
|
7315 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
|
7316 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7317 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
|
7318 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7319 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7320 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
|
7321 { |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
7322 PyObject *substring; |
|
43886
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43710
diff
changeset
|
7323 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
|
7324 Py_ssize_t end; |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
7325 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
|
7326 |
|
43886
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43710
diff
changeset
|
7327 if (!_ParseTupleFinds(args, &substring, &start, &end)) |
|
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43710
diff
changeset
|
7328 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7329 |
|
38441
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
7330 result = stringlib_rfind_slice( |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
7331 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
|
7332 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
|
7333 start, end |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
7334 ); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7335 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7336 Py_DECREF(substring); |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
7337 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
7338 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
|
7339 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7340 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7341 PyDoc_STRVAR(rindex__doc__, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7342 "S.rindex(sub [,start [,end]]) -> int\n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7343 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7344 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
|
7345 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7346 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7347 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
|
7348 { |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
7349 PyObject *substring; |
|
43886
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43710
diff
changeset
|
7350 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
|
7351 Py_ssize_t end; |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
7352 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
|
7353 |
|
43886
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43710
diff
changeset
|
7354 if (!_ParseTupleFinds(args, &substring, &start, &end)) |
|
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43710
diff
changeset
|
7355 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7356 |
|
38441
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
7357 result = stringlib_rfind_slice( |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
7358 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
|
7359 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
|
7360 start, end |
|
72327f8f4a79
needforspeed: stringlib refactoring: changed find_obj to find_slice,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
7361 ); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7362 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7363 Py_DECREF(substring); |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38422
diff
changeset
|
7364 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7365 if (result < 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7366 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
|
7367 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7368 } |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7369 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
|
7370 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7371 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7372 PyDoc_STRVAR(rjust__doc__, |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
7373 "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
|
7374 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7375 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
|
7376 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
|
7377 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7378 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7379 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
|
7380 { |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
7381 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
|
7382 Py_UNICODE fillchar = ' '; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
7383 |
|
37662
0678ce9fb6b2
Change more ints to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37661
diff
changeset
|
7384 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
|
7385 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7386 |
|
19885
72e6eb653312
More on bug 460020: disable many optimizations of unicode subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19849
diff
changeset
|
7387 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
|
7388 Py_INCREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7389 return (PyObject*) self; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7390 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7391 |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30765
diff
changeset
|
7392 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
|
7393 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7394 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7395 static PyObject* |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7396 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
|
7397 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7398 /* standard clamping */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7399 if (start < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7400 start = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7401 if (end < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7402 end = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7403 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
|
7404 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
|
7405 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
|
7406 /* 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
|
7407 Py_INCREF(self); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7408 return (PyObject*) self; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7409 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7410 if (start > end) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7411 start = end; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7412 /* copy slice */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7413 return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7414 end - start); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7415 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7416 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7417 PyObject *PyUnicode_Split(PyObject *s, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7418 PyObject *sep, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7419 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
|
7420 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7421 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
|
7422 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7423 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
|
7424 if (s == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7425 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7426 if (sep != NULL) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7427 sep = PyUnicode_FromObject(sep); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7428 if (sep == NULL) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7429 Py_DECREF(s); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7430 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7431 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7432 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7433 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7434 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
|
7435 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7436 Py_DECREF(s); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7437 Py_XDECREF(sep); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7438 return result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7439 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7440 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7441 PyDoc_STRVAR(split__doc__, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7442 "S.split([sep [,maxsplit]]) -> list of strings\n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7443 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7444 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
|
7445 delimiter string. If maxsplit is given, at most maxsplit\n\ |
|
33495
2d6e1854082e
Add a missing line continuation character.
Thomas Heller <theller@ctypes.org>
parents:
33487
diff
changeset
|
7446 splits are done. If sep is not specified or is None,\n\ |
|
33486
53c1357c9c63
Enhance the docstrings for unicode.split() and string.split()
Walter Dörwald <walter@livinglogic.de>
parents:
33428
diff
changeset
|
7447 any whitespace string is a separator."); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7448 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7449 static PyObject* |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7450 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
|
7451 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7452 PyObject *substring = Py_None; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7453 Py_ssize_t maxcount = -1; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7454 |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7455 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
|
7456 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7457 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7458 if (substring == Py_None) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7459 return split(self, NULL, maxcount); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7460 else if (PyUnicode_Check(substring)) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7461 return split(self, (PyUnicodeObject *)substring, maxcount); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7462 else |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7463 return PyUnicode_Split((PyObject *)self, substring, maxcount); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7464 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7465 |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7466 PyObject * |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7467 PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7468 { |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7469 PyObject* str_obj; |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7470 PyObject* sep_obj; |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7471 PyObject* out; |
|
38383
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7472 |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7473 str_obj = PyUnicode_FromObject(str_in); |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7474 if (!str_obj) |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7475 return NULL; |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7476 sep_obj = PyUnicode_FromObject(sep_in); |
|
38383
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7477 if (!sep_obj) { |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7478 Py_DECREF(str_obj); |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7479 return NULL; |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7480 } |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7481 |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
7482 out = stringlib_partition( |
|
38383
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7483 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
|
7484 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
|
7485 ); |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7486 |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7487 Py_DECREF(sep_obj); |
|
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
7488 Py_DECREF(str_obj); |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7489 |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7490 return out; |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7491 } |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7492 |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7493 |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7494 PyObject * |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7495 PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7496 { |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7497 PyObject* str_obj; |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7498 PyObject* sep_obj; |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7499 PyObject* out; |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7500 |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7501 str_obj = PyUnicode_FromObject(str_in); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7502 if (!str_obj) |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7503 return NULL; |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7504 sep_obj = PyUnicode_FromObject(sep_in); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7505 if (!sep_obj) { |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7506 Py_DECREF(str_obj); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7507 return NULL; |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7508 } |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7509 |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38391
diff
changeset
|
7510 out = stringlib_rpartition( |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7511 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
|
7512 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
|
7513 ); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7514 |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7515 Py_DECREF(sep_obj); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7516 Py_DECREF(str_obj); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7517 |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7518 return out; |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7519 } |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7520 |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7521 PyDoc_STRVAR(partition__doc__, |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7522 "S.partition(sep) -> (head, sep, tail)\n\ |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7523 \n\ |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7524 Searches for the separator sep in S, and returns the part before it,\n\ |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7525 the separator itself, and the part after it. If the separator is not\n\ |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7526 found, returns S and two empty strings."); |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7527 |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7528 static PyObject* |
|
38338
958f91684209
needforspeed: use METH_O for argument handling, which made partition some
Fredrik Lundh <fredrik@pythonware.com>
parents:
38335
diff
changeset
|
7529 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
|
7530 { |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7531 return PyUnicode_Partition((PyObject *)self, separator); |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7532 } |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38304
diff
changeset
|
7533 |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7534 PyDoc_STRVAR(rpartition__doc__, |
|
39734
cfb77fff0240
Fix endcase for str.rpartition()
Raymond Hettinger <python@rcn.com>
parents:
39621
diff
changeset
|
7535 "S.rpartition(sep) -> (tail, sep, head)\n\ |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7536 \n\ |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7537 Searches for the separator sep in S, starting at the end of S, and returns\n\ |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7538 the part before it, the separator itself, and the part after it. If the\n\ |
|
39734
cfb77fff0240
Fix endcase for str.rpartition()
Raymond Hettinger <python@rcn.com>
parents:
39621
diff
changeset
|
7539 separator is not found, returns two empty strings and S."); |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7540 |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7541 static PyObject* |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7542 unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7543 { |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7544 return PyUnicode_RPartition((PyObject *)self, separator); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7545 } |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7546 |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7547 PyObject *PyUnicode_RSplit(PyObject *s, |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7548 PyObject *sep, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7549 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
|
7550 { |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7551 PyObject *result; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7552 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7553 s = PyUnicode_FromObject(s); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7554 if (s == NULL) |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7555 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7556 if (sep != NULL) { |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7557 sep = PyUnicode_FromObject(sep); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7558 if (sep == NULL) { |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7559 Py_DECREF(s); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7560 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7561 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7562 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7563 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7564 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
|
7565 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7566 Py_DECREF(s); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7567 Py_XDECREF(sep); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7568 return result; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7569 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7570 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7571 PyDoc_STRVAR(rsplit__doc__, |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7572 "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7573 \n\ |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7574 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
|
7575 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
|
7576 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
|
7577 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
|
7578 is a separator."); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7579 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7580 static PyObject* |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7581 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
|
7582 { |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7583 PyObject *substring = Py_None; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7584 Py_ssize_t maxcount = -1; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7585 |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7586 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
|
7587 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7588 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7589 if (substring == Py_None) |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7590 return rsplit(self, NULL, maxcount); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7591 else if (PyUnicode_Check(substring)) |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7592 return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7593 else |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7594 return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7595 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
7596 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7597 PyDoc_STRVAR(splitlines__doc__, |
| 12369 | 7598 "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
|
7599 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7600 Return a list of the lines in S, breaking at line boundaries.\n\ |
| 12369 | 7601 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
|
7602 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
|
7603 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7604 static PyObject* |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7605 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
|
7606 { |
| 12369 | 7607 int keepends = 0; |
| 7608 | |
| 7609 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
|
7610 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7611 |
| 12369 | 7612 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
|
7613 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7614 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7615 static |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7616 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
|
7617 { |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
7618 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
|
7619 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7620 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7621 PyDoc_STRVAR(swapcase__doc__, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7622 "S.swapcase() -> unicode\n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7623 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7624 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
|
7625 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
|
7626 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7627 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
|
7628 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
|
7629 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7630 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
|
7631 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7632 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7633 PyDoc_STRVAR(translate__doc__, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7634 "S.translate(table) -> unicode\n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7635 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7636 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
|
7637 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
|
7638 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
|
7639 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
|
7640 are deleted."); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7641 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7642 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
|
7643 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
|
7644 { |
|
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
|
7645 return PyUnicode_TranslateCharmap(self->str, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7646 self->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
|
7647 table, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7648 "ignore"); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7649 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7650 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7651 PyDoc_STRVAR(upper__doc__, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7652 "S.upper() -> unicode\n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7653 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7654 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
|
7655 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7656 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
|
7657 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
|
7658 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7659 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
|
7660 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7661 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7662 PyDoc_STRVAR(zfill__doc__, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7663 "S.zfill(width) -> unicode\n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7664 \n\ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7665 Pad a numeric string x with zeros on the left, to fill a field\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7666 of the specified width. The string x 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
|
7667 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7668 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7669 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
|
7670 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7671 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
|
7672 PyUnicodeObject *u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7673 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7674 Py_ssize_t width; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7675 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
|
7676 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7677 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7678 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
|
7679 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
|
7680 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
|
7681 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
|
7682 } |
|
19ea00feda33
Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents:
23045
diff
changeset
|
7683 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
|
7684 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
|
7685 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
|
7686 PyUnicode_GET_SIZE(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
|
7687 ); |
|
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 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7690 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
|
7691 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7692 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
|
7693 |
|
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
|
7694 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
|
7695 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
|
7696 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7697 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
|
7698 /* 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
|
7699 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
|
7700 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
|
7701 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7702 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7703 return (PyObject*) u; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7704 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7705 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7706 #if 0 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7707 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
|
7708 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
|
7709 { |
|
920a7633ae08
Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents:
45053
diff
changeset
|
7710 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
|
7711 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7712 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7713 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7714 PyDoc_STRVAR(startswith__doc__, |
|
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
|
7715 "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
|
7716 \n\ |
|
28559
aff080f2efb4
Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents:
28494
diff
changeset
|
7717 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
|
7718 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
|
7719 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
|
7720 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
|
7721 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7722 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7723 unicode_startswith(PyUnicodeObject *self, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7724 PyObject *args) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7725 { |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7726 PyObject *subobj; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7727 PyUnicodeObject *substring; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7728 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
|
7729 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
|
7730 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
|
7731 |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7732 if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
| 12629 | 7733 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7734 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
|
7735 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
|
7736 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
|
7737 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
|
7738 substring = (PyUnicodeObject *)PyUnicode_FromObject( |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7739 PyTuple_GET_ITEM(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
|
7740 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
|
7741 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
|
7742 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
|
7743 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
|
7744 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
|
7745 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
|
7746 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7747 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7748 /* 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
|
7749 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
|
7750 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7751 substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7752 if (substring == 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
|
7753 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
|
7754 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
|
7755 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
|
7756 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
|
7757 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7758 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7759 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
7760 PyDoc_STRVAR(endswith__doc__, |
|
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
|
7761 "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
|
7762 \n\ |
|
28559
aff080f2efb4
Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents:
28494
diff
changeset
|
7763 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
|
7764 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
|
7765 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
|
7766 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
|
7767 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7768 static PyObject * |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7769 unicode_endswith(PyUnicodeObject *self, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7770 PyObject *args) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7771 { |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7772 PyObject *subobj; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7773 PyUnicodeObject *substring; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7774 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
|
7775 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
|
7776 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
|
7777 |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7778 if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7779 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7780 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
|
7781 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
|
7782 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
|
7783 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
|
7784 substring = (PyUnicodeObject *)PyUnicode_FromObject( |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7785 PyTuple_GET_ITEM(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
|
7786 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
|
7787 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
|
7788 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
|
7789 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
|
7790 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
|
7791 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
|
7792 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7793 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7794 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
|
7795 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7796 substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7797 if (substring == 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
|
7798 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
|
7799 |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38587
diff
changeset
|
7800 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
|
7801 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
|
7802 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
|
7803 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7804 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7805 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7806 /* 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
|
7807 #include "stringlib/string_format.h" |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7808 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7809 PyDoc_STRVAR(format__doc__, |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7810 "S.format(*args, **kwargs) -> unicode\n\ |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7811 \n\ |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7812 "); |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7813 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7814 PyDoc_STRVAR(p_format__doc__, |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7815 "S.__format__(format_spec) -> unicode\n\ |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7816 \n\ |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7817 "); |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
45341
diff
changeset
|
7818 |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7819 |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7820 static PyObject * |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7821 unicode_getnewargs(PyUnicodeObject *v) |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7822 { |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7823 return Py_BuildValue("(u#)", v->str, v->length); |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7824 } |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7825 |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7826 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7827 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
|
7828 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7829 /* 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
|
7830 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
|
7831 |
| 37306 | 7832 {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19121
diff
changeset
|
7833 {"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
|
7834 {"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
|
7835 {"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
|
7836 {"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
|
7837 {"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
|
7838 {"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
|
7839 {"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
|
7840 {"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
|
7841 {"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
|
7842 {"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
|
7843 {"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
|
7844 {"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
|
7845 {"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
|
7846 {"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
|
7847 {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32363
diff
changeset
|
7848 {"decode", (PyCFunction) unicode_decode, METH_VARARGS, decode__doc__}, |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19121
diff
changeset
|
7849 /* {"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
|
7850 {"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
|
7851 {"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
|
7852 {"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
|
7853 {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
7854 {"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
|
7855 {"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
|
7856 {"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
|
7857 {"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
|
7858 {"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
|
7859 {"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
|
7860 {"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
|
7861 {"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
|
7862 {"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
|
7863 {"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
|
7864 {"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
|
7865 {"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
|
7866 {"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
|
7867 {"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
|
7868 {"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
|
7869 {"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
|
7870 {"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
|
7871 {"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
|
7872 {"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
|
7873 {"__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
|
7874 {"_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
|
7875 {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7876 #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
|
7877 {"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
|
7878 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7879 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7880 #if 0 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7881 /* 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
|
7882 {"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
|
7883 #endif |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7884 |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27175
diff
changeset
|
7885 {"__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
|
7886 {NULL, NULL} |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7887 }; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7888 |
|
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
|
7889 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
|
7890 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
|
7891 { |
|
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
|
7892 if (!PyUnicode_Check(v)) { |
|
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
|
7893 Py_INCREF(Py_NotImplemented); |
|
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
|
7894 return Py_NotImplemented; |
|
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
|
7895 } |
|
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
|
7896 return PyUnicode_Format(v, 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
|
7897 } |
|
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
|
7898 |
|
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
|
7899 static PyNumberMethods unicode_as_number = { |
|
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
|
7900 0, /*nb_add*/ |
|
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
|
7901 0, /*nb_subtract*/ |
|
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
|
7902 0, /*nb_multiply*/ |
|
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
|
7903 0, /*nb_divide*/ |
|
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
|
7904 unicode_mod, /*nb_remainder*/ |
|
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
|
7905 }; |
|
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
|
7906 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7907 static PySequenceMethods unicode_as_sequence = { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7908 (lenfunc) unicode_length, /* sq_length */ |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36945
diff
changeset
|
7909 PyUnicode_Concat, /* sq_concat */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7910 (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7911 (ssizeargfunc) unicode_getitem, /* sq_item */ |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7912 (ssizessizeargfunc) unicode_slice, /* sq_slice */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7913 0, /* sq_ass_item */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7914 0, /* sq_ass_slice */ |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36945
diff
changeset
|
7915 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
|
7916 }; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7917 |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7918 static PyObject* |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7919 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
|
7920 { |
|
39491
2312e178b34d
Correct an accidentally removed previous patch.
Marc-André Lemburg <mal@egenix.com>
parents:
39487
diff
changeset
|
7921 if (PyIndex_Check(item)) { |
|
2312e178b34d
Correct an accidentally removed previous patch.
Marc-André Lemburg <mal@egenix.com>
parents:
39487
diff
changeset
|
7922 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
|
7923 if (i == -1 && PyErr_Occurred()) |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7924 return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7925 if (i < 0) |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
7926 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
|
7927 return unicode_getitem(self, i); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7928 } else if (PySlice_Check(item)) { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7929 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
|
7930 Py_UNICODE* source_buf; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7931 Py_UNICODE* result_buf; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7932 PyObject* result; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7933 |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
7934 if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7935 &start, &stop, &step, &slicelength) < 0) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7936 return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7937 } |
|
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 if (slicelength <= 0) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7940 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
|
7941 } 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
|
7942 PyUnicode_CheckExact(self)) { |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42652
diff
changeset
|
7943 Py_INCREF(self); |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42652
diff
changeset
|
7944 return (PyObject *)self; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42652
diff
changeset
|
7945 } else if (step == 1) { |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42652
diff
changeset
|
7946 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
|
7947 } else { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7948 source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
|
45804
d33cadb595f6
Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents:
45731
diff
changeset
|
7949 result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
|
d33cadb595f6
Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents:
45731
diff
changeset
|
7950 sizeof(Py_UNICODE)); |
|
36203
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
7951 |
|
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
7952 if (result_buf == NULL) |
|
44cfca6136c6
Stop maintaining the buildno file.
Martin v. Löwis <martin@v.loewis.de>
parents:
36056
diff
changeset
|
7953 return PyErr_NoMemory(); |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7954 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7955 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
|
7956 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
|
7957 } |
|
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
|
7958 |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7959 result = PyUnicode_FromUnicode(result_buf, slicelength); |
|
45804
d33cadb595f6
Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents:
45731
diff
changeset
|
7960 PyObject_FREE(result_buf); |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7961 return result; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7962 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7963 } else { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7964 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
|
7965 return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7966 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7967 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7968 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7969 static PyMappingMethods unicode_as_mapping = { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7970 (lenfunc)unicode_length, /* mp_length */ |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7971 (binaryfunc)unicode_subscript, /* mp_subscript */ |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7972 (objobjargproc)0, /* mp_ass_subscript */ |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7973 }; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
7974 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7975 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
|
7976 unicode_buffer_getreadbuf(PyUnicodeObject *self, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7977 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
|
7978 const void **ptr) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7979 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7980 if (index != 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7981 PyErr_SetString(PyExc_SystemError, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7982 "accessing non-existent unicode segment"); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7983 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7984 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7985 *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
|
7986 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
|
7987 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7988 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7989 static Py_ssize_t |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
7990 unicode_buffer_getwritebuf(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
|
7991 const void **ptr) |
|
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 PyErr_SetString(PyExc_TypeError, |
|
23837
359a57e96d5e
Fix typo in exception message
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
7994 "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
|
7995 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7996 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7997 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7998 static int |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7999 unicode_buffer_getsegcount(PyUnicodeObject *self, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8000 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
|
8001 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8002 if (lenp) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8003 *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
|
8004 return 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8005 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8006 |
|
36519
d6d415df0df0
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36510
diff
changeset
|
8007 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
|
8008 unicode_buffer_getcharbuf(PyUnicodeObject *self, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8009 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
|
8010 const void **ptr) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8011 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8012 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
|
8013 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8014 if (index != 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8015 PyErr_SetString(PyExc_SystemError, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8016 "accessing non-existent unicode segment"); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8017 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8018 } |
|
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
|
8019 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
|
8020 if (str == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8021 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8022 *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
|
8023 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
|
8024 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8025 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8026 /* 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
|
8027 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8028 static PyObject * |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8029 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
|
8030 { |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8031 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
|
8032 if (argidx < arglen) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8033 (*p_argidx)++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8034 if (arglen < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8035 return args; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8036 else |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8037 return PyTuple_GetItem(args, argidx); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8038 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8039 PyErr_SetString(PyExc_TypeError, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8040 "not enough arguments for format string"); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8041 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8042 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8043 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8044 #define F_LJUST (1<<0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8045 #define F_SIGN (1<<1) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8046 #define F_BLANK (1<<2) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8047 #define F_ALT (1<<3) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8048 #define F_ZERO (1<<4) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8049 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8050 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
|
8051 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
|
8052 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8053 register Py_ssize_t i; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8054 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
|
8055 for (i = len - 1; i >= 0; i--) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8056 buffer[i] = (Py_UNICODE) charbuffer[i]; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8057 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8058 return len; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8059 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8060 |
|
36261
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8061 static int |
|
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8062 doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
|
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8063 { |
|
36508
7322dccba224
doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents:
36506
diff
changeset
|
8064 Py_ssize_t result; |
|
7322dccba224
doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents:
36506
diff
changeset
|
8065 |
|
36261
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8066 PyOS_ascii_formatd((char *)buffer, len, format, x); |
|
36508
7322dccba224
doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents:
36506
diff
changeset
|
8067 result = strtounicode(buffer, (char *)buffer); |
|
7322dccba224
doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents:
36506
diff
changeset
|
8068 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
|
8069 } |
|
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8070 |
|
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8071 static int |
|
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8072 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
|
8073 { |
|
36508
7322dccba224
doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents:
36506
diff
changeset
|
8074 Py_ssize_t result; |
|
7322dccba224
doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents:
36506
diff
changeset
|
8075 |
|
36261
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8076 PyOS_snprintf((char *)buffer, len, format, x); |
|
36508
7322dccba224
doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents:
36506
diff
changeset
|
8077 result = strtounicode(buffer, (char *)buffer); |
|
7322dccba224
doubletounicode(), longtounicode():
Tim Peters <tim.peters@gmail.com>
parents:
36506
diff
changeset
|
8078 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
|
8079 } |
|
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8080 |
|
24736
ff05eef3a6d1
Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents:
24703
diff
changeset
|
8081 /* 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
|
8082 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
|
8083 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
|
8084 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8085 static int |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8086 formatfloat(Py_UNICODE *buf, |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8087 size_t buflen, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8088 int flags, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8089 int prec, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8090 int type, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8091 PyObject *v) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8092 { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8093 /* fmt = '%#.' + `prec` + `type` |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8094 worst case length = 3 + 10 (len of INT_MAX) + 1 = 14 (use 20)*/ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8095 char fmt[20]; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8096 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
|
8097 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8098 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
|
8099 if (x == -1.0 && PyErr_Occurred()) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8100 return -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8101 if (prec < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8102 prec = 6; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8103 if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8104 type = 'g'; |
|
26825
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8105 /* Worst case length calc to ensure no buffer overrun: |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8106 |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8107 'g' formats: |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8108 fmt = %#.<prec>g |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8109 buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8110 for any double rep.) |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8111 len = 1 + prec + 1 + 2 + 5 = 9 + prec |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8112 |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8113 'f' formats: |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8114 buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8115 len = 1 + 50 + 1 + prec = 52 + prec |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8116 |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8117 If prec=0 the effective precision is 1 (the leading digit is |
|
30441
76e998aee644
On c.l.py, Martin v. Löwis said that Py_UNICODE could be of a signed type,
Tim Peters <tim.peters@gmail.com>
parents:
30439
diff
changeset
|
8118 always given), therefore increase the length by one. |
|
26825
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8119 |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8120 */ |
|
42216
86d1dbfc5833
Patch #1673759: add a missing overflow check when formatting floats
Georg Brandl <georg@python.org>
parents:
42024
diff
changeset
|
8121 if (((type == 'g' || type == 'G') && |
|
86d1dbfc5833
Patch #1673759: add a missing overflow check when formatting floats
Georg Brandl <georg@python.org>
parents:
42024
diff
changeset
|
8122 buflen <= (size_t)10 + (size_t)prec) || |
|
26825
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8123 (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8124 PyErr_SetString(PyExc_OverflowError, |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8125 "formatted float is too long (precision too large?)"); |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8126 return -1; |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8127 } |
|
21082
c8d775b9ff92
formatfloat(), formatint(): Conversion of sprintf() to PyOS_snprintf()
Barry Warsaw <barry@python.org>
parents:
21067
diff
changeset
|
8128 PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
|
26825
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8129 (flags&F_ALT) ? "#" : "", |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26274
diff
changeset
|
8130 prec, type); |
|
36261
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8131 return doubletounicode(buf, buflen, fmt, x); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8132 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8133 |
|
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
|
8134 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
|
8135 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
|
8136 { |
|
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
|
8137 char *buf; |
|
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
|
8138 int i, len; |
|
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
|
8139 PyObject *str; /* temporary string object. */ |
|
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
|
8140 PyUnicodeObject *result; |
|
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
|
8141 |
|
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
|
8142 str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
|
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
|
8143 if (!str) |
|
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
|
8144 return NULL; |
|
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
|
8145 result = _PyUnicode_New(len); |
|
36877
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36675
diff
changeset
|
8146 if (!result) { |
|
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36675
diff
changeset
|
8147 Py_DECREF(str); |
|
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36675
diff
changeset
|
8148 return NULL; |
|
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36675
diff
changeset
|
8149 } |
|
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
|
8150 for (i = 0; i < len; i++) |
|
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
|
8151 result->str[i] = buf[i]; |
|
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
|
8152 result->str[len] = 0; |
|
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
|
8153 Py_DECREF(str); |
|
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
|
8154 return (PyObject*)result; |
|
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
|
8155 } |
|
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
|
8156 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8157 static int |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8158 formatint(Py_UNICODE *buf, |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8159 size_t buflen, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8160 int flags, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8161 int prec, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8162 int type, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8163 PyObject *v) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8164 { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8165 /* 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
|
8166 * 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
|
8167 * + 1 + 1 |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8168 * = 24 |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8169 */ |
|
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
|
8170 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
|
8171 char *sign; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8172 long x; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8173 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8174 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
|
8175 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
|
8176 return -1; |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8177 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
|
8178 type = 'd'; |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8179 } |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8180 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
|
8181 sign = "-"; |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8182 else |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8183 sign = ""; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8184 if (prec < 0) |
|
22087
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8185 prec = 1; |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8186 |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8187 /* 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
|
8188 * 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
|
8189 */ |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8190 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
|
8191 PyErr_SetString(PyExc_OverflowError, |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8192 "formatted integer is too long (precision too large?)"); |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8193 return -1; |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8194 } |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8195 |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8196 if ((flags & F_ALT) && |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8197 (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
|
8198 /* 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
|
8199 * 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
|
8200 * - 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
|
8201 * 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
|
8202 * %#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
|
8203 * hex() function |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8204 * - 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
|
8205 * 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
|
8206 * (Metrowerks, Compaq Tru64) |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8207 * - 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
|
8208 * 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
|
8209 * 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
|
8210 * |
|
22087
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8211 * 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
|
8212 * 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
|
8213 * of %#x/%#X. |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8214 * |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8215 * 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
|
8216 * 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
|
8217 */ |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8218 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
|
8219 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
|
8220 } |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8221 else { |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8222 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
|
8223 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
|
8224 prec, type); |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
8225 } |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
8226 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
|
8227 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
|
8228 else |
|
36261
303dea847639
- Patch #1400181, fix unicode string formatting to not use the locale.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36236
diff
changeset
|
8229 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
|
8230 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8231 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8232 static int |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8233 formatchar(Py_UNICODE *buf, |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8234 size_t buflen, |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8235 PyObject *v) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8236 { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8237 /* 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
|
8238 if (PyUnicode_Check(v)) { |
|
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8239 if (PyUnicode_GET_SIZE(v) != 1) |
|
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8240 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8241 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
|
8242 } |
|
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8243 |
|
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8244 else if (PyString_Check(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
|
8245 if (PyString_GET_SIZE(v) != 1) |
|
12899
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8246 goto onError; |
|
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8247 buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
|
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8248 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8249 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8250 else { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8251 /* Integer input truncated to a character */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8252 long x; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8253 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
|
8254 if (x == -1 && PyErr_Occurred()) |
|
12899
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8255 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
|
8256 #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
|
8257 if (x < 0 || x > 0x10ffff) { |
|
28494
32bea41fef68
Change formatchar(), so that u"%c" % 0xffffffff now raises
Walter Dörwald <walter@livinglogic.de>
parents:
28242
diff
changeset
|
8258 PyErr_SetString(PyExc_OverflowError, |
|
24737
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
8259 "%c arg not in range(0x110000) " |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
8260 "(wide Python build)"); |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
8261 return -1; |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
8262 } |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
8263 #else |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
8264 if (x < 0 || x > 0xffff) { |
|
28494
32bea41fef68
Change formatchar(), so that u"%c" % 0xffffffff now raises
Walter Dörwald <walter@livinglogic.de>
parents:
28242
diff
changeset
|
8265 PyErr_SetString(PyExc_OverflowError, |
|
24737
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
8266 "%c arg not in range(0x10000) " |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
8267 "(narrow Python build)"); |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
8268 return -1; |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
8269 } |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
8270 #endif |
|
eaa87632b1e8
Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents:
24736
diff
changeset
|
8271 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
|
8272 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8273 buf[1] = '\0'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8274 return 1; |
|
12899
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8275 |
|
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8276 onError: |
|
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8277 PyErr_SetString(PyExc_TypeError, |
|
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8278 "%c requires int or char"); |
|
9df91f91e5cc
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12876
diff
changeset
|
8279 return -1; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8280 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8281 |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8282 /* 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
|
8283 |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8284 FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8285 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
|
8286 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
|
8287 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
|
8288 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
|
8289 */ |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8290 #define FORMATBUFLEN (size_t)120 |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8291 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8292 PyObject *PyUnicode_Format(PyObject *format, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8293 PyObject *args) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8294 { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8295 Py_UNICODE *fmt, *res; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8296 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
|
8297 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
|
8298 PyUnicodeObject *result = NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8299 PyObject *dict = NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8300 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
|
8301 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8302 if (format == NULL || args == NULL) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8303 PyErr_BadInternalCall(); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8304 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8305 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8306 uformat = PyUnicode_FromObject(format); |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
8307 if (uformat == NULL) |
|
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
8308 return NULL; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8309 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
|
8310 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
|
8311 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8312 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
|
8313 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
|
8314 if (result == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8315 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8316 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
|
8317 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8318 if (PyTuple_Check(args)) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8319 arglen = PyTuple_Size(args); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8320 argidx = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8321 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8322 else { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8323 arglen = -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8324 argidx = -2; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8325 } |
|
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
|
8326 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
|
8327 !PyObject_TypeCheck(args, &PyBaseString_Type)) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8328 dict = args; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8329 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8330 while (--fmtcnt >= 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8331 if (*fmt != '%') { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8332 if (--rescnt < 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8333 rescnt = fmtcnt + 100; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8334 reslen += rescnt; |
|
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
|
8335 if (_PyUnicode_Resize(&result, reslen) < 0) |
|
36877
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36675
diff
changeset
|
8336 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8337 res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8338 --rescnt; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8339 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8340 *res++ = *fmt++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8341 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8342 else { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8343 /* Got a format specifier */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8344 int flags = 0; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8345 Py_ssize_t width = -1; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8346 int prec = -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8347 Py_UNICODE c = '\0'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8348 Py_UNICODE fill; |
|
45500
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8349 int isnumok; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8350 PyObject *v = NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8351 PyObject *temp = NULL; |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8352 Py_UNICODE *pbuf; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8353 Py_UNICODE sign; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8354 Py_ssize_t len; |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8355 Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8356 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8357 fmt++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8358 if (*fmt == '(') { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8359 Py_UNICODE *keystart; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8360 Py_ssize_t keylen; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8361 PyObject *key; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8362 int pcount = 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8363 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8364 if (dict == NULL) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8365 PyErr_SetString(PyExc_TypeError, |
|
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
|
8366 "format requires a mapping"); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8367 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8368 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8369 ++fmt; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8370 --fmtcnt; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8371 keystart = fmt; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8372 /* Skip over balanced parentheses */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8373 while (pcount > 0 && --fmtcnt >= 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8374 if (*fmt == ')') |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8375 --pcount; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8376 else if (*fmt == '(') |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8377 ++pcount; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8378 fmt++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8379 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8380 keylen = fmt - keystart - 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8381 if (fmtcnt < 0 || pcount > 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8382 PyErr_SetString(PyExc_ValueError, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8383 "incomplete format key"); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8384 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8385 } |
|
21028
3dfbbf42bf16
Fix for bug #438164: %-formatting using Unicode objects.
Marc-André Lemburg <mal@egenix.com>
parents:
20563
diff
changeset
|
8386 #if 0 |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
8387 /* keys are converted to strings using UTF-8 and |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8388 then looked up since Python uses strings to hold |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8389 variables names etc. in its namespaces and we |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
8390 wouldn't want to break common idioms. */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8391 key = PyUnicode_EncodeUTF8(keystart, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8392 keylen, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8393 NULL); |
|
21028
3dfbbf42bf16
Fix for bug #438164: %-formatting using Unicode objects.
Marc-André Lemburg <mal@egenix.com>
parents:
20563
diff
changeset
|
8394 #else |
|
3dfbbf42bf16
Fix for bug #438164: %-formatting using Unicode objects.
Marc-André Lemburg <mal@egenix.com>
parents:
20563
diff
changeset
|
8395 key = PyUnicode_FromUnicode(keystart, keylen); |
|
3dfbbf42bf16
Fix for bug #438164: %-formatting using Unicode objects.
Marc-André Lemburg <mal@egenix.com>
parents:
20563
diff
changeset
|
8396 #endif |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8397 if (key == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8398 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8399 if (args_owned) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8400 Py_DECREF(args); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8401 args_owned = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8402 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8403 args = PyObject_GetItem(dict, key); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8404 Py_DECREF(key); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8405 if (args == NULL) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8406 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8407 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8408 args_owned = 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8409 arglen = -1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8410 argidx = -2; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8411 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8412 while (--fmtcnt >= 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8413 switch (c = *fmt++) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8414 case '-': flags |= F_LJUST; continue; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8415 case '+': flags |= F_SIGN; continue; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8416 case ' ': flags |= F_BLANK; continue; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8417 case '#': flags |= F_ALT; continue; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8418 case '0': flags |= F_ZERO; continue; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8419 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8420 break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8421 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8422 if (c == '*') { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8423 v = getnextarg(args, arglen, &argidx); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8424 if (v == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8425 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8426 if (!PyInt_Check(v)) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8427 PyErr_SetString(PyExc_TypeError, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8428 "* wants int"); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8429 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8430 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8431 width = PyInt_AsLong(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8432 if (width < 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8433 flags |= F_LJUST; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8434 width = -width; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8435 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8436 if (--fmtcnt >= 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8437 c = *fmt++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8438 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8439 else if (c >= '0' && c <= '9') { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8440 width = c - '0'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8441 while (--fmtcnt >= 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8442 c = *fmt++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8443 if (c < '0' || c > '9') |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8444 break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8445 if ((width*10) / 10 != width) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8446 PyErr_SetString(PyExc_ValueError, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8447 "width too big"); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8448 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8449 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8450 width = width*10 + (c - '0'); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8451 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8452 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8453 if (c == '.') { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8454 prec = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8455 if (--fmtcnt >= 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8456 c = *fmt++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8457 if (c == '*') { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8458 v = getnextarg(args, arglen, &argidx); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8459 if (v == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8460 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8461 if (!PyInt_Check(v)) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8462 PyErr_SetString(PyExc_TypeError, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8463 "* wants int"); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8464 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8465 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8466 prec = PyInt_AsLong(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8467 if (prec < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8468 prec = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8469 if (--fmtcnt >= 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8470 c = *fmt++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8471 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8472 else if (c >= '0' && c <= '9') { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8473 prec = c - '0'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8474 while (--fmtcnt >= 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8475 c = Py_CHARMASK(*fmt++); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8476 if (c < '0' || c > '9') |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8477 break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8478 if ((prec*10) / 10 != prec) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8479 PyErr_SetString(PyExc_ValueError, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8480 "prec too big"); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8481 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8482 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8483 prec = prec*10 + (c - '0'); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8484 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8485 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8486 } /* prec */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8487 if (fmtcnt >= 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8488 if (c == 'h' || c == 'l' || c == 'L') { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8489 if (--fmtcnt >= 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8490 c = *fmt++; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8491 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8492 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8493 if (fmtcnt < 0) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8494 PyErr_SetString(PyExc_ValueError, |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8495 "incomplete format"); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8496 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8497 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8498 if (c != '%') { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8499 v = getnextarg(args, arglen, &argidx); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8500 if (v == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8501 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8502 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8503 sign = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8504 fill = ' '; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8505 switch (c) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8506 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8507 case '%': |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8508 pbuf = formatbuf; |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8509 /* presume that buffer length is at least 1 */ |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8510 pbuf[0] = '%'; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8511 len = 1; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8512 break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8513 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8514 case 's': |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8515 case 'r': |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8516 if (PyUnicode_Check(v) && c == 's') { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8517 temp = v; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8518 Py_INCREF(temp); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8519 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8520 else { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8521 PyObject *unicode; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8522 if (c == 's') |
|
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
|
8523 temp = PyObject_Unicode(v); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8524 else |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8525 temp = PyObject_Repr(v); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8526 if (temp == NULL) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8527 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
|
8528 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
|
8529 /* 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
|
8530 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
|
8531 /* convert to string to Unicode */ |
|
36945
0f19a6fc9756
- Reindent a confusingly indented piece of code (no intended code changes
Thomas Wouters <thomas@python.org>
parents:
36924
diff
changeset
|
8532 unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8533 PyString_GET_SIZE(temp), |
|
36945
0f19a6fc9756
- Reindent a confusingly indented piece of code (no intended code changes
Thomas Wouters <thomas@python.org>
parents:
36924
diff
changeset
|
8534 NULL, |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8535 "strict"); |
|
36945
0f19a6fc9756
- Reindent a confusingly indented piece of code (no intended code changes
Thomas Wouters <thomas@python.org>
parents:
36924
diff
changeset
|
8536 Py_DECREF(temp); |
|
0f19a6fc9756
- Reindent a confusingly indented piece of code (no intended code changes
Thomas Wouters <thomas@python.org>
parents:
36924
diff
changeset
|
8537 temp = unicode; |
|
0f19a6fc9756
- Reindent a confusingly indented piece of code (no intended code changes
Thomas Wouters <thomas@python.org>
parents:
36924
diff
changeset
|
8538 if (temp == NULL) |
|
0f19a6fc9756
- Reindent a confusingly indented piece of code (no intended code changes
Thomas Wouters <thomas@python.org>
parents:
36924
diff
changeset
|
8539 goto onError; |
|
0f19a6fc9756
- Reindent a confusingly indented piece of code (no intended code changes
Thomas Wouters <thomas@python.org>
parents:
36924
diff
changeset
|
8540 } |
|
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
|
8541 else { |
|
16de4f90ef44
Let u'%s' % obj try obj.__unicode__() first and fallback to obj.__str__().
Marc-André Lemburg <mal@egenix.com>
parents:
32695
diff
changeset
|
8542 Py_DECREF(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
|
8543 PyErr_SetString(PyExc_TypeError, |
|
16de4f90ef44
Let u'%s' % obj try obj.__unicode__() first and fallback to obj.__str__().
Marc-André Lemburg <mal@egenix.com>
parents:
32695
diff
changeset
|
8544 "%s argument has non-string str()"); |
|
16de4f90ef44
Let u'%s' % obj try obj.__unicode__() first and fallback to obj.__str__().
Marc-André Lemburg <mal@egenix.com>
parents:
32695
diff
changeset
|
8545 goto onError; |
|
16de4f90ef44
Let u'%s' % obj try obj.__unicode__() first and fallback to obj.__str__().
Marc-André Lemburg <mal@egenix.com>
parents:
32695
diff
changeset
|
8546 } |
|
16de4f90ef44
Let u'%s' % obj try obj.__unicode__() first and fallback to obj.__str__().
Marc-André Lemburg <mal@egenix.com>
parents:
32695
diff
changeset
|
8547 } |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8548 pbuf = PyUnicode_AS_UNICODE(temp); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8549 len = PyUnicode_GET_SIZE(temp); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8550 if (prec >= 0 && len > prec) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8551 len = prec; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8552 break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8553 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8554 case 'i': |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8555 case 'd': |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8556 case 'u': |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8557 case 'o': |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8558 case 'x': |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8559 case 'X': |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8560 if (c == 'i') |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8561 c = 'd'; |
|
45500
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8562 isnumok = 0; |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8563 if (PyNumber_Check(v)) { |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8564 PyObject *iobj=NULL; |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8565 |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8566 if (PyInt_Check(v) || (PyLong_Check(v))) { |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8567 iobj = v; |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8568 Py_INCREF(iobj); |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8569 } |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8570 else { |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8571 iobj = PyNumber_Int(v); |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8572 if (iobj==NULL) iobj = PyNumber_Long(v); |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8573 } |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8574 if (iobj!=NULL) { |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8575 if (PyInt_Check(iobj)) { |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8576 isnumok = 1; |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8577 pbuf = formatbuf; |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8578 len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8579 flags, prec, c, iobj); |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8580 Py_DECREF(iobj); |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8581 if (len < 0) |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8582 goto onError; |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8583 sign = 1; |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8584 } |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8585 else if (PyLong_Check(iobj)) { |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8586 isnumok = 1; |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8587 temp = formatlong(iobj, flags, prec, c); |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8588 Py_DECREF(iobj); |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8589 if (!temp) |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8590 goto onError; |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8591 pbuf = PyUnicode_AS_UNICODE(temp); |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8592 len = PyUnicode_GET_SIZE(temp); |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8593 sign = 1; |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8594 } |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8595 else { |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8596 Py_DECREF(iobj); |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8597 } |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8598 } |
|
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
|
8599 } |
|
45500
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8600 if (!isnumok) { |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8601 PyErr_Format(PyExc_TypeError, |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8602 "%%%c format: a number is required, " |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
8603 "not %.200s", c, Py_TYPE(v)->tp_name); |
|
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
|
8604 goto onError; |
|
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
|
8605 } |
|
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
|
8606 if (flags & F_ZERO) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8607 fill = '0'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8608 break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8609 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8610 case 'e': |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8611 case 'E': |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8612 case 'f': |
|
30216
7ab1a41f16da
SF bug #795506: Wrong handling of string format code for float values.
Raymond Hettinger <python@rcn.com>
parents:
30164
diff
changeset
|
8613 case 'F': |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8614 case 'g': |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8615 case 'G': |
|
30216
7ab1a41f16da
SF bug #795506: Wrong handling of string format code for float values.
Raymond Hettinger <python@rcn.com>
parents:
30164
diff
changeset
|
8616 if (c == 'F') |
|
7ab1a41f16da
SF bug #795506: Wrong handling of string format code for float values.
Raymond Hettinger <python@rcn.com>
parents:
30164
diff
changeset
|
8617 c = 'f'; |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8618 pbuf = formatbuf; |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8619 len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8620 flags, prec, c, v); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8621 if (len < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8622 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8623 sign = 1; |
|
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
|
8624 if (flags & F_ZERO) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8625 fill = '0'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8626 break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8627 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8628 case 'c': |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8629 pbuf = formatbuf; |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8630 len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8631 if (len < 0) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8632 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8633 break; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8634 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8635 default: |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8636 PyErr_Format(PyExc_ValueError, |
|
15579
31d78f361530
[ Patch #102852 ] Make % error a bit more informative by indicates the
Andrew M. Kuchling <amk@amk.ca>
parents:
15455
diff
changeset
|
8637 "unsupported format character '%c' (0x%x) " |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39734
diff
changeset
|
8638 "at index %zd", |
|
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
|
8639 (31<=c && c<=126) ? (char)c : '?', |
|
25375
4df0e691f4da
Add cast to avoid compiler warning.
Marc-André Lemburg <mal@egenix.com>
parents:
25248
diff
changeset
|
8640 (int)c, |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39734
diff
changeset
|
8641 (Py_ssize_t)(fmt - 1 - |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39734
diff
changeset
|
8642 PyUnicode_AS_UNICODE(uformat))); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8643 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8644 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8645 if (sign) { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8646 if (*pbuf == '-' || *pbuf == '+') { |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
13110
diff
changeset
|
8647 sign = *pbuf++; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8648 len--; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8649 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8650 else if (flags & F_SIGN) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8651 sign = '+'; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8652 else if (flags & F_BLANK) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8653 sign = ' '; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8654 else |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8655 sign = 0; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8656 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8657 if (width < len) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8658 width = len; |
|
25719
f532e9b69551
Fix a nasty endcase reported by Armin Rigo in SF bug 618623:
Guido van Rossum <guido@python.org>
parents:
25375
diff
changeset
|
8659 if (rescnt - (sign != 0) < width) { |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8660 reslen -= rescnt; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8661 rescnt = width + fmtcnt + 100; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8662 reslen += rescnt; |
|
25719
f532e9b69551
Fix a nasty endcase reported by Armin Rigo in SF bug 618623:
Guido van Rossum <guido@python.org>
parents:
25375
diff
changeset
|
8663 if (reslen < 0) { |
|
36877
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36675
diff
changeset
|
8664 Py_XDECREF(temp); |
|
36945
0f19a6fc9756
- Reindent a confusingly indented piece of code (no intended code changes
Thomas Wouters <thomas@python.org>
parents:
36924
diff
changeset
|
8665 PyErr_NoMemory(); |
|
0f19a6fc9756
- Reindent a confusingly indented piece of code (no intended code changes
Thomas Wouters <thomas@python.org>
parents:
36924
diff
changeset
|
8666 goto onError; |
|
25719
f532e9b69551
Fix a nasty endcase reported by Armin Rigo in SF bug 618623:
Guido van Rossum <guido@python.org>
parents:
25375
diff
changeset
|
8667 } |
|
36945
0f19a6fc9756
- Reindent a confusingly indented piece of code (no intended code changes
Thomas Wouters <thomas@python.org>
parents:
36924
diff
changeset
|
8668 if (_PyUnicode_Resize(&result, reslen) < 0) { |
|
0f19a6fc9756
- Reindent a confusingly indented piece of code (no intended code changes
Thomas Wouters <thomas@python.org>
parents:
36924
diff
changeset
|
8669 Py_XDECREF(temp); |
|
0f19a6fc9756
- Reindent a confusingly indented piece of code (no intended code changes
Thomas Wouters <thomas@python.org>
parents:
36924
diff
changeset
|
8670 goto onError; |
|
0f19a6fc9756
- Reindent a confusingly indented piece of code (no intended code changes
Thomas Wouters <thomas@python.org>
parents:
36924
diff
changeset
|
8671 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8672 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
|
8673 + reslen - rescnt; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8674 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8675 if (sign) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8676 if (fill != ' ') |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8677 *res++ = sign; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8678 rescnt--; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8679 if (width > len) |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8680 width--; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8681 } |
|
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
|
8682 if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
|
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
|
8683 assert(pbuf[0] == '0'); |
|
17450
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
8684 assert(pbuf[1] == c); |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
8685 if (fill != ' ') { |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
8686 *res++ = *pbuf++; |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
8687 *res++ = *pbuf++; |
|
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
|
8688 } |
|
17450
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
8689 rescnt -= 2; |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
8690 width -= 2; |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
8691 if (width < 0) |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
8692 width = 0; |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
8693 len -= 2; |
|
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
|
8694 } |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8695 if (width > len && !(flags & F_LJUST)) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8696 do { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8697 --rescnt; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8698 *res++ = fill; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8699 } while (--width > len); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8700 } |
|
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
|
8701 if (fill == ' ') { |
|
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
|
8702 if (sign) |
|
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
|
8703 *res++ = sign; |
|
17450
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
8704 if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
|
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
|
8705 assert(pbuf[0] == '0'); |
|
17450
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
8706 assert(pbuf[1] == c); |
|
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
|
8707 *res++ = *pbuf++; |
|
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
|
8708 *res++ = *pbuf++; |
|
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
|
8709 } |
|
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
|
8710 } |
|
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
|
8711 Py_UNICODE_COPY(res, pbuf, len); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8712 res += len; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8713 rescnt -= len; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8714 while (--width >= len) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8715 --rescnt; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8716 *res++ = ' '; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8717 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8718 if (dict && (argidx < arglen) && c != '%') { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8719 PyErr_SetString(PyExc_TypeError, |
|
23494
76f376bea5e5
Patch 549187. Improve string formatting error message.
Raymond Hettinger <python@rcn.com>
parents:
23295
diff
changeset
|
8720 "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
|
8721 Py_XDECREF(temp); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8722 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8723 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8724 Py_XDECREF(temp); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8725 } /* '%' */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8726 } /* until end */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8727 if (argidx < arglen && !dict) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8728 PyErr_SetString(PyExc_TypeError, |
|
23494
76f376bea5e5
Patch 549187. Improve string formatting error message.
Raymond Hettinger <python@rcn.com>
parents:
23295
diff
changeset
|
8729 "not all arguments converted during string formatting"); |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8730 goto onError; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8731 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8732 |
|
36945
0f19a6fc9756
- Reindent a confusingly indented piece of code (no intended code changes
Thomas Wouters <thomas@python.org>
parents:
36924
diff
changeset
|
8733 if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
|
0f19a6fc9756
- Reindent a confusingly indented piece of code (no intended code changes
Thomas Wouters <thomas@python.org>
parents:
36924
diff
changeset
|
8734 goto onError; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8735 if (args_owned) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8736 Py_DECREF(args); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8737 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8738 Py_DECREF(uformat); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8739 return (PyObject *)result; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8740 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8741 onError: |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8742 Py_XDECREF(result); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8743 Py_DECREF(uformat); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8744 if (args_owned) { |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8745 Py_DECREF(args); |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8746 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8747 return NULL; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8748 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8749 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8750 static PyBufferProcs unicode_as_buffer = { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8751 (readbufferproc) unicode_buffer_getreadbuf, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8752 (writebufferproc) unicode_buffer_getwritebuf, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8753 (segcountproc) unicode_buffer_getsegcount, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8754 (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
|
8755 }; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8756 |
|
24280
1598a1e473e2
staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24071
diff
changeset
|
8757 static PyObject * |
|
19593
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8758 unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
|
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8759 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8760 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8761 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
|
8762 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8763 PyObject *x = NULL; |
|
36675
dba6494735d0
Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36532
diff
changeset
|
8764 static char *kwlist[] = {"string", "encoding", "errors", 0}; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8765 char *encoding = NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8766 char *errors = NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8767 |
|
19593
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8768 if (type != &PyUnicode_Type) |
|
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8769 return unicode_subtype_new(type, args, kwds); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8770 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8771 kwlist, &x, &encoding, &errors)) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8772 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8773 if (x == NULL) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8774 return (PyObject *)_PyUnicode_New(0); |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
8775 if (encoding == NULL && errors == NULL) |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
8776 return PyObject_Unicode(x); |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
8777 else |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8778 return PyUnicode_FromEncodedObject(x, encoding, errors); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8779 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8780 |
|
19593
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8781 static PyObject * |
|
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8782 unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8783 { |
|
19886
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19885
diff
changeset
|
8784 PyUnicodeObject *tmp, *pnew; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36261
diff
changeset
|
8785 Py_ssize_t n; |
|
19593
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8786 |
|
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8787 assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
|
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8788 tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
|
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8789 if (tmp == NULL) |
|
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8790 return NULL; |
|
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8791 assert(PyUnicode_Check(tmp)); |
|
19886
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19885
diff
changeset
|
8792 pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
|
29641
4909aa64405f
SF patch 703666: Several objects don't decref tmp on failure in subtype_new
Raymond Hettinger <python@rcn.com>
parents:
29164
diff
changeset
|
8793 if (pnew == NULL) { |
|
4909aa64405f
SF patch 703666: Several objects don't decref tmp on failure in subtype_new
Raymond Hettinger <python@rcn.com>
parents:
29164
diff
changeset
|
8794 Py_DECREF(tmp); |
|
19593
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8795 return NULL; |
|
29641
4909aa64405f
SF patch 703666: Several objects don't decref tmp on failure in subtype_new
Raymond Hettinger <python@rcn.com>
parents:
29164
diff
changeset
|
8796 } |
|
45804
d33cadb595f6
Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents:
45731
diff
changeset
|
8797 pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
|
19886
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19885
diff
changeset
|
8798 if (pnew->str == NULL) { |
|
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19885
diff
changeset
|
8799 _Py_ForgetReference((PyObject *)pnew); |
|
22981
dc1fb19d990d
Remove PyMalloc_*.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22938
diff
changeset
|
8800 PyObject_Del(pnew); |
|
29641
4909aa64405f
SF patch 703666: Several objects don't decref tmp on failure in subtype_new
Raymond Hettinger <python@rcn.com>
parents:
29164
diff
changeset
|
8801 Py_DECREF(tmp); |
|
27826
7dffcc498c45
Add more missing PyErr_NoMemory() after failled memory allocs
Neal Norwitz <nnorwitz@gmail.com>
parents:
27774
diff
changeset
|
8802 return PyErr_NoMemory(); |
|
19593
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8803 } |
|
19886
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19885
diff
changeset
|
8804 Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
|
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19885
diff
changeset
|
8805 pnew->length = n; |
|
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19885
diff
changeset
|
8806 pnew->hash = tmp->hash; |
|
19593
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8807 Py_DECREF(tmp); |
|
19886
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19885
diff
changeset
|
8808 return (PyObject *)pnew; |
|
19593
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8809 } |
|
a80b4b006eb7
Make unicode subclassable.
Guido van Rossum <guido@python.org>
parents:
19301
diff
changeset
|
8810 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
8811 PyDoc_STRVAR(unicode_doc, |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8812 "unicode(string [, encoding[, errors]]) -> object\n\ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8813 \n\ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8814 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
|
8815 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
|
8816 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
|
8817 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8818 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
|
8819 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8820 "unicode", /* tp_name */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8821 sizeof(PyUnicodeObject), /* tp_size */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8822 0, /* tp_itemsize */ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8823 /* Slots */ |
|
20277
2983b54256c7
Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents:
20018
diff
changeset
|
8824 (destructor)unicode_dealloc, /* tp_dealloc */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8825 0, /* tp_print */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8826 0, /* tp_getattr */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8827 0, /* tp_setattr */ |
|
39487
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
8828 0, /* tp_compare */ |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36945
diff
changeset
|
8829 unicode_repr, /* tp_repr */ |
|
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
|
8830 &unicode_as_number, /* tp_as_number */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8831 &unicode_as_sequence, /* tp_as_sequence */ |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23562
diff
changeset
|
8832 &unicode_as_mapping, /* tp_as_mapping */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8833 (hashfunc) unicode_hash, /* tp_hash*/ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8834 0, /* tp_call*/ |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8835 (reprfunc) unicode_str, /* tp_str */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8836 PyObject_GenericGetAttr, /* tp_getattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8837 0, /* tp_setattro */ |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8838 &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
|
8839 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
|
40908
d01bd37bf5ce
Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
8840 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8841 unicode_doc, /* tp_doc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8842 0, /* tp_traverse */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8843 0, /* tp_clear */ |
|
39487
635b09b1f542
Slightly revised version of patch #1538956:
Marc-André Lemburg <mal@egenix.com>
parents:
39463
diff
changeset
|
8844 PyUnicode_RichCompare, /* tp_richcompare */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8845 0, /* tp_weaklistoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8846 0, /* tp_iter */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8847 0, /* tp_iternext */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8848 unicode_methods, /* tp_methods */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8849 0, /* tp_members */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8850 0, /* tp_getset */ |
|
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
|
8851 &PyBaseString_Type, /* tp_base */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8852 0, /* tp_dict */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8853 0, /* tp_descr_get */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8854 0, /* tp_descr_set */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8855 0, /* tp_dictoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8856 0, /* tp_init */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8857 0, /* tp_alloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
8858 unicode_new, /* tp_new */ |
|
22981
dc1fb19d990d
Remove PyMalloc_*.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22938
diff
changeset
|
8859 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
|
8860 }; |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8861 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8862 /* 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
|
8863 |
|
13660
a1ce07218781
Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents:
13635
diff
changeset
|
8864 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
|
8865 { |
|
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
|
8866 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
|
8867 |
|
38257
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8868 /* 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
|
8869 Py_UNICODE linebreak[] = { |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8870 0x000A, /* LINE FEED */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8871 0x000D, /* CARRIAGE RETURN */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8872 0x001C, /* FILE SEPARATOR */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8873 0x001D, /* GROUP SEPARATOR */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8874 0x001E, /* RECORD SEPARATOR */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8875 0x0085, /* NEXT LINE */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8876 0x2028, /* LINE SEPARATOR */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8877 0x2029, /* PARAGRAPH SEPARATOR */ |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8878 }; |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8879 |
|
12645
c4eb70d24a87
M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents:
12629
diff
changeset
|
8880 /* 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
|
8881 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
|
8882 numfree = 0; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8883 unicode_empty = _PyUnicode_New(0); |
|
39129
f2031f15d99d
Handle allocation failures gracefully. Found with failmalloc.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38817
diff
changeset
|
8884 if (!unicode_empty) |
|
f2031f15d99d
Handle allocation failures gracefully. Found with failmalloc.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38817
diff
changeset
|
8885 return; |
|
f2031f15d99d
Handle allocation failures gracefully. Found with failmalloc.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38817
diff
changeset
|
8886 |
|
12876
243593f11fcf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12646
diff
changeset
|
8887 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
|
8888 for (i = 0; i < 256; 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
|
8889 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
|
8890 if (PyType_Ready(&PyUnicode_Type) < 0) |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
8891 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
|
8892 |
|
af46c7ed05eb
needforspeed: use append+reverse for rsplit, use "bloom filters" to
Fredrik Lundh <fredrik@pythonware.com>
parents:
38243
diff
changeset
|
8893 /* 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
|
8894 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
|
8895 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
|
8896 ); |
|
38786
9c52efcaff65
Initialize the type object so pychecker can't crash the interpreter.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38709
diff
changeset
|
8897 |
|
9c52efcaff65
Initialize the type object so pychecker can't crash the interpreter.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38709
diff
changeset
|
8898 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
|
8899 } |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8900 |
|
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8901 /* 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
|
8902 |
|
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
|
8903 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
|
8904 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
|
8905 { |
|
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
|
8906 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
|
8907 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
|
8908 |
|
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
|
8909 for (u = free_list; u != 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
|
8910 PyUnicodeObject *v = 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
|
8911 u = *(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
|
8912 if (v->str) |
|
45804
d33cadb595f6
Issue 2321: reduce memory usage (increase the memory that is returned
Neal Norwitz <nnorwitz@gmail.com>
parents:
45731
diff
changeset
|
8913 PyObject_DEL(v->str); |
|
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
|
8914 Py_XDECREF(v->defenc); |
|
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
|
8915 PyObject_Del(v); |
|
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
|
8916 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
|
8917 } |
|
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
|
8918 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
|
8919 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
|
8920 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
|
8921 } |
|
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
|
8922 |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8923 void |
|
13660
a1ce07218781
Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents:
13635
diff
changeset
|
8924 _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
|
8925 { |
|
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
|
8926 int i; |
|
11968
13bc68faeb65
Unicode implementation by Marc-Andre Lemburg based on original code by
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8927 |
|
14998
c16bdf555c79
In _PyUnicode_Fini(), decref unicode_empty before tearng down the free
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
8928 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
|
8929 unicode_empty = NULL; |
|
15001
a349b89bf10e
_PyUnicode_Fini(): Initialize the local freelist walking variable `u'
Barry Warsaw <barry@python.org>
parents:
14998
diff
changeset
|
8930 |
|
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
|
8931 for (i = 0; i < 256; 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
|
8932 if (unicode_latin1[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
|
8933 Py_DECREF(unicode_latin1[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
|
8934 unicode_latin1[i] = 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
|
8935 } |
|
b44ba7c01217
This patch originated from an idea by Martin v. Loewis who submitted a
Marc-André Lemburg <mal@egenix.com>
parents:
17600
diff
changeset
|
8936 } |
|
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
|
8937 (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
|
8938 } |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
8939 |
|
37655
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
8940 #ifdef __cplusplus |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
8941 } |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
8942 #endif |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
8943 |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37596
diff
changeset
|
8944 |
|
29164
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
8945 /* |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
8946 Local variables: |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
8947 c-basic-offset: 4 |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
8948 indent-tabs-mode: nil |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
8949 End: |
|
4d9017ef7b5f
Consider \U-escapes in raw-unicode-escape. Fixes #444514.
Martin v. Löwis <martin@v.loewis.de>
parents:
28575
diff
changeset
|
8950 */ |
